将Powershell脚本中包含“\t”的文件路径作为参数传递给Jython脚本的最佳方法

2024-07-05 10:47:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Windows服务器上使用Powershell作为主脚本语言,在这个特殊情况下,我使用它触发由wsadmin(WebSphere命令行)运行的Jython脚本

其中一个参数是文件的完整路径,除了路径分隔符后面紧跟字母“t”之外,其他参数都可以,Jython将其理解为“\t”,换句话说,是tab的特殊字符

我在脚本中添加了三行,如下所示

print getTimeStamp() + " [DEBUG] This is the name of the script: ", str(sys.argv[0])
print getTimeStamp() + " [DEBUG] Number of arguments: ", len(sys.argv)
print getTimeStamp() + " [INFO] Script argument should be the patht to the property file: " , str(sys.argv)

如果脚本的参数没有“特殊字符”,则输出如下

2021-01-14 15:55:48 [DEBUG] This is the name of the script:  e:\scripts\klpbpm\deployments\klporders-6336\stest-dns-aliaes.prop]
2021-01-14 15:55:48 [DEBUG] Number of arguments:  1
2021-01-14 15:55:48 [INFO] Script argument should be the patht to the property file:  ['e:\\scripts\\klpbpm\\deployments\\klporders-6336\\stest-dns-aliaes.prop]']

但是,当文件以字母“t”开头时,它是另一个故事,它丢失并导致脚本失败

2021-01-14 23:23:42 [DEBUG] This is the name of the script:  e:\scripts\klpbpm\deployments\klporders-6336   est-dns-aliaes.prop]
2021-01-14 23:23:42 [DEBUG] Number of arguments:  1
2021-01-14 23:23:42 [INFO] Script argument should be the patht to the property file:  ['e:\\scripts\\klpbpm\\deployments\\klporders-6336\test-dns-aliaes.prop]']

有没有关于如何避免这个问题的提示


Tags: ofthedebug脚本参数dnsscriptsthis
1条回答
网友
1楼 · 发布于 2024-07-05 10:47:12

解决此问题的最简单方法是保持py脚本不变,只需将参数封装在单引号中:

bash-4.2$ ./wsadmin.sh -f test.py 'e:\\dir1\\dir2\\ta.prop'
 WASX7209I: Connected to process "server1" on node DefaultNode01 using SOAP connector;  The type of process is: UnManagedProcess
WASX7303I: The following options are passed to the scripting environment and are available as arguments that are stored in the argv variable: "[e:\\dir1\\dir2\\ta.prop]"
[DEBUG] This is the name of the script:  e:\dir1\dir2\ta.prop
[DEBUG] Number of arguments:  1
[INFO] Script argument should be the path to the property file:  ['e:\\dir1\\dir2\\ta.prop']

相关问题 更多 >