从python程序将参数传递给shell脚本

时间:2015-02-18 06:02:33

标签: python shell subprocess

我想从我的python脚本调用一个shell脚本。我需要将3个参数/参数传递给shell脚本。我能够调用shell脚本(与python脚本位于同一目录中),但是参数传递存在一些问题

from subprocess import call

// other code here.
line = "Hello"
// Here is how I call the shell command
call (["./myscript.sh", "/usr/share/file1.txt", ""/usr/share/file2.txt", line], shell=True)

In my shell script I have this
#!/bin/sh

echo "Parameters are $1 $2 $3"
...

Unfortunately parameters are not getting passed correctly.

I get this message:

Parameters are 

None of the parameter values are passed in the script

2 个答案:

答案 0 :(得分:2)

call ("./myscript.sh /usr/share/file1.txt /usr/share/file2.txt "+line, shell=True)

当您使用shell=True时,您可以直接传递命令,就像直接传递shell一样。

答案 1 :(得分:0)

删除shell=True(您可能需要myscript.sh可执行文件:$ chmod +x myscript.sh):

#!/usr/bin/env python
from subprocess import check_call

line = "Hello world!"
check_call(["./myscript.sh", "/usr/share/file1.txt", "/usr/share/file2.txt", 
            line])

Do not use a list argument and shell=True together

相关问题