从.sh到Python获取变量值

从.sh到Python获取变量值,python,raspberry-pi,Python,Raspberry Pi,我有一个.sh文件,可以在树莓皮上生成一张图片。在这个文件中,我有以下内容: 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 -ge

我有一个.sh文件,可以在树莓皮上生成一张图片。在这个文件中,我有以下内容:

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"  
我在这里尝试的是获取它生成到Python中的
PB_${suffix}.jpg
“returnvalue”值(文件名)。现在我的Python程序有了这一行,它运行上面的.sh文件

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"  

非常感谢您的帮助。

这是因为
subprocess.call
只返回执行脚本()的返回代码。您需要脚本返回的实际输出,因此应该使用,并避免使用
shell=True

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

您可能还想通过
sudo
修改在没有root权限的情况下运行脚本。它似乎不应该使用root权限运行

尝试将Popen构造函数与stdout arg一起使用:

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

另外,这里还有Python文档中关于Popen的更多信息。

像这样的简单操作不应该使用
sudo
。sudo是为运行管理命令而保留的。subprocess.check\u输出解决了这个问题,非常感谢。