3

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

1
  • 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)? Commented Mar 14, 2021 at 15:16

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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)
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).
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_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)
@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.
Could we pass more than one string using multiple prints? If yes how should we obtain them in the bash file?
3

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

1 Comment

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_3657/tb zsh: permission denied: /Users/brando/ultimate-utils/ultimate-utils-project/execute_tensorboard.py but it doesn't work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.