繁体   English   中英

如何在从Raspberry Pi python脚本调用的3个bash脚本之间传递变量

[英]How to pass variable between 3 bash scripts which are called from Raspberry pi python script

我在Raspberry pi 3上运行python脚本,该脚本对iMac(Yosemite)上的bash脚本进行了3次调用。

pi的python脚本具有3个调用的以下代码(请注意,这是一个简单的示例... python脚本具有一系列IF语句,这些语句确定进行了哪些调用以及发送了“ phrase”参数... I为了避免我将这3个通话分组的建议,我们正在对此进行解释:

url = "http://10.0.1.11/cgi-bin/saysomethinghttp9a.sh"
response = urllib2.urlopen(url).read()

url = "http://10.0.1.11/cgi-bin/saysomethinghttp9.sh?phrase="
response = urllib2.urlopen(url).read()

url = "http://10.0.1.11/cgi-bin/saysomethinghttp9b.sh"
response = urllib2.urlopen(url).read()

iMac上有3个bash脚本 第一个sayomethinghttp9a.sh )将当前输出音量捕获到变量( parm )中,然后将输出音量设置为14级。

#!/bin/bash
echo -e "Content-type: text/html\n"

cat << junk
<html>
<head>
<title>
saying
</title>
</head>
<body>
junk
#-----------------------
currVol=$(osascript -e "get volume settings")
var1=$( echo $currVol | cut -d":" -f1 )
var2=$( echo $currVol | cut -d":" -f2 )
origVol=$( echo $var2 | cut -d"," -f1 )
parm="set volume output volume $origVol"

export parm

osascript -e "set volume output volume 14"
#-----------------------
cat << junk
</body>
</html>
junk

第二个saysomethinghttp9.sh )从调用的python脚本中获取短语 ,然后使用say命令说出该短语。 (请注意,由于某些原因,使用say命令时的音量会比播放音乐时大得多)

#!/bin/bash
echo -e "Content-type: text/html\n"

PHRASE=`echo "$QUERY_STRING" | sed -n 's/^.*phrase=\([^&]*\).*$/\1/p' | sed "s/+/ /g"$

cat << junk
<html>
<head>
<title>
saying
</title>
</head>
<body>
junk
#-----------------------
say $PHRASE
#-----------------------
cat << junk
</body>
</html>
junk

第三个 脚本saidomethinghttp9b.sh )试图使用第一个脚本中捕获的变量parm来将输出音量级别重置为原始音量。

#!/bin/bash
echo -e "Content-type: text/html\n"

cat << junk
<html>
<head>
<title>
saying
</title>
</head>
<body>
junk
#-----------------------
osascript -e "$parm"
#-----------------------
cat << junk
</body>
</html>
junk

我的问题是我试图将在第一个脚本中创建的变量parm传递给第三个脚本。 从代码中可以看到,我尝试在第一个脚本中“导出”变量,但在第三个脚本中看不到它!

在多个脚本中传递变量的简单方法是将变量存储在临时文件中,其他任何脚本都可以读取该文件。 例如:

echo "$parm" > /tmp/ParmHolder

然后从中读取是很容易的:

cat /tmp/ParmHolder

在许多系统中,每次启动时都会“清除”目录/ tmp /,这在这种情况下是一件好事-我建议将临时文件保存在这样的目录中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM