[英]Does the `shell` in `shell=True` in subprocess means `bash`?
我想知道subprocess.call("if [ ! -d '{output}' ]; then mkdir -p {output}; fi",shell=True)
是否将由sh
或zsh
解释,而不是由bash
在其他服务器中解释?
有人对此有想法吗?
我应该怎么做才能确保它被bash
解释?
http://docs.python.org/2/library/subprocess.html
On Unix with shell=True, the shell defaults to /bin/sh
请注意,/ bin / sh通常与不同的符号链接,例如在ubuntu上:
$ ls -la /bin/sh
lrwxrwxrwx 1 root root 4 Mar 29 2012 /bin/sh -> dash
您可以使用executable
参数替换默认值:
...如果shell = True,则在Unix上,可执行参数为默认的/ bin / sh指定替换shell。
subprocess.call("if [ ! -d '{output}' ]; then mkdir -p {output}; fi",
shell=True,
executable="/bin/bash")
您可以显式调用您选择的外壳,但是对于您发布的示例代码,这不是最佳方法。 相反,只需直接用Python编写代码。 参见此处: Python中的mkdir -p功能
如果shell = True,则在Unix上,可执行参数为默认的/ bin / sh指定替换壳。
In [26]: subprocess.call("if [ ! -d '{output}' ]; then mkdir -p {output}; fi", shell=True, executable='/bin/bash')
Out[26]: 0
显然,使用可执行参数更干净,但是也可以从sh调用bash:
In [27]: subprocess.call('''bash -c "if [ ! -d '{output}' ]; then mkdir -p {output}; fi"''', shell=True)
Out[27]: 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.