nested case statements in linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lifeisgreat20009
    New Member
    • Oct 2007
    • 70

    nested case statements in linux

    the simple script below is not working after i implemented a case statement within another..plz tell me where the problem lies...

    My script is as follows

    Code:
    #!/bin/bash
    # A menu driven Shell script which has following options
    # Contents of /etc/passwd
    # List of users currently logged
    # Simple Calculator
    # Exit
    # As per option do the job
    while :
    do
     clear
     echo "   M A I N - M E N U"
     echo "1. Contents of /etc/passwd"
     echo "2. List of users currently logged"
     echo "3. Simple Calculator"
     echo "4. Exit"
     echo -n "Please enter option [1 - 4]"
     read opt
     case $opt in
      1) echo "************ Conents of /etc/passwd *************";
         more /etc/passwd;;
      2) echo "*********** List of users currently logged";
         who | more;;
      3) a=$1
    op="$2"
    b=$3
     
    if [ $# -lt 3 ]
    then
    	echo "$0 num1  opr num2"
    	echo "opr can be +, -, / , x"
    	exit 1
    fi
     
    case "$op" in
    	+) echo $(( $a + $b ));;
    	-) echo $(( $a - $b ));;
    	/) echo $(( $a / $b ));;
    	x) echo $(( $a * $b ));;
    	*) echo "Error ";;
    esac
         echo "Press [enter] key to continue. . .";
         read enterKey;;
      4) echo "Bye $USER";
         exit 1;;
      *) echo "$opt is an invaild option. Please select option between 1-4 only";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
    esac
    done
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by lifeisgreat2000 9
    the simple script below is not working after i implemented a case statement within another..plz tell me where the problem lies...

    My script is as follows

    Code:
    #!/bin/bash
    # A menu driven Shell script which has following options
    # Contents of /etc/passwd
    # List of users currently logged
    # Simple Calculator
    # Exit
    # As per option do the job
    while :
    do
     clear
     echo "   M A I N - M E N U"
     echo "1. Contents of /etc/passwd"
     echo "2. List of users currently logged"
     echo "3. Simple Calculator"
     echo "4. Exit"
     echo -n "Please enter option [1 - 4]"
     read opt
     case $opt in
      1) echo "************ Conents of /etc/passwd *************";
         more /etc/passwd;;
      2) echo "*********** List of users currently logged";
         who | more;;
      3) a=$1
    op="$2"
    b=$3
     
    if [ $# -lt 3 ]
    then
    	echo "$0 num1  opr num2"
    	echo "opr can be +, -, / , x"
    	exit 1
    fi
     
    case "$op" in
    	+) echo $(( $a + $b ));;
    	-) echo $(( $a - $b ));;
    	/) echo $(( $a / $b ));;
    	x) echo $(( $a * $b ));;
    	*) echo "Error ";;
    esac
         echo "Press [enter] key to continue. . .";
         read enterKey;;
      4) echo "Bye $USER";
         exit 1;;
      *) echo "$opt is an invaild option. Please select option between 1-4 only";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
    esac
    done
    I think you are a bit confused in all that you are trying to do with this script. First, I have never seen anyone try to nest case statements. Even if they did try though, wouldn't it make sense to nest it inside of one of the options from the previous menu?

    Also, for #3, your simple calculator, it errors out because it expects input, but from where? Also, #2 doesn't even print anything and if it does, its so very fast it cannot be seen. Making the program wait till a key is pressed might be a good idea.

    Regards,

    Jeff

    Comment

    • kaarthikeyapreyan
      New Member
      • Apr 2007
      • 106

      #3
      Trouble finding error?

      Hey where is the error, the script runs fine for me
      I removed the clear to visualize the output of #1 and #2
      and for #3 I ran the program the way it wanted it to be run
      Code:
      ./case.sh 1 + 5
      and i don get an error ?

      Comment

      Working...