切换案例:&#34中的混淆错误;语法错误:单词意外(期待" in")"

时间:2015-02-08 22:07:11

标签: linux bash shell

这是我的代码:

echo
echo "WELCOME"
echo "-------------------------------------------------"
while true
do
  echo
  echo "Select A Menu Option"
  echo "1: Ancestry History"
  echo "2: Who is Online"
  echo "3: What Process Any User is Running"
  echo "4: Exit"
  read mOption
  case $mOption in

  1)  echo
      echo "The Ancestry Tree For Current Process is....";
      ps -ef > processes
      grep "ps -ef" processes > grepProcesses
      awk '{print $2, $3}' processes > awkProcesses
      PID=$(awk '{print $2}' grepProcesses)
      echo $PID
      SPID=$(awk '{print $3}' grepProcesses)
      PID=$SPID
      end=0
      while [ $end != 1 ]
      do
        echo " | "
        echo $SPID
        PID=$SPID
        SPID=$(grep ^"$PID " awkProcesses | cut -d' ' -f2)
        if [ "$PID" = "1" ]
          then
          end=1
        fi
      done

      rm processes
      rm grepProcesses
      rm awkProcesses
      ;;

  2)  echo 
      echo "Users Currently Online";
      who | cut -d' ' -f1
      ;;

  3)  echo
      echo "Select a Currently Online User to View their Processes:"
      index=0
      who | while read onlineUser
            do 
              echo "$index-$onlineUser" who|cut -d' ' -f1>>userList
              index=$((index+1))
            done
            awk '{ print $0 }' userList
            read choice
            if [ $choice ]
            then
              echo
              echo ***Process Currently Running***
              person=$(grep ^$choice userList |cut -d'-' -f2)
          ps -ef >> process
          grep $person process
        else
          echo You have made a mistake. Please start over.
        fi
        rm userList
        rm process
        ;;

  4)  echo
      echo "Exiting..."
      exit 1;;

  *)  
      echo
      echo "Invalid Input. Try Again."
      ;;
  esac

done

每次我运行它时我都会不断收到语法错误" hmwk1.sh:17:hmwk1.sh:语法错误:单词意外(期待" in")" 我查找了不同的语法示例,看起来我的代码看起来是正确的。但是我的反复试验让我无法修复我的代码。

我可以错过某种括号或引号吗?

1 个答案:

答案 0 :(得分:1)

如果您只是按提示上的回车

 read mOption
 case $mOption in

然后$ mOption是一个空令牌,使shell看到

case in

这是一个语法错误。如果你添加

test -z "$mOption" && continue
在中间,如果会修复那个问题。