Nayan Chaudhari
Nayan Chaudhari

Reputation: 1

How to break from case only and continue other loop

Remember I want to break only case not whole script; which loop is better for this problem?

#!/bin/bash
. /home/nayanc/project/info
. /home/nayanc/project/servers
#. /home/nayanc/project/cal
echo -e "1.System Information \n2.Calculation \n3.Server Configuration \n4.Exit"
read -p "Enter Your Choice " ch
case $ch in
1 )
        echo -e "a.Basic Information \nb.Intermedite Information \nc.All Information \nd.Exit from case \nExit from program"
        read -p "Enter Your Choice" ch1
        case $ch1 in
        a )
                basic
                ;;
        b )
                inter
                ;;
        c )
                myinfo1
                ;;
        esac
        ;;
2 )
        echo -e "a.Addition \nb.Subtraction \nc.multiplication \nd.Exit from case \ne.Exit from program"
        read -p "Enter your Choice " ch2
        case $ch2 in
        a )
                add_numbers
                ;;
        b )
                sub_numbers
                ;;
        c )
                mul_numbers
                ;;
        d )
                echo "Exit from Loop"
                break 1
                ;;
        e )
                echo "Exit from program"
                ;;
        * )
                echo "Please enter correct choice"
                ;;
        esac
        ;;
3 )
        ;;
4 )
        exit 0
        ;;
* )
        ;;
esac

Upvotes: 0

Views: 4114

Answers (1)

George Vasiliou
George Vasiliou

Reputation: 6345

break is used only in for, select, while and until loops, but not in case. But it seems you are a bit confused. By default the use of ;; in case terminates the case and not the program.

The question is not how to terminate case but how to keep it running until we want to terminate it.

See this example:

echo -e "a.Basic Information \nb.Intermedite Information \nc.All Information \nd.Exit from case \ne.Exit from program"
read -p "Enter Your Choice" ch1
case $ch1 in
    a ) echo "-->run basic information script";;
    b ) echo "-->run intermediate information script";;
    c ) echo "-->run allinformation script";;
    d ) echo "-->Exit from case";;
    e ) echo "-->Exit from program"
        exit;;
    * ) echo "Wrong Selection - Try Again"
esac
echo "Command X: This is command X after the case" #Command inside program but out of case

If you press either a,b,c or d then the corresponding command inside case will be executed, case will terminate and the command X outside case will also be executed since program is not terminated.
But if you press e , command exit inside case will be executed, programm will be terminated and thus last command X (outside case) will not be executed.

Output in case of selecting a,b,c, or d

root@debi64:/home/gv/Desktop/PythonTests# ./bashtest.sh
a.Basic Information 
b.Intermedite Information 
c.All Information 
d.Exit from case 
e.Exit from program
Enter Your Choice  : a
-->run basic information script
Command X : This is command X after the case
root@debi64:/home/gv/Desktop/PythonTests#

#If you select e, the last command X is not executed since program is  terminated due to exit.

If you want to keep the case alive until your selection (pressing d) then you can use a while loop before the case:

while [ "$ch1" != "d" ];do
    echo -e "a.Basic Information \nb.Intermedite Information \nc.All Information \nd.Exit from case \ne.Exit from program"
    read -p "Enter Your Choice  : " ch1
    case $ch1 in
        a ) echo "-->run basic information script";;
        b ) echo "-->run intermediate information script";;
        c ) echo "-->run allinformation script";;
        d ) echo "-->Exit from case";;
        e ) echo "-->Exit from program"
            exit;;
        * ) echo "Wrong Selection - Try Again"
    esac
done #if d or e are not pressed the menu will appear again
echo "Command X: This is command X after the case"

In above case / while combination if you select a, b or c the corresponding case commands will be executed, Command X will not be executed and the case menu will be reloaded.

If you press d case will terminate and Command X will be executed since program is not terminated.

If you press e , case and program will exit and command X will not be executed since program is terminated.

Alternatively you can use select & case, which is similar to while & case:

IFS=$'\n' #necessary since select by default separates option using space as a delimiter
op=( "Basic Information" "Intermedite Information" "All Information" "Exit from case" "Exit from program" ) #Use an array
select ch1 in ${op[@]}; do
echo "You choose $ch1"
    case $ch1 in                                                                     #or case $ch1 in
            "Basic Information" )       echo "-->run basic information script";;        #${op[0} )....;;
            "Intermedite Information" ) echo "-->run intermediate information script";; #${op[1} )....;;
            "All Information" )         echo "-->run allinformation script";;           #${op[2} )....;;
            "Exit from case" )          echo "-->Exit from case"                        #${op[3} )....;;
                                        break;;
            "Exit from program" )       echo "-->Exit from program"                     #${op[4} )....;;
                                        exit;;
            * )                         echo "Wrong Selection - Try Again"
    esac                                                                                #esac
done
echo "Command X: This is command X after the menu"

Again if you press "Exit from case" , case and select will be terminated and Command X after the select will be executed since program is not terminated.

Upvotes: 1

Related Questions