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

发布于 2024-07-14 04:50:58 字数 469 浏览 5 评论 0原文

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

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

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

我不能告诉 python 打开“tcsh”子进程而不是“bash”子进程吗? 那可能吗?

聚苯乙烯 我意识到 bash 是猫的叫声,但我在公司环境中工作,我将选择进行 tcsh 与 bash 的战斗 - 更大的鱼要煎。

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.

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:

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?

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 not fight a tcsh vs bash battle -- bigger fish to fry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断爱 2024-07-21 04:50:59

只需将要使用的 shell 设置为 tcsh:

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

Just set the shell to use to be tcsh:

>>> os.environ['SHELL'] = 'tcsh'
>>> os.environ['SHELL']
'tcsh'
>>> os.system("echo $SHELL")
tcsh
居里长安 2024-07-21 04:50:58

只需在命令中添加 shell 前缀即可。 我没有安装 tcsh,但安装了 zsh:

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

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

>>> os.system ("zsh -c 'echo $0'")
zsh
0
山田美奈子 2024-07-21 04:50:58

怎么样:

>>> os.system("tcsh your_own_script")

或者只是编写脚本并添加到

#!/bin/tcsh

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

How about:

>>> os.system("tcsh your_own_script")

Or just write the script and add

#!/bin/tcsh

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文