在使用运行命令时不执行Shell扩展`子流程调用'带'shell=True'

2024-10-08 20:17:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python构建一些文件,但是它以错误的方式执行它。你知道吗

我试着在linux中构建一些文件。 当我在终端中使用“make ./package/feeds/proj/{clean,compile} V=s”命令时,它工作正常,但是当我尝试用python脚本运行它时,使用命令“p = subprocess.call(r'/usr/bin/make package/feeds/proj/{clean,compile} V=s',shell = True))”,它的行为不同。你知道吗

日志:

试运行终端时:

make[1]: Entering directory '/local/mnt/workspace/rubaeshk/unused2/qsdk'
make[2]: Entering directory '/local/mnt/workspace/rubaeshk/unused2/qsdk/package/feeds/whc/qca-whc-crash-log'
rm -f /local/mnt/workspace/rubaeshk/unused2/qsdk/bin/ipq/packages/whc/qca-whc-crash-log_*
..(log continued until successfully built)

运行python脚本时:

WARNING: your configuration is out of sync. Please run make menuconfig, oldconfig or defconfig!
make[1]: Entering directory '/local/mnt/workspace/rubaeshk/unused2/qsdk'
make[1]: *** No rule to make target 'package/feeds/whc/qca-whc-crash-log/{clean,compile}'.  Stop.
make[1]: Leaving directory '/local/mnt/workspace/rubaeshk/unused2/qsdk'
/local/mnt/workspace/rubaeshk/unused2/qsdk/include/toplevel.mk:186: recipe for target 'package/feeds/whc/qca-whc-crash-log/{clean,compile}' failed
make: *** [package/feeds/whc/qca-whc-crash-log/{clean,compile}] Error 2

有人能看出哪里出了问题吗。。你知道吗


Tags: cleanlogpackagemakelocalcrashworkspacefeeds
2条回答

Brace扩展不是标准shell的一部分;它是一些shell(比如bash)在POSIX之外提供的附加功能。当你跑的时候子流程调用在Python中,它可能使用/bin/sh,而不是/bin/bash。你知道吗

所以,写出来:package/feeds/whc/qca-whc-crash-log/clean package/feeds/whc/qca-whc-crash-log/compile

正如在^{} documentationsubprocess.call委托给Popen中所解释的,与所有其他方便函数一样,subprocess.call('command', shell=True)在Unix中等同于运行argv

['/bin/sh', '-c', 'command']

而且sh不支持大括号扩展(这是{a,b}语法的正式名称)。你知道吗

要使用bash运行命令,需要重写与executable参数一起使用的shell可执行文件:

p = subprocess.call('command', shell=True, executable='/bin/bash')

示例:

$ python -c 'import subprocess; subprocess.call("echo /usr/{lib,bin}", shell=True, executable="/bin/bash")'
/usr/lib /usr/bin

但是请注意,不鼓励使用shell=True,因为它本身是特定于平台的,依赖于本地shell及其设置,如果使用不受信任的输入,则可能是一个bug或安全漏洞。最好手动构造命令行并传递结果argv。

相关问题 更多 >

    热门问题