简体   繁体   English

如何从python调用Shell脚本函数/变量?

[英]How to call a shell script function/variable from python?

Is there any way to call a shell script and use the functions/variable defined in the script from python? 有什么方法可以调用shell脚本并使用python中脚本中定义的函数/变量吗?

The script is unix_shell.sh 脚本是unix_shell.sh

#!/bin/bash
function foo
{
...
}

Is it possible to call this function foo from python? 是否可以从python调用此函数foo?

Solution: 解:

  1. For functions: Convert Shell functions to python functions 对于函数:将Shell函数转换为python函数
  2. For shell local variables(non-exported), run this command in shell, just before calling python script: 对于shell局部变量(非导出),在调用python脚本之前,在shell中运行以下命令:
    export $(set | tr '\\n' ' ') 导出$(set | tr'\\ n''')

  3. For shell global variables(exported from shell), in python, you can: import os print os.environ["VAR1"] 对于shell全局变量(从shell导出),在python中,您可以:import os print os.environ [“ VAR1”]

Yes, in a similar way to how you would call it from another bash script: 是的,类似于从另一个bash脚本调用它的方式:

import subprocess
subprocess.check_output(['bash', '-c', 'source unix_shell.sh && foo'])

No, that's not possible. 不,那不可能。 You can execute a shell script, pass parameters on the command line, and it could print data out, which you could parse from Python. 您可以执行 Shell脚本,在命令行上传递参数,并且可以打印出数据,然后可以从Python进行解析。

But that's not really calling the function. 但这并不是真正的调用函数。 That's still executing bash with options and getting a string back on stdio. 仍然使用选项执行bash并在stdio上获取字符串。

That might do what you want. 那可能会做您想要的。 But it's probably not the right way to do it. 但这可能不是正确的方法。 Bash can not do that many things that Python can not. Bash无法做到Python无法做到的许多事情。 Implement the function in Python instead. 而是在Python中实现该功能。

This can be done with subprocess. 这可以通过子过程来完成。 (At least this was what I was trying to do when I searched for this) (至少这是我在搜索时试图做的事情)

Like so: 像这样:

output = subprocess.check_output(['bash', '-c', 'source utility_functions.sh; get_new_value 5'])

where utility_functions.sh looks like this: 其中utility_functions.sh如下所示:

#!/bin/bash
function get_new_value
{
    let "new_value=$1 * $1"
    echo $new_value
}

Here's how it looks in action... 这是实际的样子...

>>> import subprocess
>>> output = subprocess.check_output(['bash', '-c', 'source utility_functions.sh; get_new_value 5'])
>>> print(output)
b'25\n'

I do not know to much about python, but if You use export -f foo after the shell script function definition, then if You start a sub bash, the function could be called. 我对python不太了解,但是如果您在shell脚本函数定义之后使用export -f foo ,那么如果您启动sub bash,则可以调用该函数。 Without export , You need to run the shell script as . script.sh 如果不export ,则需要将shell脚本运行为. script.sh . script.sh inside the sub bash started in python, but it will run everything in it and will define all the functions and all variables. sub bash内的. script.sh是从python开始的,但是它将运行其中的所有内容并定义所有函数和所有变量。

You could separate each function into their own bash file. 您可以将每个函数分成各自的bash文件。 Then use Python to pass the right parameters to each separate bash file. 然后使用Python将正确的参数传递给每个单独的bash文件。

This may be easier than just re-writing the bash functions in Python yourself. 这可能比仅用Python重写bash函数要容易。

You can then call these functions using 然后,您可以使用调用这些函数

import subprocess
subprocess.call(['bash', 'function1.sh'])
subprocess.call(['bash', 'function2.sh'])
# etc. etc.

You can use subprocess to pass parameters too. 您也可以使用子过程来传递参数。

With the help of above answer and this answer , I come up with this: 上述答案答案的帮助下,我想到了:

import subprocess
command = 'bash -c "source ~/.fileContainingTheFunction && theFunction"'
stdout = subprocess.getoutput(command)
print(stdout)

I'm using Python 3.6.5 in Ubuntu 18.04 LTS. 我在Ubuntu 18.04 LTS中使用Python 3.6.5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM