Passing Arguments To Python From Shell Script
I wrote a small shell script that looks like this: cd models/syntaxnet var1=$(jq --raw-output '.['avl_text']' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var
Solution 1:
If there is space in between argument and argument is not in quotes, then python consider as two different arguments.
That's why the output of print data in the python code is just 1.
Check the below output.
[root@dsp-centos ~]# python dsp.py Dinesh Pundkar
In python code
Dinesh
[root@dsp-centos ~]# python dsp.py "Dinesh Pundkar"
In python code
Dinesh Pundkar
[root@dsp-centos ~]#
So, in your shell script, put $var1 in quotes.
Content of shell script(a.sh):
var1="Dinesh Pundkar"
python dsp.py "$var1"
Content of python code(dsp.py):
import sys
data = sys.argv[1]
print"In python code"print data
Output:
[root@dsp-centos ~]# sh a.sh
In python code
Dinesh Pundkar
Solution 2:
Use Join and list slicing
import sys
data = ' '.join(sys.argv[1:])
print"In python code"print data
printtype(data)
Post a Comment for "Passing Arguments To Python From Shell Script"