I am trying to run both Python and bash commands in a bash script. In the bash script, I want to execute some bash commands enclosed by a Python loop:
我试图在bash脚本中运行Python和bash命令。在bash脚本中,我想执行Python循环包含的一些bash命令:
#!/bin/bash
python << END
for i in range(1000):
#execute some bash command such as echoing i
END
How can I do this?
我怎样才能做到这一点?
2 个解决方案
#1
38
Use subprocess, e.g.:
使用子进程,例如:
import subprocess
# ...
subprocess.call(["echo", i])
There is another function like subprocess.call
: subprocess.check_call
. It is exactly like call, just that it throws an exception if the command executed returned with a non-zero exit code. This is often feasible behaviour in scripts and utilities.
还有另一个函数,如subprocess.call:subprocess.check_call。它与调用完全相同,只是如果执行的命令以非零退出代码返回,则抛出异常。这通常是脚本和实用程序中的可行行为。
subprocess.check_output
behaves the same as check_call
, but returns the standard output of the program.
subprocess.check_output的行为与check_call相同,但返回程序的标准输出。
If you do not need shell features (such as variable expansion, wildcards, ...), never use shell=True (shell=False is the default). If you use shell=True then shell escaping is your job with these functions and they're a security hole if passed unvalidated user input.
如果您不需要shell功能(例如变量扩展,通配符等),请永远不要使用shell = True(shell = False是默认值)。如果你使用shell = True,那么shell转义就是你使用这些函数的工作,如果传递了未经验证的用户输入,它们就是一个安全漏洞。
The same is true of os.system() -- it is a frequent source of security issues. Don't use it.
os.system()也是如此 - 它是安全问题的常见来源。不要使用它。
#2
16
Look in to the subprocess module. There is the Popen method and some wrapper functions like call
.
查看子进程模块。有Popen方法和一些包装函数,如call。
-
If you need to check the output (retrieve the result string):
如果需要检查输出(检索结果字符串):
output = subprocess.check_output(args ....)
-
If you want to wait for execution to end before proceeding:
如果要在继续之前等待执行结束:
exitcode = subprocess.call(args ....)
-
If you need more functionality like setting environment variables, use the underlying Popen constructor:
如果您需要更多功能,如设置环境变量,请使用基础Popen构造函数:
subprocess.Popen(args ...)
Remember subprocess is the higher level module. It should replace legacy functions from OS module.
记住子进程是更高级别的模块。它应该替换OS模块中的旧功能。
#1
38
Use subprocess, e.g.:
使用子进程,例如:
import subprocess
# ...
subprocess.call(["echo", i])
There is another function like subprocess.call
: subprocess.check_call
. It is exactly like call, just that it throws an exception if the command executed returned with a non-zero exit code. This is often feasible behaviour in scripts and utilities.
还有另一个函数,如subprocess.call:subprocess.check_call。它与调用完全相同,只是如果执行的命令以非零退出代码返回,则抛出异常。这通常是脚本和实用程序中的可行行为。
subprocess.check_output
behaves the same as check_call
, but returns the standard output of the program.
subprocess.check_output的行为与check_call相同,但返回程序的标准输出。
If you do not need shell features (such as variable expansion, wildcards, ...), never use shell=True (shell=False is the default). If you use shell=True then shell escaping is your job with these functions and they're a security hole if passed unvalidated user input.
如果您不需要shell功能(例如变量扩展,通配符等),请永远不要使用shell = True(shell = False是默认值)。如果你使用shell = True,那么shell转义就是你使用这些函数的工作,如果传递了未经验证的用户输入,它们就是一个安全漏洞。
The same is true of os.system() -- it is a frequent source of security issues. Don't use it.
os.system()也是如此 - 它是安全问题的常见来源。不要使用它。
#2
16
Look in to the subprocess module. There is the Popen method and some wrapper functions like call
.
查看子进程模块。有Popen方法和一些包装函数,如call。
-
If you need to check the output (retrieve the result string):
如果需要检查输出(检索结果字符串):
output = subprocess.check_output(args ....)
-
If you want to wait for execution to end before proceeding:
如果要在继续之前等待执行结束:
exitcode = subprocess.call(args ....)
-
If you need more functionality like setting environment variables, use the underlying Popen constructor:
如果您需要更多功能,如设置环境变量,请使用基础Popen构造函数:
subprocess.Popen(args ...)
Remember subprocess is the higher level module. It should replace legacy functions from OS module.
记住子进程是更高级别的模块。它应该替换OS模块中的旧功能。