Pass arguments to Python from shell script


sk

I wrote a small shell script like this:

cd models/syntaxnet
var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh)
echo $var1
python /home/sree/python_code1.py $var1

Mine python_code1.pylooks 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 want 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 in the print datapython code is only 1. That is, the first letter of the parameter.

Why is this so? I want to pass the whole string to python code.

Dinesh Ponka

If there is no space between arguments and arguments, python considers two different arguments.

That's why the print data output in the python code is only 1.

Check the output below.

[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.

Contents of shell script ( a.sh ):

var1="Dinesh Pundkar"
python dsp.py "$var1"

The content of the 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

Related


Pass arguments to Python from shell script

sk I wrote a small shell script like this: cd models/syntaxnet var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var1 Mine python_code1.pylooks like this: import sys data = sys.argv[1] print "In python

Pass arguments to shell script from Python

RRWW I am writing a python program that passes arguments to a shell script. Here is my python code: import subprocess Process=subprocess.Popen('./copyImage.sh %s' %s str(myPic.jpg)) and my "copyImage.sh": #!/bin/sh cp /home/pi/project/$1 /home/pi/project/ne

Pass arguments to Python from shell script

sk I wrote a small shell script like this: cd models/syntaxnet var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var1 Mine python_code1.pylooks like this: import sys data = sys.argv[1] print "In python

Pass arguments to Python from shell script

sk I wrote a small shell script like this: cd models/syntaxnet var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var1 Mine python_code1.pylooks like this: import sys data = sys.argv[1] print "In python

Pass arguments to Python from shell script

sk I wrote a small shell script like this: cd models/syntaxnet var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var1 Mine python_code1.pylooks like this: import sys data = sys.argv[1] print "In python

Pass arguments to a Python script wrapped in a shell script

black website Consider the following Python script script.py : import argparse parser = argparse.ArgumentParser() parser.add_argument('-a', type=int) parser.add_argument('-b', type=int) args = parser.parse_args() print('a + b = {}'.format(args.a + args.b))

Pass arguments with spaces to command from shell script?

Adi Gerber See edit below, thanks I have the following test script ( IMPORTANT: I cannot change this part ): while (($#)); do echo $1 shift done Running the command ./test aaa "bbbb cccc" dddwill give the following output: aaa bbbb cccc ddd This is what

Pass arguments with spaces to command from shell script?

Adi Gerber See edit below, thanks I have the following test script ( IMPORTANT: I cannot change this part ): while (($#)); do echo $1 shift done Running the command ./test aaa "bbbb cccc" dddwill give the following output: aaa bbbb cccc ddd This is what

Pass arguments to shell script

A generation A very simple problem might be but can't get it to work. ...basically, I have an XML file, and internally, for unavoidable reasons, I choose a shell script that allows me to pass a small number of arguments to an external program. Now it works mos

Pass arguments to shell script

Keerthisairam I want to execute a shell script used in a usage()script. I have to provide three parameters in the usage block like this: ./script_name server_name path flag How can I write code blocks? Finally, I need to read all three parameters in one line

Pass arguments to method Python interpreter from shell

Jinzu I am learning how to use Python. I want to learn how to pass parameters to functions. In Java, I know we can use BufferedReader or Scanner to receive user input. How to get user input using Python? I have a function that prints spaces based on user input

Pass arguments to method Python interpreter from shell

Jinzu I am learning how to use Python. I want to learn how to pass parameters to functions. In Java, I know we can use BufferedReader or Scanner to receive user input. How to get user input using Python? I have a function that prints spaces based on user input

Pass arguments to method Python interpreter from shell

Jinzu I am learning how to use Python. I want to learn how to pass parameters to functions. In Java, I know we can use BufferedReader or Scanner to receive user input. How to get user input using Python? I have a function that prints spaces based on user input

Pass python arguments to shell

username In python notebook, I want to pass the temp parameter in the first line to the last line, but not sure how to do this. def grabdata(statefolders, temp, split_by): for folder in statefolders: sub = folder.split('_')[split_by] new_na

Pass arguments to Scrapy Spider from Python script

Ashutosh Saboo Before posting this question, I only mentioned some of the issues I already mentioned (I don't currently have a link to all of them I mentioned before posting this question) -: Question 1 Question 2 I can run this code entirely if I pass no para

Pass arguments to python from bash script

Cox I've been working in a mix of bashand and pythonscripting . A bash script can receive an unknown count input parameter. E.g: tinify.sh test1.jpg test2.jpg test3.jpg ..... After bash has received everything, it will pass these arguments to tinify.py. Now,

How to pass arguments from xargs to python script?

Groom I have command.lista command parameter file for my python script and my_script.pyit has 3 parameters. One of the lines looks like: <path1> <path2> -sc 4 Looks like it can't work like this, because the parameters should be split? cat command.list | xargs

Pass arguments to x executable from python script

fish tail I'm trying to call an executable (application/x executable) file with arguments from a Python script. I 've seen similar questions here , but I still can't get it to work. When calling this file through the terminal, I simply use the following form:

Pass arguments to Scrapy Spider from Python script

Ashutosh Saboo Before posting this question, I only mentioned some of the issues I already mentioned (I don't currently have a link to all of them I mentioned before posting this question) -: Question 1 Question 2 I can run this code entirely if I pass no para

How to pass arguments from xargs to python script?

Groom I have command.lista command parameter file for my python script and my_script.pyit has 3 parameters. One of the lines looks like: <path1> <path2> -sc 4 Looks like it can't work like this, because the parameters should be split? cat command.list | xargs

Pass arguments to python from bash script

Cox I've been working in a mix of bashand and pythonscripting . A bash script can receive an unknown count input parameter. E.g: tinify.sh test1.jpg test2.jpg test3.jpg ..... After bash has received everything, it will pass these arguments to tinify.py. Now,

Pass arguments to python from bash script

Cox I've been working in a mix of bashand and pythonscripting . A bash script can receive an unknown count input parameter. E.g: tinify.sh test1.jpg test2.jpg test3.jpg ..... After bash has received everything, it will pass these arguments to tinify.py. Now,

Pass arguments to python script from command line

username I'm really new to python and pandas, I'm trying to execute a python script with arguments from the command line, but I'm getting an error, here is my script #!/usr/bin/python import sys, pandas as pd df1 = pd.read_table(sys.argv[0], sep="\t", head

Pass arguments to python from bash script

Cox I've been working in a mix of bashand and pythonscripting . A bash script can receive an unknown count input parameter. E.g: tinify.sh test1.jpg test2.jpg test3.jpg ..... After bash has received everything, it will pass these arguments to tinify.py. Now,

Pass arguments to Scrapy Spider from Python script

Ashutosh Saboo Before posting this question, I only mentioned some of the issues I already mentioned (I don't currently have a link to all of them I mentioned before posting this question) -: Question 1 Question 2 I can run this code entirely if I pass no para

Pass arguments to python from bash script

Cox I've been working in a mix of bashand and pythonscripting . A bash script can receive an unknown count input parameter. E.g: tinify.sh test1.jpg test2.jpg test3.jpg ..... After bash has received everything, it will pass these arguments to tinify.py. Now,