shell script taking input from python program

2019-08-04 15:10发布

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条回答
别忘想泡老子
2楼-- · 2019-08-04 15:35

Use this

key=`python blah.py var1 var2`
./run $key
查看更多
劳资没心,怎么记你
3楼-- · 2019-08-04 15:41

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])
查看更多
【Aperson】
4楼-- · 2019-08-04 15:46

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

$ key=`python -c 'print "hi"'`
$ echo $key
查看更多
Deceive 欺骗
5楼-- · 2019-08-04 15:47

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

查看更多
登录 后发表回答