使用subprocess.Popen()函数,你可以通过"executable"参数指定你想要的shell。
我选择了传递"/bin/tcsh",并且不希望tcsh读取我的~/.cshrc
文件。
tcsh手册指出,我需要向/bin/tcsh
传递-f
参数来实现这一点。
如何让Popen执行带有-f选项的/bin/tcsh呢?
import subprocess
cmd = ["echo hi"]
print cmd
proc = subprocess.Popen(cmd, shell=False, executable="/bin/tcsh", stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return_code = proc.wait()
for line in proc.stdout:
print("stdout: " + line.rstrip())
for line in proc.stderr:
print("stderr: " + line.rstrip())
print return_code