如何从 Python 脚本返回一个值作为 Bash 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20031505/
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
How to return a value from Python script as a Bash variable?
提问by ederollora
This is a summary of my code:
这是我的代码摘要:
# import whatever
def createFolder():
#someCode
var1=Gdrive.createFolder(name)
return var1
def main():
#someCode
var2=createFolder()
return var2
if __name__ == "__main__":
print main()
One way in which I managed to return a value to a bash variable was printing what was returned from main(). Another way is just printing the variable in any place of the script.
我设法将值返回给 bash 变量的一种方法是打印从main()返回的内容。另一种方法是在脚本的任何位置打印变量。
Is there any way to return it in a more pythonicway?
有没有办法以更pythonic的方式返回它?
The script is called this way:
脚本是这样调用的:
folder=$(python create_folder.py "string_as_arg")
采纳答案by Ian Stapleton Cordasco
If you were working in bash then you could simply do:
如果您在 bash 中工作,那么您可以简单地执行以下操作:
export var="value"
However, there is no such equivalent in Python. If you try to use os.environ
those values will persist for the rest of the process and will not modify anything after the program finishes. Your best bet is to do exactly what you are already doing.
但是,Python 中没有这样的等价物。如果您尝试使用os.environ
这些值,则这些值将在整个过程中持续存在,并且在程序完成后不会修改任何内容。你最好的办法就是做你已经在做的事情。
回答by cdarke
A more pythonicway would be to avoid bash and write the whole lot in python.
一种更Pythonic 的方法是避免 bash 并在 python 中编写全部内容。
You can't expect bash
to have a pythonic way of getting values from another process - it's way is the bash way.
你不能指望bash
有一种从另一个进程获取值的 Pythonic 方式——它的方式是 bash 方式。
bash and python are running in different processes, and inter-process communication (IPC) must go via kernel. There are many IPC mechanisms, but bash does not support them all (shared memory, for example). The lowest common denominator here is bash, so you must use what bash supports, not what python has (python has everything).
bash 和 python 运行在不同的进程中,进程间通信(IPC)必须通过内核进行。有很多 IPC 机制,但 bash 并不支持所有这些机制(例如共享内存)。这里最小的公分母是 bash,所以你必须使用 bash 支持的东西,而不是 python 的(python 拥有一切)。
Without shared memory, it is not a simple thing to write to variables of another process - let alone another language. Debuggers do it, but they are written specifically for the host language.
没有共享内存,写入另一个进程的变量并不是一件简单的事情——更不用说另一种语言了。调试器会这样做,但它们是专门为宿主语言编写的。
The mechanism you use from bash is to capture the stdout of the child process, so python must print
. Under the covers this uses an anonymous pipe. You could use a named pipe (also known as a fifo) instead, which python would open as a normal file and write
to it. But it wouldn't buy you much.
你从 bash 使用的机制是捕获子进程的 stdout,所以 python 必须print
. 在幕后,这使用匿名管道。您可以改用命名管道(也称为 fifo),python 将作为普通文件打开它并打开write
它。但它不会给你带来太多好处。
回答by Luiz Vieira
You can try to set an environment variable from within the python code and read it outside, at the bash script. This way looks very elegant to me, but it is definitely not the "perfect solution" or the only solution. If you like this approach, this thread might be useful: How to set environment variables in Python
您可以尝试在 python 代码中设置环境变量,然后在 bash 脚本中读取它。这种方式对我来说看起来很优雅,但它绝对不是“完美的解决方案”或唯一的解决方案。如果你喜欢这种方法,这个线程可能有用:How to set environment variables in Python
There are other ways, very similar to what you have done. Check also this thread: store return value of a Python script in a bash script
还有其他方法,与您所做的非常相似。另请检查此线程:将 Python 脚本的返回值存储在 bash 脚本中
回答by ldx
Just use sys.exit()
, i.e.:
只需使用sys.exit()
,即:
import sys
[...]
if __name__ == "__main__":
sys.exit(main())