我正在尝试使用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
有没有人知道哪里出了问题..
发布于 2019-06-21 00:04:04
支架扩展不是标准shell的一部分;它是一些shell(如bash)在POSIX之外提供的附加特性。在Python中运行subprocess.call时,它可能使用的是/bin/sh
,而不是/bin/bash
。
所以,把它写出来:package/feeds/whc/qca-whc-crash-log/clean package/feeds/whc/qca-whc-crash-log/compile
发布于 2019-06-21 00:05:54
正如subprocess.Popen
documentation中所解释的(像所有其他便利函数一样,subprocess.call
将任务委托给Popen
),Unix中的subprocess.call('command', shell=True)
等同于运行argv
['/bin/sh', '-c', 'command']
而且sh
不支持大括号扩展(这是{a,b}
语法的官方名称)。
要改为使用bash
运行该命令,您需要覆盖与executable
参数一起使用的外壳可执行文件:
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及其设置,如果您使用不受信任的输入,则可能是一个错误或安全漏洞。最好手动构造命令行,并传递生成的argv。
https://stackoverflow.com/questions/56689416
复制相似问题