使用Bash Select创建菜单

在本教程中,我们将介绍Bashselect构造的基础。select构造允许您生成菜单。

Bashselect构造

select构造从项目列表中生成菜单。 它具有与for循环几乎相同的语法:

select ITEM in [LIST]
do
[COMMANDS]
done

[LIST]可以是一系列由空格,select语句数字范围linux操作系统基础知识,命令输出,数组等分隔的字符串。 可以使用PS3 environment变量来设置select八声甘州造的自定义提示。

调用select构造时,列表中的每个项目都会打印在屏幕上(标准错误)八十年代彪悍媳妇,并带有数字。

如果用户输入的数字与显示的项之一的编号相对应,则将[Ilinux常用命令TEM]的值设置为该项。 所选项的值存储在变量REPLY八声甘州Bash 否则,linux中文乱码视频如果用户输入为空,则再次显示提示和菜单列表。

select循环将继续运行,并提示用户输入,直到执行break命令为止。

为演示select构造的select是什么意思中文工作原理,让我们看下面的简单示例linux操作系统基础知识

PS3="Enter a number: "
select characlinux中文乱码视频ter in Sheldon Leonard Penny Howard Raj
do
echo "Selected character: $character"
echo "Selected number: $REPLY"
done

巴沙鱼本将显示一个菜单,linux命令该菜单由带有伴随数字的列表项和PS3提示组成。 当用户输入数字时,脚本将打印所选字符和linux命令数字:

1) Sheldon
2) Leonard
3) Penny
4linux) Howard
5) Raj
Enter a numBashber: 3
Selected character: Penny
Sellinux命令ected numberlinux: 3
Enter a number:

Bash select范例

通常,selectif表达式中selected是什么牌子case结合使用。

让我们看一个更实际的例子。 它是一个简单的计算器,可以提示用户输入并执行基本的算术运算,例如加法,减法,乘法和除法。

PS3="Select the opeselect语句的基本用法rationlinux: "
select opt iselect语句的基本用法n add subtract multiply divide quit; do
case $opt in
add)
read -p "Enter the first number: " n1
read -八声甘州p "Enter the second number: " n2
echo "$n1 + $n2 = $(($n1+$n2))"
;;
subtract)
rea八省联考成绩查询入口dselected是什么牌子的男装 -p "Enter the first number: " n1
read -p "Enter the second numb八十年代彪悍媳妇er: " n2
echo "$n1 - $n2 = $(($n1-$n2))"
;;
multiply)
read -p "Eselected是什么牌子的男装nter the first number: " n1
read -p "Enter the second number: " n2
echo "$n1 * $n2 = $(($n1*$n2))"
;;
divide)
read -p "Enter the first number: "八十年代军婚宠妻 n1
read -p "selectedEnter the second number: " n2
echo "$n1 / $n2 = $(($n1/$n2))"
;;
quit)
break
;;
*)
echo "Invalid oplinux系统tion $REPLY"
;;
esac
d八省联考排名one

执行脚本后,将显示菜单和P八声甘州S3提示。 提示用户选择操作,然后输入两个数字。 根据用户的输入,脚本将打印结果。 每次选择后,都将要求用户执selected是什么牌子的男装行新操作,直到执行break命令为止。

1) add
2) subtract
3) multiply
4) divide
5) quit
Select the operation: 1selector
Enter the first number: 4
Enter the second nulinux是什么操作系统mber: 5
4 + 5 = 9
Select the operation: 2
En巴沙鱼ter the first number: 4
Enter the second number: 5
4 - 5 = -1
Select the operation: 9
Invalid option 9
Select the operation: 5

该脚本的一个缺点是它只能与整数一起使用。

以下是更高级的版linux重启命令本。 我们使用支持浮点数的bc工具来执行数学计算。 同样,重复代码被分组在函数中。

calcullinux是什么操作系统ate () {
read -p "Enter the first number: " n1
read -p "Enter the second number: " n2
echo "$n1 $1 $n2 = " $(linuxb巴沙鱼的做法c -l <巴沙鱼的做法;<< "select语句$n1$1$n2")
}
PS3="Select the operatioSelectn: "
select opt in add subtract multselectedip巴蜀中学ly divide quit; do
case $opt in
add)
calculate "+";;
subtrBashact)
calculate "-";;
multiply)
calculate "*";;
divide)
calculate "/";;Select
quit)
bSelectreak;;
*)
echo "InBashvalid option $REPLY";selective;
esac
done
1) add
2) subtra巴蜀中学ct
3) multiply
4) divide
5) quit
Select the operlinux必学的60个命令ation: 4
Enter the first number: 8
Enter the secselect是什么意思中文ond number: 9
8 / 9 =  .888888888888linux系统88888888
Select the operation: 5

结论

select构造使您可以轻松生成菜单。 在编写需要用户输入的shell脚本时,它特别有用。如果您有任何问题或反馈,请随时发表评论。