我可以使用别名来从一个Python脚本执行程序 [英] Can I use an alias to execute a program from a python script

查看:137
本文介绍了我可以使用别名来从一个Python脚本执行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎全新的到Python脚本,所以请原谅任何愚蠢的问题,但任何帮助任何人都可以给将是非常美联社preciated。

I am almost brand new to python scripting, so please excuse any stupid questions, but any help anyone can give would be much appreciated.

我试图写其他人使用Python脚本,并在它我需要调用一个程序,我不会总是知道的路径。要解决这个问题,我要求用户提供路径方案,该方案将工作,但我不希望用户必须提供的路径,他们运行的脚本,所以我一直在试图建立一个bash EVERY时间别名由具有脚本添加到〜/ .profile文件,并在〜/ .bashrc文件。

I am trying to write a python script for other people to use, and in it I need to call a program that I won't always know the path to. To get around that, I ask the user to provide the path to the program, which will work, but I don't want users to have to provide the path EVERY time they run the script so I have been trying to set up a bash alias by having the script add to the ~/.profile and ~/.bashrc files.

然后我就可以使用别名来从交互式bash shell中运行的程序,但是当脚本尝试运行它,我得到一个命令未找到错误...

I can then use the alias to run the program from an interactive bash shell, but when the script tries to run it, I get a "command not found" error...

我曾尝试重新采购.bashrc文件,并使用禁用了javascript -s expand_aliases命令,没有运气。

I have tried re-sourcing the .bashrc file and using the "shopt -s expand_aliases" command with no luck.

我的〜/ .bashrc中看起来是这样的:

My ~/.bashrc looks like this:

alias nuke='/Applications/Nuke6.2v4/Nuke6.2v4.app/Contents/MacOS/Nuke6.2v4'

和片剧本是这样的:

os.system('source .bashrc')
os.system('shopt -s expand_aliases')
os.system('nuke -x scriptPath')

但是,一旦剧本得到了这一点,它返回:

But once the script gets up to this point, it returns:

sh: nuke: command not found

我做得不对或有另一种方式,我可以路径永久地存储到一个程序?

Am I doing something wrong or is there another way I can permanently store the path to a program?

推荐答案

你想要的模块是

一个快速解决你的问题是使用子模块,像这样:

A quick fix to your problem is to use the subprocess module, like so:

import subprocess
sp = subprocess.Popen(["/bin/bash", "-i", "-c", "nuke -x scriptpath"])
sp.communicate()

这是等同于调用:

nuke -x scriptpath

这是在bash shell。 -i标志是告诉bash的行为,就好像它是一个交互式会话(与使用〜/ .bashrc文件)

from the bash shell. The -i flag tells bash to behave as though it's an interactive session (and use the ~/.bashrc file)

但你要真的小心你不打开自己到任何外壳注入(例如,如果该命令是从CGI页面调用)

BUT, you should be really really careful you're not opening yourself up to any shell injection (for instance, if this command is called from a CGI page)

有关用户直接从外壳调用快速scipts他们可能不能做任何更多的伤害比他们与一般的shell访问,但如果这个脚本是由一个网页叫做恶意用户可以通过东西有点像 RM -dfr〜/&安培;作为该程序。*

For quick scipts that users invoke directly from the shell they probably can't do any more damage than they could with general shell access, but if this script is called by a web page a malicious user could pass something a bit like "rm -dfr ~/ &" as the program.*

如果可执行文件的数量很少,你可能会更好在脚本中命名它们:

If the number of executables is small, you might be better off either naming them in the script:

PROGRAMS = {"nuke": "/path/to/nuke"
                "foo" : "/path/to/foo" }

# Parse command line args
program = sys.argv[1] 

sp = subprocess.Popen([PROGRAMS[program], "other", "arguments", "to", "program"])

*这可能不完全这样的工作,但你的想法

*This might not work exactly like this, but you get the idea

这篇关于我可以使用别名来从一个Python脚本执行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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