语法错误:操作数预期(错误标记为“:prev”)

时间:2017-07-24 07:21:01

标签: bash

我无法尝试让我的bash代码正常工作。我使用macosx yosemite作为我的操作系统。我在这里复制了代码给我带来了麻烦。我认为他们是while的一个问题。

这是我的代码:

# i sorry for length code
while [[ "$house" -ne "1" && "$house" -ne "2" && "$house" -ne "3" && "$house" -ne "4" && "$house" -ne "5" && "$house" -ne "6" && "$house" -ne "7" && "$house" -ne "8" && "$house" != ':prev' && "$house" != ':preV' && "$house" != ':prEv' && "$house" != ':prEV' && "$house" != ':pRev' && "$house" != ':pReV' && "$house" != ':pREv' && "$house" != ':pREV' && "$house" != ':Prev' && "$house" != ':PreV' && "$house" != ':PrEv' && "$house" != ':PrEV' && "$house" != ':PRev' && "$house" != ':PReV' && "$house" != ':PREv' && "$house" != ':PREV' ]];do
    house=
    echo 'what is your colour house?'
    echo
    echo "1-red"
    echo "2-blue"
    echo "3-grey"
    echo "4-cyan"
    echo "5-olive"
    echo "6-beige"
    echo "7-mustard"
    echo "8-green"
    read -p " " house
done

# ":prev" should set 'house=', and set 'type=', which is before to go back
# "type" below is my previous menu. its very much of similarity
# "continue" below is for the continuation of my "while" loop
# i must use "else if" instead of elif due to limitations of yosemite, i believe. i switch to elif earlier and had a negative response
if [[ "$house" == ':prev' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':preV' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':prEv' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':prEV' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':pRev' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':pReV' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':pREv' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':pREV' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':Prev' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':PreV' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':PrEv' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':PrEV' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':PRev' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':PReV' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':PREv' ]];
then 
    type=;
    house=;
    continue;
else if [[ "$house" == ':PREV' ]];
then 
    type=;
    house=;
    continue;
fi;fi;fi;fi;fi;fi;fi;fi;fi;fi;fi;fi;fi;fi;fi;fi

如果我输入了syntax error: operand expected (error token is “:prev”)或其他上一个,我会得到mutate { gsub => [ "votingPercentage", "[]g]", " " ] } 。我也输入所有大写字母,这样用户就不会意外地搞乱,可以使用小字母或大字母;因为没有不区分大小写的选项。

我真的很感谢你的帮助!!

2 个答案:

答案 0 :(得分:0)

您可以稍微改变一下,以使其更具可读性和稳健性。

这也应解决您的问题:

while [[ 1 ]]
  do
      cat -n color.list
      echo " "
      read -p "Please choose the color of your house, q to quit: " nr

      case ${nr,,*} in
          [1-8])
              house=$(sed -n "${nr}"p "color.list")
              echo "Your house is $house"
              # TODO: Do something with selected color
              break
              ;;
          prev)
              # Break and go back to your privious menue
              echo "Go back to previous menue"
              # TODO: Do something to go back to your previous menue
              break
              ;;
          *)
              echo "Invalid choice!"
              ;;
      esac
  done

color.list

red
blue
grey
cyan
olive
beige
mustard
green

<强>解释

    如果选择了有效选项,
  • while [[ 1 ]]会运行菜单

  • read -p "Please choose the color of your house, q to quit: " nr 将您的选择输入$ nr

  • case ${nr,,*} in将变量转换为小写,因此它匹配prev,PRev,..

  • cat -n color.list输出带有前缀数字的文件color.list

  • house=$(sed -n "${nr}"p "color.list")从中读取所选的号码 将文件放入变量$ house

  • [1-8])匹配1-8

  • 中的每个数字
  • prev)匹配prev,PREV,prEV等(如果与case ${nr,,*} in一起使用)

  • 如果输入无效,则
  • *) - &gt;重复菜单

答案 1 :(得分:-1)

考虑这个有效的bash代码

case ${house,,} in
    ([1-7])
        echo $house;;
    (':prev')
        echo 'PREV!';;
esac

$ {variable ,,} 扩展为$ variable

的小写值

[1-7] 扩展至1234567。