从 bash case 语句中获取通配符值
Get wildcard value from bash case statement
我想知道如何从大小写通配符中获取值?我有一个为我生成菜单的数组。然后我有一个案例可以确定选择哪个选项。 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
在上面,"Invalid input" 值始终为空。我可以输入 "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}"
;;
我想知道如何从大小写通配符中获取值?我有一个为我生成菜单的数组。然后我有一个案例可以确定选择哪个选项。 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
在上面,"Invalid input" 值始终为空。我可以输入 "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}"
;;