请问`shell`在'壳= TRUE;在子意味着`bash`?(Does the `shel

2019-07-21 18:43发布

我想知道是否subprocess.call("if [ ! -d '{output}' ]; then mkdir -p {output}; fi",shell=True)将被解释shzsh代替bash在不同服务器?

任何人有这个想法?

我应该怎么做,以确保它是由解释bash

Answer 1:

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")


Answer 2:

您可以显式调用您所选择的外壳,但示例代码您发布,这是不是最好的方法。 相反,只需直接用Python语言编写的代码。 在这里看到: 在Python的mkdir -p功能



Answer 3:

要指定外壳, 使用可执行参数与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


文章来源: Does the `shell` in `shell=True` in subprocess means `bash`?