Return string from Python to Shell script

10,351

Solution 1

That isn't even valid Python code; you are using return outside of a function. You don't wan't return here, just a print statement.

x, y = sys.argv[1:3]
i = sofe_def(x,y)

if i == 0:
    print >>sys.stderr, "ERROR"
elif i == 1:
    print str(some_var1)
else:
    print >>sys.stderr, "OOOps"
    print >>sys.stderr, "Choose between {0} and {1}".format(some_var2, some_var3)
    num = raw_input()
    print num

(Note some other changes:

  1. Write your error messages to standard error, to avoid them being captured as well.
  2. Use raw_input, not input, in Python 2.

)

Then your shell

VAR1="foo"
VAR2="bar"

RES=$(python test.py "$VAR1" "$VAR2")

should work. Unless you have a good reason not to, always quote parameter expansions.

Solution 2

Just use print instead of return - you bash snippet expects result on STDOUT.

Share:
10,351
Андрей Далевский
Author by

Андрей Далевский

Updated on June 05, 2022

Comments

  • Андрей Далевский
    Андрей Далевский almost 2 years

    I have Python code like:

    x = sys.argv[1]
    y = sys.argv[2]
    i = sofe_def(x,y)
    
    if i == 0:
        print "ERROR"
    elif i == 1:
        return str(some_var1)
    else:
        print "OOOps"
        num = input("Chose beetwen {0} and {1}".format(some_var2, some_var3))
        return str(num)
    

    After I must execute this script in shell script and return string in shell variable, like:

    VAR1="foo"
    VAR2="bar"
    RES=$(python test.py $VAR1 $VAR2)
    

    Unfortunately it doesn't work. The way by stderr, stdout and stdin also doesn't work due to a lot of print and input() in code. So how I can resolve my issue? Thank you for answer

    • Charlie Parker
      Charlie Parker about 3 years
      did you try just printing from your python command and setting that to a variable without doing any of the system exiting? e.g. local_dir=$(python execute_tensorboard.py $1)?
  • Андрей Далевский
    Андрей Далевский about 7 years
    Oh, the first of all I've forgotten about my Python script was pulled from def)) Thank you - it all works! But you missed , in print >>sys.stderr "ERROR", print >>sys.stderr, "ERROR" and ` num =raw_input("Choose between {0} and {1}".format(some_var2, some_var3)) also output text in result, I fixed it like: print >>sys.stderr "Choose between {0} and {1}".format(some_var2, some_var3)
  • chepner
    chepner about 7 years
    For some reason, I was remembering raw_input printed its prompt to standard error; I'm probably confusing it with shell read. I was definitely confusing the print statement with Perl's printf STDERR "Ooops\n" (where no comma is allowed because Perl has nightmarish syntax).
  • Charlie Parker
    Charlie Parker about 3 years
    this doesn't seem to work, it's saving the command I want to execute instead of its return value look: (metalearning) brando~ ❯ VAR=%(python /Users/brando/ultimate-utils/ultimate-utils-project/execute_‌​tensorboard.py /home/miranda9/data/logs/logs_Mar06_11-15-02_jobid_0_pid_365‌​7/tb) (metalearning) brando~ ❯ echo $VAR %(python /Users/brando/ultimate-utils/ultimate-utils-project/execute_‌​tensorboard.py /home/miranda9/data/logs/logs_Mar06_11-15-02_jobid_0_pid_365‌​7/tb)
  • Charlie Parker
    Charlie Parker about 3 years
    can you be more precise on how the variable setting works? I tried:(metalearning) brando~ ❯ VAR=python /Users/brando/ultimate-utils/ultimate-utils-project/execute_‌​tensorboard.py /home/miranda9/data/logs/logs_Mar06_11-15-02_jobid_0_pid_365‌​7/tb zsh: permission denied: /Users/brando/ultimate-utils/ultimate-utils-project/execute_‌​tensorboard.py but it doesn't work.
  • chepner
    chepner about 3 years
    @CharlieParker What shell are you using? Command substitution is $(...) in bash, not %(...), though in that case I would expect some other error, not a full assignment to VAR.
  • cenestpamoi
    cenestpamoi over 2 years
    Could we pass more than one string using multiple prints? If yes how should we obtain them in the bash file?