0

We have a python script that accepts the following arguments as example:

mypie.py --size=20 --pielist='{"apple":[5],"orange":[7]}' --quantity=10

We tried in our test.sh bash shell script:

test.sh 20 5 7 10

test.sh

export SIZE=$1
export APPLE=$2
export ORANGE=$3
export QUANTITY=$4

echo "--size=$SIZE" --pipelist='{\"apple\":[$2],\"orange\":[$3]}\"' --quantity=$4" | tee output.txt
cat output.txt | sudo python mypie.py

We got error ERROR: size is not specified.

but when we cat output.txt, we can see that it is there with size value.

--size=20 --pielist='{"apple":[5],"orange":[7]}' --quantity=10

What are we doing wrong ? Thanks

10
  • 1
    Try: sudo python mypie.py < output.txt Commented Aug 7, 2014 at 5:53
  • hi alfasin, thanks for responding. doesn't work Commented Aug 7, 2014 at 6:08
  • What do you mean "doesn't work" what's the output ? How do you read the args in mypie.py ? Commented Aug 7, 2014 at 6:12
  • hi it says ERROR: size is not specified. Commented Aug 7, 2014 at 6:14
  • @alfasin: the problem is that the OP wants to pass the contents of output.txt as arguments, not as standard input. Commented Aug 7, 2014 at 6:54

4 Answers 4

3

This would do -

cat output.txt | sudo xargs python mypie.py

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, user abc is not allowed to execute '/usr/bin/xargs
The cat is useless, though. sudo xargs python mypie.py <output.txt
If you don't have sudo privileges to run xargs, maybe try xargs sudo python mypie.py <output.txt instead.
Exactly what @tripleee said would work. Check with this and get back.
3

Using the pipe redirects the content to stdin of the program - not the command line.
You could consider building up an args variable - then passing that to both the output.txt, and your python script.
For example:

export SIZE=$1
export APPLE=$2
export ORANGE=$3
export QUANTITY=$4

export ARGS="--size=$SIZE" --pipelist='{\"apple\":[$2],\"orange\":[$3]}\"' --quantity=$4"

echo $ARGS | tee output.txt
sudo python mypie.py $ARGS

4 Comments

Hi thanks for responding - we got error ERROR: 'str' object has no attribute 'has_key'
Potentially that's because the pipelist wasn't correctly escaped .. i.e you need to escape the script & the command line - insert additional \'s. export ARGS="--size=$SIZE" --pipelist='{\\"apple\\":[$2],\\"orange\\":[$3]}\\"' --quantity=$4"
Is it really, really, really necessary to have the parameters in a variable, though? Simply hard-coding the arguments makes the code simpler, more readable, and more maintainable. See also mywiki.wooledge.org/BashFAQ/050
tee thats new for me. That simplies a lot of my prev code's. Thanks for tee.
1

You can use command substitution to directly place the output of command as the arguments to your python program.

python mypie.py `cat output.txt`

or

python mypie.py $(cat output.txt)

2 Comments

Hi Dunes thanks for responding…It says ERROR: 'str' object has no attribute 'has_key'. Is our escaping single quotes correct - output.txt shows correct argument list
Python does not automatically evaluate expression on the command line, it just passes the raw string. To turn the string into a dict you need to use eval, which has security implications, or the more secure loads in the json module.
1

The simple and obvious solution is to change test.sh so that it passes the parameters correctly.

#!/bin/sh
set -x  # if you want to see the script's parameters
sudo python mypie.py --size="$1" \
            --pipelist="{'apple':[$2],'orange':[$3]}" --quantity="$4"

Passing the arguments as standard input (which is what a pipe does) is simply not how it is usually done, nor a particularly suitable model for this particular type of interaction.

1 Comment

I switched to single quotes around the dictionary keys in pipelist for legibility reasons. If they absolutely have to be double quotes, you need to backslash-escape them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.