0

I have shell script, which ouput some text result on terminal on execution.

I was able to execute that shellscipt from python script, but I could not store shell script result into python

My python script:

import subprocess
#result = subprocess.call(['/root/Desktop/karim/software/cherrypicker1.01/cherryP.sh'],shell = True)   This yields result 0
subprocess.call(['/root/Desktop/karim/software/cherrypicker1.01/cherryP.sh'],shell = True)
print "REsult is  : "
print result

cherryP.sh

./cherrypicker.sh input.txt
#for line in $(cat input.txt.responses); do echo "$line" ; done
DONE=false
until $DONE
do
  read line || DONE=true
  echo $line   # this will echo the result which I want to use in python script
done < input.txt.responses

What wrong I am doing, or what is other plausible solution?

UPDATED cherryP.py

import subprocess
result = subprocess.check_output(['/root/Desktop/karim/software/cherrypicker1.01/cherryP.sh'], shell = True)
print result
1
  • 1
    If you want to read the last line of a file in bash, you can use tail -n 1 input.txt.responses Commented Sep 19, 2014 at 8:11

2 Answers 2

5

You can use subprocess.check_output() instead of subprocess.call().

import subprocess
result = subprocess.check_output(['/root/Desktop/karim/software/cherrypicker1.01/cherryP.sh'])
print result
Sign up to request clarification or add additional context in comments.

5 Comments

it gives OSError: [Errno 8] Exec format error error
@user123 : either (preferred) add the line #!/usr/bin/env bash to the top of cherryP.sh, or add shell=True to check_output(). The latter is a potential security risk, see docs.python.org/2/library/…
tried it, once I execute python cherryP.y execution stops, nothing occurs
@user123 : It doesn't print result? Does cherryP.sh have a long execution time? Have you checked to see if it has terminated? what if you run cherryP.sh directly on the command line?
yeah now it gives result. ACtually it does not print the "print message" I kept, while in case if using subprocess.call() it was printing
2

In order to get the result of the external program in a variable, you need to use the subprocess.check_output function:

subprocess.check_output(['/root/Desktop/karim/software/cherrypicker1.01/cherryP.sh'])

The subprocess.call function that you are using returns the exit code of the script it executes. Exit code 0, means that the program finished execution without errors.

You can read more on exit statuses here: http://tldp.org/LDP/abs/html/exit-status.html

5 Comments

it gives OSError: [Errno 8] Exec format error error
I added the udpated code, can you please suggest conflicts?
The code that you have executes fine in my environment, and prints the contents of the text file. What are the contents of the file that you are trying to read?
so your second problem was that the text file is empty?
actually execution takes time to give final result, I have print few message in between(in batch file which subsequent executes few .jar files) getting the final result. In case of using check_output it directly gives final result

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.