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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16046806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:12:39  来源:igfitidea点击:

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

pythonbash

提问by Neerav

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
  2. For shell local variables(non-exported), run this command in shell, just before calling python script:
    export $(set | tr '\n' ' ')

  3. For shell global variables(exported from shell), in python, you can: import os print os.environ["VAR1"]

  1. 对于函数:将 Shell 函数转换为 python 函数
  2. 对于 shell 局部变量(非导出),在 shell 中运行此命令,就在调用 python 脚本之前:
    export $(set | tr '\n' ' ')

  3. 对于 shell 全局变量(从 shell 导出),在 python 中,您可以: import os print os.environ["VAR1"]

采纳答案by Lennart Regebro

No, that's not possible. You can executea 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 callingthe 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. Implement the function in Python instead.

那可能会做你想做的。但这可能不是正确的方法。Bash 不能做很多 Python 做不到的事情。改为在 Python 中实现该函数。

回答by Eric

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'])

回答by ChicagoCyclist

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= * "
    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'

回答by M Imam Pratama

With the help of above answerand 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。

回答by TrueY

I do not know to much about python, but if You use export -f fooafter the shell script function definition, then if You start a sub bash, the function could be called. Without export, You need to run the shell script as . script.shinside the sub bash started in python, but it will run everything in it and will define all the functions and all variables.

我对python不太了解,但是如果你export -f foo在shell脚本函数定义之后使用,那么如果你启动一个子bash,该函数可以被调用。如果没有export,您需要像. script.sh在 python 中启动的 sub bash 中一样运行 shell 脚本,但它将运行其中的所有内容并定义所有函数和所有变量。

回答by Cephlin

You could separate each function into their own bash file. Then use Python to pass the right parameters to each separate bash file.

您可以将每个函数分离到它们自己的 bash 文件中。然后使用 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.

您也可以使用子流程来传递参数。