1

I have a shell script which runs a python program which has its own parameters:

#!/bin/bash
python blah.py var1 var2
./run key

I need to find a way to retrieve the contents of key from the python program. I have tried just making the python program return the value, and then doing :

key=python blah.py var1 var2

in my shell script, but that didn't work because it stated key was "outside the function".

Thanks in advance

4 Answers 4

2

Use this

key=`python blah.py var1 var2`
./run $key
Sign up to request clarification or add additional context in comments.

4 Comments

this just sets key equal to python
Really?? Hope you are not using single quote, however refer here if still a problem forums.opensuse.org/english/other-forums/development/…
You're right, but I guess there's something wrong with my logic here. I can't return a variable from that python file, because returning the variable from main states makes it state that "return" is outside the function. Any suggestions?
just print it....using print(blah blah blah)[python>3.0] or print blah blah blah [python<3.0] :)
2

You can do:

./run $( python blah.py var1 var2 )

The $( ) syntax opens a subshell, and runs the command. The stdout output is then dumped in its place

Comments

0

One liner demo:
(ok, so it's two lines)

$ key=`python -c 'print "hi"'`
$ echo $key

Comments

0

Another alternative to modifying the python script to output the value of the key variable is to modify the python script to call the run program

import subprocess
# ... calculations, and set the "key" variable ...
subprocess.call(["./run", key])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.