sid
sid

Reputation: 925

Shell break while-true loop

Have the following shell, which have a (while true) loop inside a (while true) loop. I am trying to break the inner loop using a "break" but it isn't. I want to break the inner loop & display user the options of 1st loop; suggestions pls.

#!/bin/ksh
    sub_menu() {
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~"    
    echo "    S U B - M E N U "
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo "1. Display properties"
    echo "2. Back"
    }

    read_sub_options(){

    echo "Please select option" 
    read option
    case $option in
        1) sub_menu ;;
        ***2) break ;;***
        *) echo "Please insert options 1 ~ 2";;
        esac
    }

showSubMenu(){
    while true
    do
        sub_menu
        read_sub_options
done    
}

read_options(){ 
echo "Please select option "
    read option
    case $option in
        1) showSubMenu ;;
        2) exit 0;;
        *) echo "Please insert options ";;
    esac
}

show_menus() {
    echo "~~~~~~~~~~~~~~~~~~~~~"    
    echo "   M A I N - M E N U "
    echo "~~~~~~~~~~~~~~~~~~~~~"
    echo "1. Sub Menu"
    echo "2. Exit"
}

# -----------------------------------
#           MAIN 
# ------------------------------------
while true
do
    show_menus
    read_options
done

Here is the output:

~~~~~~~~~~~~~~~~~~~~~
   M A I N - M E N U
~~~~~~~~~~~~~~~~~~~~~
1. Sub Menu
2. Exit
Please select option
1
~~~~~~~~~~~~~~~~~~~~~~~~~
    S U B - M E N U
~~~~~~~~~~~~~~~~~~~~~~~~~
1. Display properties
2. Back
Please select option
2
~~~~~~~~~~~~~~~~~~~~~~~~~
    S U B - M E N U
~~~~~~~~~~~~~~~~~~~~~~~~~
1. Display properties
2. Back
Please select option
2

Upvotes: 5

Views: 31144

Answers (3)

jilles
jilles

Reputation: 11212

You are trying to break from a loop outside a function from inside that function. It is not entirely clear to me whether this should work, but I have found that it does not work with ksh93, mksh and the Heirloom Bourne shell, while it works with bash, ash (such as dash and FreeBSD sh) and zsh.

You can return a special status instead and break in the same function as the loop, as in one of the other answers.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

The shell has 'always' supported:

break 2

to break two levels of shell loop. Even the 7th Edition UNIX™ Bourne shell did that.

Upvotes: 1

Appleman1234
Appleman1234

Reputation: 16076

The following script works here.

The changes made were to have read_sub_options return 2 if sub_menu was called and return 1 when Back option was selected and then check the return value of the last run command in showSubMenu and break if Back was selected.

See KSH script basics and Learning the Korn shell for further KSH scripting reference information.

See select and the ksh man page for information on an alternate method of menu selection as mentioned in the comment of your question.

#!/bin/ksh
sub_menu() {
echo "~~~~~~~~~~~~~~~~~~~~~~~~~"    
echo "    S U B - M E N U "
echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "1. Display properties"
echo "2. Back"
return 2;
}

read_sub_options(){

echo "Please select option" 
read option
case $option in
    1) sub_menu ;;
    2) return 1;;
    *) echo "Please insert options 1 ~ 2";;
    esac
}

showSubMenu(){
while true
do
    sub_menu
    read_sub_options
    if [[ $? == 1 ]] ; then
    break
    fi
done    
}

read_options(){ 
echo "Please select option "
    read option
    case $option in
        1) showSubMenu;;
        2) exit 0;;
        *) echo "Please insert options ";;
    esac
}

show_menus() {
    echo "~~~~~~~~~~~~~~~~~~~~~"    
    echo "   M A I N - M E N U "
    echo "~~~~~~~~~~~~~~~~~~~~~"
    echo "1. Sub Menu"
    echo "2. Exit"
}

# -----------------------------------
#           MAIN 
# ------------------------------------
while true
do
    show_menus
    read_options
done

Upvotes: 7

Related Questions