pythonbashscriptingreturnmultiple-value

How to return multiple variables from python to bash


I have a bash script that calls a python script. At first I was just returning one variable and that is fine, but now I was told to return two variables and I was wondering if there is a clean and simple way to return more than one variable.

archiveID=$(python glacier_upload.py $archive_file_name $CURRENTVAULT)

Is the call I make from bash

print archive_id['ArchiveId']
archive_id['ArchiveId']

This returns the archive id to the bash script

Normally I know you can use a return statement in python to return multiple variables, but with it just being a script that is the way I found to return a variable. I could make it a function that gets called but even then, how would I receive the multiple variables that I would be passing back?


Solution

  • From your python script, output one variable per line. Then from you bash script, read one variable per line:

    Python

    print "foo bar"
    print 5
    

    Bash

    #! /bin/bash
    
    python main.py | while read line ; do
        echo $line
    done
    

    Final Solution:

    Thanks Guillaume! You gave me a great starting point out the soultion. I am just going to post my solution here for others.

    #! /bin/bash
    
    array=()
    while read line ; do
      array+=($line)
    done < <(python main.py)
    echo ${array[@]}
    

    I found the rest of the solution that I needed here