在 python 2.4 中,如何使用 csh 而不是 bash 执行外部命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/533398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:20:02  来源:igfitidea点击:

In python 2.4, how can I execute external commands with csh instead of bash?

pythonshellcshtcsh

提问by Ross Rogers

Without using the new 2.6 subprocess module, how can I get either os.popen or os.system to execute my commands using the tcsh instead of bash? I need to source some scripts which are written in tcsh before executing some other commands and I need to do this within python2.4.

不使用新的 2.6 子进程模块,我怎样才能让 os.popen 或 os.system 使用 tcsh 而不是 bash 来执行我的命令?我需要在执行其他一些命令之前获取一些用 tcsh 编写的脚本,我需要在 python2.4 中执行此操作。

EDIT Thanks for answers using 'tcsh -c', but I'd like to avoid this because I have to do escape madness. The string will be interpreted by bash and then interpreted by tcsh. I'll have to do something like:

编辑感谢使用“tcsh -c”的答案,但我想避免这种情况,因为我必须逃避疯狂。该字符串将由 bash 解释,然后由 tcsh 解释。我将不得不做这样的事情:

os.system("tcsh -c '"+re.compile("'").sub(r"""'"'"'""",my_cmd)+"'")

Can't I just tell python to open up a 'tcsh' sub-process instead of a 'bash' subprocess? Is that possible?

我不能告诉 python 打开一个 'tcsh' 子进程而不是一个 'bash' 子进程吗?那可能吗?

P.S. I realize that bash is the cat's meow, but I'm working in a corporate environment and I'm going to choose to notfight a tcsh vs bash battle -- bigger fish to fry.

PS 我意识到 bash 是猫的喵喵叫,但我在公司环境中工作,我将选择打 tcsh 与 bash 的战斗——要炸更大的鱼。

回答by anthony

Just prefix the shell as part of your command. I don't have tcsh installed but with zsh:

只需将 shell 作为命令的一部分添加前缀即可。我没有安装 tcsh,但安装了 zsh:

>>> os.system ("zsh -c 'echo 
>>> os.system("tcsh your_own_script")
'") zsh 0

回答by Torsten Marek

How about:

怎么样:

#!/bin/tcsh

Or just write the script and add

或者只是编写脚本并添加

>>> os.environ['SHELL'] = 'tcsh'
>>> os.environ['SHELL']
'tcsh'
>>> os.system("echo $SHELL")
tcsh

at the beginning of the file and let the OS take care of that.

在文件的开头,让操作系统来处理。

回答by Chris Bunch

Just set the shell to use to be tcsh:

只需将要使用的外壳设置为tcsh

##代码##