7

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")
Abdulrahman Bres
  • 2,603
  • 1
  • 20
  • 39
ederollora
  • 1,165
  • 2
  • 11
  • 29

4 Answers4

3

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.

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • maybe I didn't explain well myself, which can possible hapen due to my lack of English. What I was trying to ask is a way of returning to a bash variable a value generated in a python script. The main function returned a value but I didn't know how to finally return it from the main() call. – ederollora Nov 17 '13 at 18:33
  • That is what I thought you meant. The limitation is with mixing Bash with Python. Why not write the Bash part in the Python script? Bash can't do anything that you can't do in Python. – cdarke Nov 17 '13 at 20:49
  • Yes, to be true I'm more familiar with bash to do some unix basic stuff like list files in directory grep, cut... I'm afraid of doing this basic stuff in python and being worse. What's your opinion? – ederollora Nov 18 '13 at 23:30
  • My opinion is that you should keep at learning python - it might even make your bash code better. We all started somewhere, you can't expect to be an expert overnight. Programming any language needs work and practice - don't give up. – cdarke Nov 19 '13 at 11:38
2

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.

Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
  • Probably as you said, the best is to print main() or print the variable in any place of the code, however I'm still impressed there is no "default" or "predefined" way to return it. – ederollora Nov 17 '13 at 14:53
0

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

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

Community
  • 1
  • 1
Luiz Vieira
  • 570
  • 11
  • 35
  • That does not do what you think it does. Run this: https://gist.github.com/sigmavirus24/7514037 and then at your terminal do "echo $TEST_VARIABLE". They do not persist outside of the current process (or child processes). – Ian Stapleton Cordasco Nov 17 '13 at 14:31
-1

Just use sys.exit(), i.e.:

import sys

[...]

if __name__ == "__main__":
    sys.exit(main())
ldx
  • 3,984
  • 23
  • 28
  • 1
    What if he wants to return a string – thefourtheye Nov 17 '13 at 14:10
  • Oh yeah, right. Not very idiomatic in unix to "return" a string - just print the result of the transformation you did with the input to stdout, and return an exit code to tell the caller if your script succeeded or not (optionally provide some error message on stderr if it failed). – ldx Nov 17 '13 at 14:21
  • This solution didn't work to me, as commented before, I'm returning a string. – ederollora Nov 17 '13 at 14:52