克服 Python 2.3 中的 os.system() 限制

发布于 2024-09-09 16:26:51 字数 1072 浏览 2 评论 0原文

我在将公司的一个脚本从 csh 转换为 Python 时遇到问题。 csh 脚本调用别名命令,但是当我通过 os.system() 调用相同的别名命令时,它不起作用。

因此,如果 foo 是别名命令:

CSH 脚本(这有效,执行 foo):

foo <argument>

Python(这不起作用,错误声明 foo 是未知命令):

os.system("foo <argument>")

我认为一定有某种当我执行 python 脚本时发生上下文切换,这导致 Python 无法访问 shell 中创建的别名。阅读文档后,似乎 os.system 已被弃用,取而代之的是 subprocess,并且 subprocess 有一个 shell< /code> 参数可能可以帮助我......问题是我一直在使用 Python 2.3,并且 subprocess 直到版本 2.4 才可用。

我想到了在 python 中复制别名。例如,假设 foocp 的别名。

CSH:

alias foo cp
foo file1 file2

Python:

os.system("alias foo cp")
os.system("foo file1 file2")

没有骰子...这里也有错误:

sh: line 0: alias: foo: not found
sh: line 0: alias: cp: not found
sh: foo: command not found

我最后的努力是获取使用 foo 的几行代码并将它们放入 Python 会调用的自己的 CSH 脚本中。但如果有一种方法可以在不诉诸其他方法的情况下完成这项工作,我很想知道。

提前致谢!

I am having a problem converting one of my company's scripts from csh to Python. The csh script calls an aliased command, but when I call that same aliased command via os.system(), it does not work.

So, if foo is the aliased command:

CSH Script (this works, executes foo):

foo <argument>

Python (this does not work, error claims foo is an unknown command):

os.system("foo <argument>")

I figure there must be some kind of context switch happening when I execute the python script, which causes Python to not have access to the aliases created in the shell. After reading the documentation, it seems that os.system is being deprecated in favor of subprocess, and that subprocess has a shell parameter that might be able to help me out...problem is that I'm stuck using Python 2.3, and subprocess isn't available until version 2.4.

I had the idea to replicate the alias in python. So, for example, say foo was aliased to cp.

CSH:

alias foo cp
foo file1 file2

Python:

os.system("alias foo cp")
os.system("foo file1 file2")

No dice...errors here as well:

sh: line 0: alias: foo: not found
sh: line 0: alias: cp: not found
sh: foo: command not found

My last ditch effort is to take the few lines of code that use foo and put them into their own CSH script that Python would call. But if there's a way to make this work without resorting to that, I'd love to know about it.

Thanks in advance!

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

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

发布评论

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

评论(2

把回忆走一遍 2024-09-16 16:26:51

是什么让您认为 os.system 会使用 csh?它使用标准的C函数系统,在Unix系统上只会调用基本的/bin/sh。这不会是 csh,但很可能是 bash,或者它的一些更简单的版本。

顺便说一句:请注意,您在 os.system 中对 shell 环境所做的操作不会影响对 os.system 的后续调用,因为每个都在不同的子 shell 中运行。换句话说,对环境所做的改变将会丢失。并且对 alias 的调用失败,因为 /bin/sh 使用与 csh 不同的别名语法。

您可以通过不运行 foo 而是运行以下内容来解决此问题:

os.system("/bin/csh -i -c 'foo arg1 arg2'")

注意选项 -i,它应该强制 csh 读取启动脚本。

What made you think os.system would use csh? It uses standard C function system, that on Unix system will call just basic /bin/sh. This will not be csh, but most probably bash, or some simpler version of it.

BTW: note that what you do with shell environment in os.system will not affect subsequent calls to os.system, because each is run in different subshell. In other words, changes made to the environment are lost. And your call to alias fails, because /bin/sh uses different syntax for aliases than csh.

You could workaround this by running not foo, but something along the lines:

os.system("/bin/csh -i -c 'foo arg1 arg2'")

Note the option -i which is supposed to force csh to read startup scripts.

世界和平 2024-09-16 16:26:51

如果您愿意在 Python 中使用“foo”别名,请在调用 os.system 之前自行执行别名设置:

cmd = "foo file1 file2"
foo_alias = "cp"

cmd = re.sub("^foo ", foo_alias + " ", cmd)
os.system(cmd)

如果 foo 别名更复杂(使用参数替换等),这可能会更困难。

If you are willing to have the "foo" alias in Python, then perform the aliasing yourself before calling os.system:

cmd = "foo file1 file2"
foo_alias = "cp"

cmd = re.sub("^foo ", foo_alias + " ", cmd)
os.system(cmd)

If the foo alias is more elaborate (with argument substitution, etc), this could be more difficult.

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