如果我有多个else语句,则会出现Bash错误

时间:2016-10-27 16:57:22

标签: bash bluetooth beacon

所以我一直在教这个项目的bash脚本。当公共汽车驶过某个位置时,我正在使用RadBeacon蓝牙信标和覆盆子pi来自动化总线音频。这是我目前的代码:

    while :
do
    sudo beacon scan -A -b -d 4| /home/pi/Desktop/test3.sh
    sleep 1
done

设置扫描参数并运行第二部分,读取扫描并执行我需要的内容。

#!/bin/bash 
while read line
do
    if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA5 0 1"* ]]; then
      mplayer /home/pi/Desktop/01intro.mp3 else
    if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 0 1"* ]]; then
      mplayer /home/pi/Desktop/02togravesite.mp3 else
    if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA7 0 1"* ]]; then
      mplayer /home/pi/Desktop/03gravesite.mp3 else
    if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA8 0 1"* ]]; then
      mplayer /home/pi/Desktop/04visitorcenter.mp3 else
    if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA9 0 1"* ]]; then
      mplayer /home/pi/Desktop/05conclusion.mp3   
    else
    sudo beacon scan -A -b -d 4| /home/pi/Desktop/test3.sh
fi
done

如果我只是做一个然后声明代码运行没有问题但是当我去多个它告诉我在“完成”附近有一个意外的标记。我一直在研究这个问题并尝试不同的方法,但无济于事。有谁知道我的问题?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用elif关键字:

while read line
do
    if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA5 0 1"* ]]; then
      mplayer /home/pi/Desktop/01intro.mp3
    elif [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 0 1"* ]]; then
      mplayer /home/pi/Desktop/02togravesite.mp3
    elif [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA7 0 1"* ]]; then
      mplayer /home/pi/Desktop/03gravesite.mp3
    elif [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA8 0 1"* ]]; then
      mplayer /home/pi/Desktop/04visitorcenter.mp3
    elif [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA9 0 1"* ]]; then
      mplayer /home/pi/Desktop/05conclusion.mp3   
    else
     sudo beacon scan -A -b -d 4| /home/pi/Desktop/test3.sh
    fi
done

或者更好的是,使用case声明:

while read line
do
    case $line of
    "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA5 0 1"*) f=01intro.mp3 ;;
    "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 0 1"*) f=02togravesite.mp3 ;;
    "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA7 0 1"*) f=03gravesite.mp3 ;;
    "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA8 0 1"*) f=04visitorcenter.mp3 ;;
    "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA9 0 1"*) f=05conclusion.mp3 ;;
    esac

    if [[ $f ]]; then
      mplayer /home/pi/Desktop/"$f"
    else
      sudo beacon scan -A -b -d 4| /home/pi/Desktop/test3.sh ;;
    fi
done

否则,您有大量单独的if语句,每个else一个语句,每个语句都需要fi终止。此外,如果else不在下一行,则每个;前面都需要while read line do if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA5 0 1"* ]]; then mplayer /home/pi/Desktop/01intro.mp3; else if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 0 1"* ]]; then mplayer /home/pi/Desktop/02togravesite.mp3; else if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA7 0 1"* ]]; then mplayer /home/pi/Desktop/03gravesite.mp3; else if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA8 0 1"* ]]; then mplayer /home/pi/Desktop/04visitorcenter.mp3; else if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA9 0 1"* ]]; then mplayer /home/pi/Desktop/05conclusion.mp3 else sudo beacon scan -A -b -d 4| /home/pi/Desktop/test3.sh fi; fi; fi; fi; fi done

if

(同样有不同的缩进以强调单独的while read line do if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA5 0 1"* ]]; then mplayer /home/pi/Desktop/01intro.mp3 else if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 0 1"* ]]; then mplayer /home/pi/Desktop/02togravesite.mp3 else if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA7 0 1"* ]]; then mplayer /home/pi/Desktop/03gravesite.mp3 else if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA8 0 1"* ]]; then mplayer /home/pi/Desktop/04visitorcenter.mp3 else if [[ $line = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA9 0 1"* ]]; then mplayer /home/pi/Desktop/05conclusion.mp3 else sudo beacon scan -A -b -d 4| /home/pi/Desktop/test3.sh fi fi fi fi fi done s:

Sales.find({
         "email":req.body.email, 
         "createAt": {
         "$gte": firstDayOfMonth(month),
         "$lte": lastDayOfMonth(month)
         },
         "sort":{
            createAt: -1 //Sort by Date Added DESC
         }
        }, function(err,response){
            callback(err,response);
        });