linux Bash:如何改变选择语句选项输入键?

hivapdat  于 2023-08-03  发布在  Linux
关注(0)|答案(1)|浏览(102)

我用两个case语句制作了一个bash脚本,用于创建菜单及其后续子菜单。
我的主菜单结构如下:在此,任何选择都将重定向到子菜单功能。

Main-menu() {
    select INPUT in "Apple" "Banana" "Kiwi" "Coconut" "Pasta" "Exit"
    do
        case $INPUT in
            "Apple") echo "You selected: $INPUT";SubMenu;;
            "Banana") echo "You selected: $INPUT";SubMenu;;
            "Kiwi") echo "You selected: $INPUT";SubMenu;;
            "Coconut") echo "You selected: $INPUT";SubMenu;;
            "Pasta") echo "You selected: $INPUT";SubMenu;;
            "Exit") echo "Goodbye..";exit;;
            *) echo -e "Please enter valid number";;
        esac
    done
}

字符串
我的子菜单结构类似:这里,选择被重定向到函数(FruitType等)。

SubMenu() {
echo "Which Category should this be under? Select from options below: [F,N,D,E]"
    select type in "Fruits" "Nuts" "Dinner" "Exit"
    do
        case $type in
            "Fruits") echo "You selected: $type";FruitType;;
            "Nuts") echo "You selected: $type";NutsType;;
            "Dinner") echo "You selected: $type";DinnerType;;
            "Exit") echo "Goodbye..";exit;;
            *) echo -e "Please enter a valid letter";;
        esac
    done
}

这里的问题是针对我的子菜单。我需要有[F,N,D,E]作为我的输入选项,但它显示数字选项[1,2,3,4]。

我已经看到了OPTARG的一些例子和选项如下:

while getopts u:p: option; do
    case $option in
        u) user=$OPTARG;;
        p) pass=$OPTARG;;
    esac
done


但在这里,我必须输入-u而不是u作为我的输入。也不会显示选项。我需要有子菜单选项显示和字母作为输入。
有什么想法吗

y4ekin9u

y4ekin9u1#

select被硬编码为使用数字,但你可以只使用read -p而不使用它:

while read -r -p $'Which category should this be under?\nSelect from options: [F]ruit, [N]uts, [D]inner, [E]ggs -- or [Q]uit:' type
  case ${type^^} in
    F|FRUIT)  echo "You selected fruit";;
    N|NUTS)   echo "You selected nuts";;
    D|DINNER) echo "You selected dinner";;
    E|EGGS)   echo "You selected eggs";;
    Q|QUIT)   echo "Done with the loop"; break;;
    *)        echo "Invalid selection";;
  esac
done

字符串

相关问题