Bash script: always show menu after loop execution

18,224

Solution 1

Make it beautiful and userfriendly ;-)

#!/bin/bash

while :
do
    clear
    cat<<EOF
    ==============================
    Menusystem experiment
    ------------------------------
    Please enter your choice:

    Option (1)
    Option (2)
    Option (3)
           (Q)uit
    ------------------------------
EOF
    read -n1 -s
    case "$REPLY" in
    "1")  echo "you chose choice 1" ;;
    "2")  echo "you chose choice 2" ;;
    "3")  echo "you chose choice 3" ;;
    "Q")  exit                      ;;
    "q")  echo "case sensitive!!"   ;; 
     * )  echo "invalid option"     ;;
    esac
    sleep 1
done

Replace the echos in this example with function calls or calls to other scripts.

Solution 2

just modified your script like below. its working for me !

 #!/bin/bash
 while true
 do
 PS3='Please enter your choice: '
 options=("Option 1" "Option 2" "Option 3" "Quit")
 select opt in "${options[@]}" 
 do
     case $opt in
         "Option 1")
             echo "you chose choice 1"
             break
             ;;
         "Option 2")
             echo "you chose choice 2"
             break
             ;;
         "Option 3")
             echo "you chose choice 3"
             break
             ;;
         "Quit")
             echo "Thank You..."                 
             exit
             ;;
         *) echo invalid option;;
     esac
 done
 done
Share:
18,224

Related videos on Youtube

EightBall
Author by

EightBall

Updated on September 19, 2022

Comments

  • EightBall
    EightBall over 1 year

    I'm using a bash script menu like this:

    #!/bin/bash
    PS3='Please enter your choice: '
    options=("Option 1" "Option 2" "Option3" "Quit")
    select opt in "${options[@]}"
    do
        case $opt in
            "Option 1")
                echo "you chose choice 1"
                ;;
            "Option 2")
                echo "you chose choice 2"
                ;;
            "Option 3")
                echo "you chose choice 3"
                ;;
            "Quit")
                break
                ;;
            *) echo invalid option;;
        esac
    done
    

    After each menu selection I just get prompted with

    Please enter your choice:
    

    How do I always show the menu after each option has finished execution? I've done some looking around and I think I can do some kind of while loop, but I haven't been able to get anything working.

    • Mat
      Mat over 10 years
      Show what you tried so we can help you fix it.