0

I have an .sh file that produces a picture on Raspberry Pi. and inside this file I have the following:

Config.sh:

#!/bin/bash
suffix=$(date +%H%M%S)  
cd /home/pi/photobooth_images/  
sudo cp image1.jpg /usb/photobooth_images/image-${suffix}-1.jpg  
sudo convert -size 1800x1200 xc:white \  
        image1.jpg -geometry 1536x1152+240+24 -composite \   
    /home/pi/template/logo.png -geometry 192x1152+24+24 -composite \  
        PB_${suffix}.jpg  
sudo cp PB_${suffix}.jpg /usb/photobooth_montage/PB_${suffix}.jpg  
sudo rm /home/pi/photobooth_images/*  
returnvalue=PB_${suffix}.jpg  
echo "$returnvalue"  

What I am trying to do here is get the PB_${suffix}.jpg "returnvalue" value (file name) it generated into Python. Now my Python program has this line, it runs the .sh file above.

Main.py:

return_value = subprocess.call("sudo ./" + config.sh, shell=True)  
print "The Value is: " + str(return_value) + " This value from Python"  

The output I get is this  
[08:33:02 04-10-2016] [PHOTO] Assembling pictures according to 1a template.  
PB_083302.jpg  
The Value is: 0 This value from Python  
The output I am expected should be something like "PB_070638.jpg"  

Any help is greatly appreciated.

martijnn2008
  • 3,552
  • 5
  • 30
  • 40
  • you shouldn't use `sudo` for simple operations like this. sudo is reserved for running administrative commands. – miraculixx Apr 10 '16 at 13:01

2 Answers2

0

That is because subprocess.call only returns the return code of executing the script (documentation). You want the actual output of what your script returns, so you should be using check_output, and avoid using shell=True

subprocess.check_output(["sudo", "./", config.sh])

You also might want to revise running your script without root permission via sudo. It does not seem like something that should run using root privileges.

idjaw
  • 25,487
  • 7
  • 64
  • 83
0

Try using the Popen constructor the with stdout arg:

subprocess.Popen(['"sudo ./" + config.sh'], stdout=subprocess.PIPE)

See also: Get return value from shell command in python

Also, here's more info on Popen in the Python docs. https://docs.python.org/2/library/subprocess.html#popen-constructor

Community
  • 1
  • 1
Anthony E
  • 11,072
  • 2
  • 24
  • 44