linux - 开关盒 : Confusing error in "Syntax error: word unexpected (expecting "in")"

标签 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: Syntax error: word unexpected (expecting "in")” 我查找了不同的语法示例,看来我的代码看起来是正确的。但是我的反复试验让我无处修复我的代码。

我会不会遗漏了某种括号或引号?

最佳答案

如果你只是在提示符下按回车键

 read mOption
 case $mOption in

然后 $mOption 是一个空标记,使 shell 只看到

case in

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

test -z "$mOption" && continue

在中间,如果会修复那个问题。

关于linux - 开关盒 : Confusing error in "Syntax error: word unexpected (expecting "in")",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28399928/

相关文章:

linux - 如何添加到 bash 脚本帮助选项 "yourscript --help"

linux - bash 脚本中执行 Postgresql 命令

linux - 尝试查找其中包含特殊字符的文件

node.js - 升级到 Nodejs 8 和 "error: failed to commit transaction"

c - 如何在线程中等待信号?

linux - bash 和 awk 脚本以 YYYY-MM-DD 格式获取上个月的日期

linux - Bash 脚本没有产生预期的结果

node.js - Git Bash 找不到 node.js,即使它在我的路径中并且可以在 Powershell 中运行

json - 修改JSON jq中的键值数组

linux - 重启后自动直接运行一个shell命令并保持运行