本文介绍了Python的subprocess.call一个bash别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中有列出完成的任务的脚本。这是别人写的,并托管在网络上。我在我的.bashrc一个别名调用此脚本,与它的许多标志和这样的,我想写一个python脚本,将调用这个别名每隔几分钟,所以我可以有更新的统计数据打开外壳。然而, subprocess.call(myAlias​​)失败。我还是相当新的蟒蛇,并很努力想出解决办法。

At work there's a script that lists completed tasks. This was written by someone else and is hosted over the network. I have an alias in my .bashrc that calls this script, with its many flags and such, and I wanted to write a python script that would call this alias every few minutes so I can have a shell open with updated stats. However, subprocess.call("myAlias") fails. I'm still fairly new to python, and am struggling to figure this out.

from subprocess import call

def callAlias():
    call("myAlias")

callAlias()

我计划增加更多的太多了,但我打的障碍在我的第一个步骤。 :P

I plan on adding more too it, but I hit the snag on my first step. :P

我会发布更多,但还有很多敏感机密的东西,我必须要小心。很抱歉的含糊code和错误输出的不足。

I would post more, but there's a lot of sensitive confidential stuff I have to be careful with. Sorry for the vague code, and lack of error output.

推荐答案

您需要设置关键字为True:

You need to set the shell keyword to True:

call("myAlias", shell=True)

从:

如果,指定的命令将通过在shell执行。这可如果你正在使用Python主要是为加强控制其流提供了超过大多数系统贝壳仍希望访问其他shell的功能,如文件名通配符,壳管和环境变量扩充是有用的。

别名是一个壳特征(例如它们的定义和由外壳PTED间$ P $)。

Aliases are a shell feature (e.g. they are defined and interpreted by the shell).

然而,外壳(/ bin / sh的)执行非交互的,所以没有 .profile文件的.bashrc 文件的读取,可能你的别名是不会用。

However, the shell (/bin/sh) is executed non-interactively, so no .profile or .bashrc files are read and your alias probably is not going to be available.

如果你不愿意使用完整的扩展命令到您的Python脚本,你必须使用使外壳读取文件中都定义别名反正:

If you are reluctant to use the full expanded command into your python script, you'll have to use the $ENV environment variable to make the shell read the file with the alias defined in it anyway:

call("myAlias", shell=True, env=dict(ENV='/path/to/aliasfile'))

这篇关于Python的subprocess.call一个bash别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:23
查看更多