Python:使用参数(变量)执行shell脚本,但shell脚本中未读取参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19325761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:26:49  来源:igfitidea点击:

Python: executing shell script with arguments(variable), but argument is not read in shell script

pythonshellsubprocesspopen

提问by creativeDrive

I am trying to execute a shell script(not command) from python:

我正在尝试从 python 执行一个 shell 脚本(不是命令):

main.py
-------
from subprocess import Popen

Process=Popen(['./childdir/execute.sh',str(var1),str(var2)],shell=True)

execute.sh
----------

echo  //does not print anything
echo  //does not print anything

var1 and var2 are some string that I am using as an input to shell script. Am I missing something or is there another way to do it?

var1 和 var2 是我用作 shell 脚本输入的一些字符串。我错过了什么还是有其他方法可以做到?

Referred: How to use subprocess popen Python

参考:如何使用子进程 popen Python

采纳答案by micromoses

The problem is with shell=True. Either remove that argument, or pass all arguments as a string, as follows:

问题在于shell=True. 删除该参数,或将所有参数作为字符串传递,如下所示:

Process=Popen('./childdir/execute.sh %s %s' % (str(var1),str(var2),), shell=True)

The shell will only pass the arguments you provide in the 1st argument of Popento the process, as it does the interpretation of arguments itself. See a similar question answered here.What actually happens is your shell script gets no arguments, so $1 and $2 are empty.

shell 只会将您在第一个参数中提供的参数Popen传递给进程,因为它本身会解释参数。请参阅此处回答的类似问题实际发生的是你的 shell 脚本没有参数,所以 $1 和 $2 是空的。

Popen will inherit stdout and stderr from the python script, so usually there's no need to provide the stdin=and stderr=arguments to Popen (unless you run the script with output redirection, such as >). You should do this only if you need to read the output inside the python script, and manipulate it somehow.

Popen 将从 python 脚本继承 stdout 和 stderr,因此通常不需要向 Popen提供stdin=stderr=参数(除非您运行带有输出重定向的脚本,例如>)。只有当您需要读取 python 脚本中的输出并以某种方式对其进行操作时,才应该这样做。

If all you need is to get the output (and don't mind running synchronously), I'd recommend trying check_output, as it is easier to get output than Popen:

如果您只需要获取输出(并且不介意同步运行),我建议您尝试check_output,因为获取输出比获取输出更容易Popen

output = subprocess.check_output(['./childdir/execute.sh',str(var1),str(var2)])
print(output)

Notice that check_outputand check_callhave the same rules for the shell=argument as Popen.

请注意,check_outputcheck_call具有与 相同的shell=参数规则Popen

回答by Joran Beasley

you actually are sending the arguments ... if your shell script wrote a file instead of printing you would see it. you need to communicate to see your printed output from the script ...

你实际上是在发送参数......如果你的shell脚本写了一个文件而不是打印你会看到它。您需要进行通信以查看脚本的打印输出...

from subprocess import Popen,PIPE

Process=Popen(['./childdir/execute.sh',str(var1),str(var2)],shell=True,stdin=PIPE,stderr=PIPE)
print Process.communicate() #now you should see your output