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:
- Write your error messages to standard error, to avoid them being captured as well.
- Use
raw_input
, notinput
, 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.
Author by
Андрей Далевский
Updated on June 05, 2022Comments
-
Андрей Далевский 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 about 3 yearsdid 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 yearsOh, the first of all I've forgotten about my Python script was pulled from def)) Thank you - it all works! But you missed
,
inprint >>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 about 7 yearsFor some reason, I was remembering
raw_input
printed its prompt to standard error; I'm probably confusing it with shellread
. I was definitely confusing theprint
statement with Perl'sprintf STDERR "Ooops\n"
(where no comma is allowed because Perl has nightmarish syntax). -
Charlie Parker about 3 yearsthis 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_3657/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_3657/tb)
-
Charlie Parker about 3 yearscan 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_3657/tb zsh: permission denied: /Users/brando/ultimate-utils/ultimate-utils-project/execute_tensorboard.py
but it doesn't work. -
chepner about 3 years@CharlieParker What shell are you using? Command substitution is
$(...)
inbash
, not%(...)
, though in that case I would expect some other error, not a full assignment toVAR
. -
cenestpamoi over 2 yearsCould we pass more than one string using multiple prints? If yes how should we obtain them in the bash file?