使用python3脚本创建UNIX别名 [英] create unix alias using a python3 script

查看:72
本文介绍了使用python3脚本创建UNIX别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,在终端外壳中创建别名非常容易:

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$

但是现在我想通过Python3脚本执行相同的操作.我检查了参考手册,发现可以使用 subprocess 模块解决这类命令工作.

But now I want to do the same thing through a Python3 script. I've checked the ref manual and found these sort of command work can be solved using subprocess module.

然后我在下面编写脚本:

Then I write the script below:

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

但是请注意,此操作不会对当前使用的Shell生效,而是将使用子Shell,然后退出.因此,此脚本所做的一切都是徒劳的.

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. So what this script has done is totally in vain.

所以我的问题是:如何通过执行python脚本在当前使用的shell中创建别名?

So my problem is: how to create an alias in the currently using shell by executing a python script?

推荐答案

通常,您不能

您设置的所有别名仅在当前shell中有效,新别名只能由shell自已添加,而不是子shell或子进程添加.

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.

以hack的方式,您可以使用gdb来附加您的父shell并更改其别名表.但是在现代Unix中,不允许子进程附加父进程.您需要降低系统安全级别

In hack way, you can use gdb to attach you parent shell and change its alias table. But in modern Unix, child process is not allowed to attach parent process. You need to low down the system security level

这篇关于使用python3脚本创建UNIX别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆