简体   繁体   English

如何从Python脚本返回一个值作为Bash变量?

[英]How to return a value from Python script as a Bash variable?

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() . 我设法将值返回到bash变量的一种方法是打印从main()返回的内容。 Another way is just printing the variable in any place of the script . 另一种方法是在脚本的任何位置打印变量

Is there any way to return it in a more pythonic way? 有没有办法以更加pythonic的方式返回它?

The script is called this way: 脚本以这种方式调用:

folder=$(python create_folder.py "string_as_arg")

A more pythonic way 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. bash和python在不同的进程中运行,进程间通信(IPC)必须通过内核。 There are many IPC mechanisms, but bash does not support them all (shared memory, for example). 有许多IPC机制,但bash并不支持所有这些机制(例如共享内存)。 The lowest common denominator here is bash, so you must use what bash supports, not what python has (python has everything). 这里最低的共同点是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 . 你从bash使用的机制是捕获子进程的stdout,所以python必须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. 您可以使用命名管道(也称为fifo),python将作为普通文件打开并write But it wouldn't buy you much. 但它不会给你带来太大的收获。

If you were working in bash then you could simply do: 如果你在bash工作,那么你可以简单地做:

export var="value"

However, there is no such equivalent in Python. 但是,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. 如果您尝试使用os.environ那么这些值将在剩余的进程中持续存在,并且在程序完成后不会修改任何内容。 Your best bet is to do exactly what you are already doing. 你最好的选择就是做你正在做的事情。

You can try to set an environment variable from within the python code and read it outside, at the bash script. 您可以尝试在python代码中设置环境变量,并在bash脚本中将其读取到外部。 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中设置环境变量

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 还要检查此线程: 在bash脚本中存储Python脚本的返回值

Just use sys.exit() , ie: 只需使用sys.exit() ,即:

import sys

[...]

if __name__ == "__main__":
    sys.exit(main())

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

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