ent*_*mum 5 python shell subprocess
我有一个调用另一个shell脚本的Python脚本 -
arg1 = "-a"
arg2 = "-b"
arg3 = "Some String"
argList = ["script.sh", arg1, arg2, arg3]
subprocess.call(argList)
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,arg3被拆分为"Some"和,"String"并作为两个单独的参数传递.
我怎样才能克服这一点"Some String"作为一个论点传递?
编辑:已解决
我通过将参数传递为$ 1,$ 2,$ 3等来调用被调用脚本内部的另一个函数.当我应该引用可能有$ 1,$ 2,$ 3等空格的参数时..等.对不起为了混乱.
这应该有效:
import subprocess
arg1 = "-a"
arg2 = "-b"
arg3 = "Some String"
argList = ["sh", "script.sh", arg1, arg2, arg3]
subprocess.call(argList)
Run Code Online (Sandbox Code Playgroud)
和里面script.sh
printf '%s\n' "$@"
Run Code Online (Sandbox Code Playgroud)
确保"$@"加引号以避免 shell 脚本中的分词。
这将输出:
-a
-b
Some String
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
158 次 |
| 最近记录: |