Christina Phillips
Christina Phillips

Reputation: 1

Switch case: Confusing error in "Syntax error: word unexpected (expecting "in")"

This is my code:

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

Each time I run it I just keep getting the syntax error "hmwk1.sh: 17: hmwk1.sh: Syntax error: word unexpected (expecting "in")" I looked up different syntax examples and it seems that my code looks correct. But my trial and error gets me nowhere to fixing my code.

Could I be missing a parenthesis or quotation marks of some kind?

Upvotes: 0

Views: 3961

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54495

If you simply press return on the prompt

 read mOption
 case $mOption in

then $mOption is an empty token, making the shell see just

case in

which is a syntax error. If you add

test -z "$mOption" && continue

in the middle, if will repair that problem.

Upvotes: 1

Related Questions