gpt4 book ai didi

bash - 从 bash case 语句中获取通配符值

转载 作者:行者123 更新时间:2023-11-29 09:46:52 25 4
gpt4 key购买 nike

我想知道如何从大小写通配符中获取值?我有一个为我生成菜单的数组。然后我有一个案例可以确定选择哪个选项。 case 语句的最后一部分是通配符值。我正在寻找用于通配符的值。

这是我使用的代码:

menu=()
menu+=('Option 1')
menu+=('Option 2')
menu+=('Option 3')
menu+=('Option 4')
menu+=('Quit')

echo "What would you like to install?"
echo " "
select opt in "${menu[@]}"
do
case $opt in
'Option 1' )
echo "Doing Option 1"
;;
'Option 2' )
echo "Doing Option 2"
;;
'Option 3' )
echo "Doing Option 3"
;;
'Option 4' )
echo "Doing Option 4"
;;
'Quit' )
echo "Quitting installations"
exit;
;;
* )
echo "Invalid input: ${opt}"
;;
esac
done

在上面,“无效输入”值始终为空。我可以输入“foobar”作为选项,但它没有显示。我还将变量更改为 $opt 但它仍然没有打印出来。

最佳答案

来自 man bash :

select name [ in word ] ; do list ; done

The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the positional parameters are printed (see PARAMETERS below). The PS3 prompt is then displayed and a line read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The list is executed after each selection until a break command is executed. The exit status of select is the exit status of the last command executed in list, or zero if no commands were executed.

所以只要改变你的

    * )
echo "Invalid input: ${opt}"
;;

    * )
echo "Invalid input: ${REPLY}"
;;

关于bash - 从 bash case 语句中获取通配符值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35098423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com