Snippets: Shell: Prompt and Choose Item Interactive

 20th August 2020 at 2:19pm

这个 snippet 描述在 Bash 脚本中如何提供给用户选项,并让用户选择。

PS3="Which distro you're currently using: "
options=("CentOS 7" "RHEL 7 / Oracle Linux 7")
select opt in "${options[@]}"
do
    case $opt in
        "CentOS 7")
            echo "> Installing mariadb client tools..."
            yum -Cy install $SRC_DIR/mariadb/el7/mariadb-*
            break
            ;;
        "RHEL 7 / Oracle Linux 7")
            echo "> Installing mysql client tools..."
            yum -Cy install $SRC_DIR/mysql/el7/mysql-community-{client,common,libs}-*
            break
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done