python 如何通过subprocess.call调用自定义alias别名

2019-1-20 杜世伟 Python

为了更好的通过Python脚本执行linux命令,通过自定义别名(Alias)进行多个命令组合,然而通过python中如何通过subprocess类库

自定义alias

#alias lt='ls --human-readable --size -1 -S --classify'
alias lt='du -sh * | sort -h'

测试调用的Python代码

from subprocess import call

def test():
    call("lt")

if __name__ == "__main__":
    test()

直接运行上面的代码提示错误信息如下

Traceback (most recent call last):
  File "test_alias.py", line 7, in <module>
    test()
  File "test_alias.py", line 4, in test
    call("lt")
  File "/Users/shiwei/anaconda3/lib/python3.7/subprocess.py", line 323, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/Users/shiwei/anaconda3/lib/python3.7/subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "/Users/shiwei/anaconda3/lib/python3.7/subprocess.py", line 1522, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'lt': 'lt'

通过错误信息可以看出lt命令被当作了文件或目录提示找不到

如果您需要的别名是在 ~/.bashrc 中定义的,可以通过尝试以下几种方式进行运行:

1)你必须给’shell’关键字:

subprocess.call('command', shell=True)

否则,您给定的命令用于查找可执行文件,而不是传递给 shell,它是扩展别名和函数等内容的 shell。

from subprocess import call

def test():
    call("lt", shell=True)

if __name__ == "__main__":
    test()

运行此处代码发现报错找不到文件或目录的错误了,但是出现了新的错误,错误信息如下

# /bin/sh: lt: command not found

有错误信息可以看出还是找不到lt命令,不过不报错误了(此处应该有掌声)

2) 默认情况下,subprocess.call 使用’/bin/sh’ shell。如果这是您要调用的 Bash 别名,则需要使用“可执行”关键字 告诉子进程使用 bash 而不是 sh:

subprocess.call('command', shell=True, executable='/bin/bash')

代码如下

from subprocess import call

def test():
    call("lt", shell=True, executable='/bin/bash')

if __name__ == "__main__":
    test()

通过执行代码发现,报错的错误同上,还是提示lt找到

3) 但是,/bin/bash 除非作为“交互式”shell(使用“-i”)启动,否则不会获取 ~/.bashrc。不幸的是,您不能传递 executable=’/bin/bash -i’,因为它认为整个值是可执行文件的名称。因此,如果您的别名是在用户的正常交互式启动中定义的,例如在 .bashrc 中,您必须使用以下替代形式调用命令:

subprocess.call(['/bin/bash', '-i', '-c', 命令])
# i.e. shell=False (the default)
from subprocess import call

def test():
    call(['/bin/bash', '-i', '-c', 'lt'])

if __name__ == "__main__":
    test()

正常执行,成功调用了alias命令

标签: python subprocess alias

评论(0) 浏览(5368)

10 个 Linux 中方便的 Bash 别名

2018-10-10 杜世伟 Linux

你有多少次在命令行上输入一个长命令,并希望有一种方法可以保存它以供日后使用?这就是 Bash 别名派上用场的地方。它们允许你将长而神秘的命令压缩为易于记忆和使用的东西。需要一些例子来帮助你入门吗?没问题!

要使用你创建的 Bash 别名,你需要将其添加到 .bash_profile 中,该文件位于你的家目录中。请注意,此文件是隐藏的,并只能从命令行访问。编辑此文件的最简单方法是使用 Vi 或 Nano 之类的东西。

1、 你有几次遇到需要解压 .tar 文件但无法记住所需的确切参数?别名可以帮助你!只需将以下内容添加到 .bash_profile 中,然后使用 untar FileName 解压缩任何 .tar 文件。

alias untar='tar -zxvf '
2、 想要下载的东西,但如果出现问题可以恢复吗?

alias wget='wget -c '

3、 是否需要为新的网络帐户生成随机的 20 个字符的密码?没问题。

alias getpass="openssl rand -base64 20"

阅读全文>>

标签: linux wget tar alias

评论(0) 浏览(2587)

Powered by emlog 沪ICP备2023034538号-1