Need a help to move control to another line in my script

I have menu option in my script and in that i have 4 options like below :-

echo "Please select a target server .."
         echo "[1] AIX - AIX1"
         echo "[2] AIX - AIX2"
         echo "[3] AIX - AIX3"
         echo "[4] HP-UX - HP-UX1"
 echo "Enter your menu choice [1-11]: \n"
         read tgtser
                case $tgtser in
                1|2|3|4)
                        echo "Checked out to test server ..." ;;
                *)echo "Opps !!! please select a valid option" ;;

If i gave anything other than 1 to 4, it is exiting now. But I want the control to be moved to first so that it will show the menu option again instead of exiting from the script:b:...

Please help me out

Renjesh

#!/bin/bash

menu()
{
        clear
        echo "Please select a target server .."
        echo "[1] AIX - AIX1"
        echo "[2] AIX - AIX2"
        echo "[3] AIX - AIX3"
        echo "[4] HP-UX - HP-UX1"

        echo "Enter your menu choice [1-11]:"
}

menu

while read tgtser; do
case $tgtser in
        1|2|3|4)
                echo "Checked out to test server ..."
                break
                ;;
        *)
                echo "Opps !!! please select a valid option"
                sleep 2
                menu
                ;;
esac
done

exit 0
#!/bin/bash

tgtser=""

function menu
{
        clear
                echo "Please select a target server .."
                echo "[1] AIX - AIX1"
                echo "[2] AIX - AIX2"
                echo "[3] AIX - AIX3"
                echo "[4] HP-UX - HP-UX1"
                echo "Enter your menu choice [1-11]: \n"
                read tgtser
}

while true
do
        menu
        case $tgtser in
            1|2|3|4)
               echo "Checked out to test server ..."
                break
                ;;
            *)echo "Opps !!! please select a valid option"
                sleep 2
                menu
                ;;
        esac
done

--ahamed

---------- Post updated at 04:20 AM ---------- Previous update was at 04:19 AM ----------

oh man!!... exactly (~99%) same code... even the sleep timer... :slight_smile:

--ahamed

Thank you for your help ...

I don't want to call another function for doing this ..
Is there something like "goto" command in which i can move the flow of the script ?

@ahamed:
:D, I was thinking to have 3 seconds, but 3 was just too long - 2 was ideal watching the screen to change :slight_smile:

@Renjesh:
Why not use functions? They are very helpful and help structuring your code and also prevent writing same code over and over.

I will show the entire code and then probably you can help me ..

# Ver   Date            Description             Analyst
# ---   ----            -----------             -------
# A01  09/08/11      Original Implementation   Renjesh Raju
#***************************************************************************

function menu
{
    clear
    echo "[1] check-out to test server"
    echo "[2] check-in"
    echo "[3] check-out to local machine"
    echo "[4] Exit/stop"
    echo "----------------------------------------------"
    echo "Enter your menu choice [1-4]: \n"
    read choice
      case $choice in
      1)
         clear
         echo "Please select a target server .."
                echo "[1] AIX - AIX1"
                echo "[2] AIX - AIX2"
                echo "[3] AIX - AIX3"
                echo "[4] HP-UX - HP-UX1"
                echo "Enter your menu choice [1-4]: \n"
                read tgtser                
                case $tgtser in
                1|2|3|4|)
                        echo "Checked out to test server ..." ;;
                *)echo "Opps !!! please select a valid option" goto ;;
          (From here, i need to go to the sub menu again)
                esac
       ;;
      2) echo "Checked in ..." ;;
      3) echo "Checked out to local machine ..." ;;
      4) echo "Exiting ... " ;;
      *) echo "Opps !!! please select a valid option (1/2/3/4)" ;;
(From here, i need to go to the first menu again)
      esac
}
menu

It's a case looping .. Can you help me on this ?

thanks,
Renjesh

Make separate functions for each menu and hand over parameters when calling the function. Example in theory:

  • When you select Checkin, assign a value to some variable like "ACTION=checkin" and call next menue with this parameter like menu2 $ACTION
  • In the second menu function process read the OS as you already do and do an action according to $ACTION, which will be $1 in this menu2().
  • When an actions follow up is to go back to some menu or the same, just call it again as part of a case/esac or if/then/fi.

Thank you for your help ...

#!/bin/bash
PS3="Enter your menu choice [1-11]: "
select choice in "AIX - AIX1"  "AIX - AIX2" "AIX - AIX3" "HP-UX - HP-UX1" 
do 
# consider a debug:
   echo $REPLY 
   echo $choice
   if [ $REPLY -lt 5 ]
   then 
      # your code including case statement goes here
      break; 
   fi; 
done

This only repeats the whole menu if you enter a blank line. Place the break in one of your options if you want it to repeat. The above solutions can be made more sophisticated than this but it is built into bash, ksh and zsh. And EOF (^-D) entered into the prompt finishes the loop too. What does it do with the above solutions? (don't tell me, I can try them for myself if I'm interested).

Andrew