0

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?

5
  • 1
    What kind of shell are you running this on? Try adding #!/bin/bash on the top of script? Commented Feb 8, 2015 at 22:14
  • Works for me on bash. How are you running it? Commented Feb 8, 2015 at 22:15
  • It is working for me too, but we are copying it from here, please check your file if it has dos ^M and convert new lines to unix format. Commented Feb 8, 2015 at 22:33
  • works for me in bash as well Commented Feb 8, 2015 at 23:56
  • I have the #!/bin/bash at the top, i forgot to include it in this code but its fixed now. Commented Feb 9, 2015 at 22:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.