While-true interrupt wrapper

You have the following shell that has a while (while) loop inside a while (while) loop. I am trying to break the inner loop using "break", but it is not. I want to break the inner loop and display the parameters of the 1st loop to the user; offers 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 result:

~~~~~~~~~~~~~~~~~~~~~
   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
+5
source share
3 answers

This is where the script works.

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

KSH script Korn KSH.

man- ksh , .

#!/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
+7

"":

break 2

. 7- UNIX β„’ Bourne shell .

+1

. , , , ksh93, mksh Birne Heirloom, bash, (, FreeBSD sh) zsh.

, , .

0

All Articles