본문 바로가기

Bash 쉘 스크립트

How can I create a select menu in a shell script?


[hanadmin@search-bfblog-test1 service]$ vi menu.sh

#!/bin/bash

# Bash Menu Script Example


PS3='Please enter your choice: '

options=("Option 1A" "Option 2" "Option 3" "Option 10" "Quit")

select opt in "${options[@]}"

do

    case $opt in

        "Option 1A")

            echo "you chose choice 1A"

            ;;

        "Option 2")

            echo "you chose choice 2"

            ;;

        "Option 3")

            echo "you chose choice 3"

            ;;

        "Option 10")

            echo "you chose choice 10"

            ;;

        "Quit")

            break

            ;;

        *) echo invalid option;;

    esac

done



[hanadmin@search-bfblog-test1 service]$ sh menu.sh

1) Option 1A

2) Option 2

3) Option 3

4) Option 10

5) Quit


Please enter your choice: 1

you chose choice 1A

Please enter your choice: 4

you chose choice 10

Please enter your choice: 2

you chose choice 2

Please enter your choice: Option 2

invalid option

Please enter your choice: 6

invalid option

Please enter your choice: 5    # 종료

[hanadmin@search-bfblog-test1 service]$