有没有办法改变 os.system() 调用的 SHELL 程序?

Is there a way to change the SHELL program called by os.system()?

我处于一个嵌入式环境中,其中/bin/sh不存在,所以当我调用posix.system()os.system()时,它总是returns-1
由于该环境,subprocess 模块不可用。我也不会创建管道。

我正在使用 ɴᴀᴄʟ as glibc

设置SHELL环境变量似乎不起作用(它仍然尝试打开/bin/sh.

那么有没有办法改变 os.system() 调用的 shell 进程?

不,您不能更改 shell os.system() 使用的内容,因为该函数调用 system() C function,并且该函数具有 shell 硬编码至 /bin/sh.

改用 subprocess.call() function,并将 executable 参数设置为您要使用的 shell:

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

来自 Popen() documentation(所有 subprocess 功能的基础):

On Unix with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell.

If shell=True, on Unix the executable argument specifies a replacement shell for the default /bin/sh.

如果你不能使用 subprocess 你不能使用管道,你将被限制为 os.spawn*() functions;将 mode 设置为 os.P_WAIT 以等待退出代码:

retval = os.spawnl(os.P_WAIT, '/bin/bash', '-c', 'command')