简体   繁体   English

使用python3脚本创建UNIX别名

[英]create unix alias using a python3 script

Well, as we all known, creating an alias in a terminal shell is quite easy: 众所周知,在终端外壳中创建别名非常容易:

ZZ:~ zhangzhao$ alias c='uname'
ZZ:~ zhangzhao$ c
Darwin
ZZ:~ zhangzhao$

But now I want to do the same thing through a Python3 script. 但是现在我想通过Python3脚本执行相同的操作。 I've checked the ref manual and found these sort of command work can be solved using subprocess module. 我检查了参考手册,发现可以使用subprocess模块解决这类命令工作。

Then I write the script below: 然后,我在下面编写脚本:

import subprocess
subprocess.call(["alias", "c=\'uname\'"])

But note that this operation will not take effect to the shell you are currently using, instead, it will use a subshell and then leave. 但是请注意,此操作不会对当前使用的Shell生效,而是将使用子Shell,然后退出。 So what this script has done is totally in vain. 因此,此脚本所做的一切都是徒劳的。

So my problem is: how to create an alias in the currently using shell by executing a python script? 所以我的问题是:如何通过执行python脚本在当前使用的shell中创建别名?

In general, you can't 一般来说,你不能

All alias you set in only works in current shell and new aliaes can be added only by shell it self rather than sub-shell or sub-process. 您在其中设置的所有别名仅在当前Shell中有效,新别名只能由Shell自已添加,而不是子Shell或子进程添加。

In hack way, you can use gdb to attach you parent shell and change its alias table. 以黑客的方式,您可以使用gdb来附加您的父shell并更改其别名表。 But in modern Unix, child process is not allowed to attach parent process. 但是在现代Unix中,不允许子进程附加父进程。 You need to low down the system security level 您需要降低系统安全级别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM