vendredi 3 janvier 2020

bash: not reading else statement in if..elif statements

I have the following script trying to create a menu using if..elif..else statements. However, my script never run the else statement in the outer while loop while it works in inner while loop? It should echo "try again!" statement on pressing any keys not mentioned in the menu but it exits from the loop. It should only break the loop and exit on pressing "X" key.

#!/bin/bash

while [[ 1 ]]; do

echo 1: menu1
echo 2: menu2
echo 3: menu3
echo X: exit

echo -n enter: ; read menu

if [[ ${menu} -eq 1 ]]; then
    echo menu1
    echo do something in menu1

elif [[ ${menu} -eq 2 ]]; then
    echo menu2
    echo do something in menu2

elif [[ ${menu} -eq 3 ]]; then
    while [[ 1 ]]; do
        echo 11: second menu1
        echo 12: second menu2
        echo 13: exit

        echo -n enter number: ; read second_menu

        if [[ ${second_menu} -eq 11 ]]; then
            echo do something in second menu1
        elif [[ ${second_menu} -eq 12 ]]; then
            echo do something in second menu2
        elif [[ ${second_menu} -eq 13 ]]; then
            break
        else
        echo try again second_menu!
        fi
    done

elif [[ ${menu} -eq 'x' ]] || [[ ${menu} -eq 'X' ]]; then
    break
else
    echo try again menu!
fi
done

Aucun commentaire:

Enregistrer un commentaire