我正在尝试从 python 脚本(运行 Python 2.7.1)为 tcsh 创建别名。 创建别名后,我想在运行 python 脚本的同一个 shell 中使用它们。
我试过:
os.system('alias test "echo test"')
但我收到以下错误:
sh: line 0: alias: test: not found
sh: line 0: alias: echo test: not found
然后我尝试了:
os.system(r"""/bin/csh -i -c 'alias test "echo test"'""")
然后没有发生错误,但是别名没有注册,因此我无法使用它。
我要找的结果是这样的:
tcsh>python my_script.py
tcsh>test
test
谢谢!
最佳答案
os.system
在子 shell(从外观上看是 bourne shell)中执行该命令,因此即使您的语法正确 alias test="echo test"
,它不会在调用后持续存在(因为子 shell 关闭)。
但这似乎是一个 XY 问题。你问的是 Y - 你想到的解决方案,而不是 X - 你的问题。
如果您只想一次创建一堆别名,为什么不使用 c-shell 脚本!? (为什么要用 c-shell 折磨自己完全是另一回事)。
关于python - 如何使用 python 创建 tcsh 别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110121/