我想知道是否subprocess.call("if [ ! -d '{output}' ]; then mkdir -p {output}; fi",shell=True)
将被解释sh
或zsh
代替bash
在不同服务器?
任何人有这个想法?
我应该怎么做,以确保它是由解释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
参数来替换默认:
...如果壳=真,在Unix上执行的参数指定默认的/ bin / sh的更换外壳。
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