我试图创建一个将在后台持续运行的BASH脚本。这个bash脚本应该从命令rpi-cecd请求结果,如果它包含45:00,做一些事情,如果它包含45:03,那么做其他事情。但是目前,如果我要求不止一个结果,我就无法使脚本工作。
工作代码,但仅要求45:00
/home/rpi-cecd/rpi-cecd | grep -q "45:00" && ok
我试过的代码,但根本没用。
/home/rpi-cecd/rpi-cecd | grep '45' > /dev/null
if [[ $? =~ .*45:00.* ]]
then
echo "OK Button Clicked - HMS"
ok
elif [ $? == *"45:03"* ]
then
echo "Left Button Clicked - Canteen"
left
else
echo "Nothing Clicked?"
fi
修改 我只是试着这个没有运气。
hhmm=$(/home/rpi-cecd/rpi-cecd)
case "$hhmm" in
45[:]00 )
ok
;;
45[:]03 )
left
;;
* )
echo "don't know how to process $hhmm"
;;
esac
答案 0 :(得分:2)
If rpi-cecd
runs continuously without exiting, you can't just capture its output once. You need to continually read from it. You can do that by piping its output to a while read
loop.
/home/rpi-cecd/rpi-cecd | while IFS= read -r line; do
case $line in
*45:00*)
ok
;;
*45:03*)
left
;;
*)
echo "don't know how to process $line"
;;
esac
done
答案 1 :(得分:0)
在使用之前,您可能需要清理答案:
payload=
while read line;do
[ "$line" ] && [ -z "${line//*payload=*}" ] && payload=${line#*payload=[}
done < <(/home/rpi-cecd/rpi-cecd)
case ${payload%%]*} in
*45:00 )
echo payload 45oo
;;
*45:03 )
echo payload 45.3
;;
* )
echo "other ("${payload%%]*}")..."
;;
esac