Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(). 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?

The script is called this way:

folder=$(python create_folder.py "string_as_arg")
like image 217
ederollora Avatar asked Nov 17 '13 13:11

ederollora


People also ask

How do you return a value from a Python script?

The Python return statement is a special statement that you can use inside a function or method to send the function's result back to the caller. A return statement consists of the return keyword followed by an optional return value. The return value of a Python function can be any Python object.

What is %% bash in Python?

GNU Bash or simply Bash is a Unix shell and command language. %%bash. Means, that the following code will be executed by bash. In bash $() means it will return with the result of the commands inside the parentheses, in this case the commands are: gcloud config list project --format "value(core.project)"

Can a bash script return a value?

Unlike functions in “real” programming languages, Bash functions don't allow you to return a value when called. When a bash function completes, its return value is the status of the last statement executed in the function, 0 for success and non-zero decimal number in the 1 - 255 range for failure.


2 Answers

A more pythonic way would be to avoid bash and write the whole lot in 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 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).

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.

like image 83
cdarke Avatar answered Oct 20 '22 22:10

cdarke


If you were working in bash then you could simply do:

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.

like image 22
Ian Stapleton Cordasco Avatar answered Oct 20 '22 20:10

Ian Stapleton Cordasco