0

I need to execute my python program from shell script so that the print commands in my python program will be exposed for reading from another program. the shell script:

#!/bin/bash

if [ $# -lt 1 ] || [ $# -gt 2 ];then
    echo "Usage: $0 APK <duration in seconds>"
    exit 1;
fi

printf "$(python '/root/Desktop/DroidBox_4.1.1/scripts/droidbox.py' $1 $2 -c 'import sys; exec sys.stdin.read()')"

My python program should get the parametrs $1 and $2 but it doesn't recognize those as parametrs but taking -c and the command after it as the parametrs.

For answers like: getting the process input stream in my other project won't work for me. the only way that seems to be working is to use -c option and the command 'exec sys.stdin.read()'.

thank you.

2
  • What does droidbox.py look like? If you print(sys.argv) in that script, what does it display? Commented May 23, 2016 at 14:02
  • I would not use the python output inside printf directly, instead use printf "%s" "$( python ... Then, what input should it read, the stdin of the shell script? Also, as test, you might assign variables from $1 and $2 and then use the variable names on the call to droidbox.py to avoid someone mangling with the position parameters. Commented May 23, 2016 at 14:09

2 Answers 2

1

It should work pretty well just the way you've written it. In a stripped down version here's what I get:

the (bash) test.sh script:

#!/bin/bash    
python /tmp/test.py $1 $2

the (python) test.py script:

import sys
print "in python"
print sys.argv

and finally a shell session:

smassey@hacklabs:/tmp $ ./test.sh hello world
in python
['/tmp/test.py', 'hello', 'world']

As you can see, the bash script calls the python script which prints values to stdout so they're just as exposed as anything else directed to stdout:

smassey@hacklabs:/tmp $ ./test.sh hello world | grep python | tr 'a-z' 'A-Z'
IN PYTHON
Sign up to request clarification or add additional context in comments.

Comments

0

The argparse module is very helpful as well. This allow you to add custom flags to your program (makes it more convenient to works with from a user's perspective as well).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.