6

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.

1
  • 1
    Show what you tried so we can help you fix it. Commented Nov 26, 2013 at 18:24

4 Answers 4

18

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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

Comments

0

You can also do something like:

#!/bin/bash

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"
            ;;
        "Option 2")
            echo "you chose choice 2"
            ;;
        "Option 3")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
    counter=1
    SAVEIFS=$IFS
    IFS=$(echo -en "\n\b")   # we set another delimiter, see also: https://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
    for i in ${options[@]};
    do
        echo $counter')' $i
        let 'counter+=1'
    done
    IFS=$SAVEIFS
done

Comments

0

You can achieve this by set the REPLY variable to be empty after esac.

Example with your code:

#!/usr/bin/env 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
    REPLY=
done

Another example ( simpler version to understand ):

#!/usr/bin/env bash

select animal in dog duck cat bird quit ; do
    case $animal in
        dog)  echo "i'm a dog!" ;;
        duck) echo "i'm a duck!" ;;
        cat)  echo "i'm a cat!" ;;
        bird) echo "i'm a bird!" ;;
        quit) echo "bye" ; break ;;
        *)    echo "unknown animal, please try again..." ;;
    esac
    REPLY=
done

For an explanation, see the GNU bash documentation about conditional constructs - select

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.