python - 如何使用 python 创建 tcsh 别名?

标签 python linux tcsh

我正在尝试从 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/

相关文章:

vim - 如何从 shell 脚本启动编辑器?

python - 防止用户多次提交喜欢或不喜欢的信息

Python,合并多级字典

c - 在此信号处理程序中会发生什么?

python - 如何将信息提供给 Python 守护进程?

c - 使用字符串表和打印部分名称

unix - tcl/tcsh 混合脚本上错误放置 () 的错误

python - 如何在 python 中获取新接受的连接的端口号?

python - Numba 中的 Numpy 聚合函数垫片、打字和 np.sort()

shell - 检查文件是否不存在或比 csh 中的另一个文件旧