python脚本可以执行bash脚本里面的函数? [英] Can a python script execute a function inside a bash script?
本文介绍了python脚本可以执行bash脚本里面的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个3D一方,它定义了一组函数提供的bash脚本。这里是什么样子的模板
$猫test.sh#!/斌/庆典去定义(){
回声你好
}
我可以做从bash shell中以下叫走():
$源test.sh
$走
你好
有没有办法从一个python脚本访问相同的功能?我尝试以下,但它没有工作:
的Python 2.6.6(r266:84292,2010年9月15日,15时52分39秒)
[GCC 4.4.5] linux2的上
键入help,版权,信用或许可的详细信息。
>>>进口子
>>> subprocess.call(源test.sh)
回溯(最近通话最后一个):
文件<&标准输入GT;,1号线,上述<&模块GT;
文件/usr/lib/python2.6/subprocess.py,线路470,呼叫
返回POPEN(* popenargs,** kwargs).wait()
文件/usr/lib/python2.6/subprocess.py,线路623,在__init__
errread,ERRWRITE)
文件/usr/lib/python2.6/subprocess.py,1141线,在_execute_child
提高child_exception
OSERROR:[错误2]没有这样的文件或目录
>>>
解决方案
是的,间接的影响。鉴于这种 foo.sh
函数围棋(){
回声嗨
}
试试这个:
>>> subprocess.Popen(['庆典','-c','foo.sh;去'])
输出:
喜
I have a bash script provided by a 3d party which defines a set of functions. Here's a template of what that looks like
$ cat test.sh
#!/bin/bash
define go() {
echo "hello"
}
I can do the following from a bash shell to call go():
$ source test.sh
$ go
hello
Is there any way to access the same function from a python script? I tried the following, but it didn't work:
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call("source test.sh")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
>>>
解决方案
Yes, indirectly. Given this foo.sh:
function go() {
echo "hi"
}
Try this:
>>> subprocess.Popen(['bash', '-c', '. foo.sh; go'])
Output:
hi
这篇关于python脚本可以执行bash脚本里面的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文