shell脚本和python脚本如何传参数,从Shell脚本向Python传递参数

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 $var1

My python_code1.py looks like this:

import sys

data = sys.argv[1]

print "In python code"

print data

print type(data)

Now, the output of echo $var1 in my shell script is exactly what I wanted to see:

1 Check _ VERB VB _ 0 ROOT _ _ 2 out _ PRT RP _ 1 prt _ _ 3 this _ DET DT _ 4 det _ _ 4 video _ NOUN NN _ 1 dobj _ _ 5 about _ ADP IN _ 4 prep _ _ 6 Northwest _ NOUN NNP _ 7 nn _ _ 7 Arkansas _ NOUN NNP _ 5 pobj _ _ 8 - _ . , _ 7 punct _ _ 9 https _ X ADD _ 7 appos _ _

But the output of print data in the python code is just 1. i.e. the first letter of the argument.

Why is this happening? I want to pass the entire string to the python code.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值