bash - 当做出无效选择时让菜单循环 BASH
问题描述
嘿伙计们,所以我试图让这个菜单在 case 语句中做出无效选择时循环,但很难弄清楚我应该在我的 while 循环中回调什么我尝试使用 * 因为这是在case 作为无效的选择,但是当它看到它时它需要一个操作数,所以我不确定如何在下面调用它是代码,非常感谢任何帮助。
#Main menu.
#Displays a greeting and waits 8 seconds before clearing the screen
echo "Hello and welcome to the group 97 project we hope you enjoy using our program!"
sleep 8s
clear
while [[ $option -eq "*" ]]
do
#Displays a list of options for the user to choose.
echo "Please select one of the folowing options."
echo -e "\t0. Exit program"
echo -e "\t1. Find the even multiples of any number."
echo -e "\t2. Find the terms of any linear sequence given the rule Un=an+b."
echo -e "\t2. Find the numbers that can be expressed as the product of two nonnegative integers in succession and print them in increasing order."
#Reads the option selection from user and checks it against case for what to do.
read -n 1 option
case $option in
0)
exit ;;
1)
echo task1 ;;
2)
echo task2 ;;
3)
echo task3 ;;
*)
clear
echo "Invalid selection, please try again.";;
esac
done
解决方案
不要重新发明内置select
命令
choices=(
"Exit program"
"Find the even multiples of any number."
"Find the terms of any linear sequence given the rule Un=an+b."
"Find the numbers that can be expressed as the product of two nonnegative integers in succession and print them in increasing order."
)
PS3="Please select one of the options: "
select choice in "${choices[@]}"; do
case $choice in
"${choices[0]}") exit ;;
"${choices[1]}")
echo task1
break ;;
"${choices[2]}")
echo task2
break ;;
"${choices[3]}")
echo task3
break ;;
esac
done
如果您想留在菜单中直到“退出”,然后删除中断。
推荐阅读
- react-native - 错误:由于 React native (Realm) 中出现以下错误,需要迁移
- ruby-on-rails - 从 c9.io 迁移到 AWS Cloud9 后如何修复 rake db:migrate 错误?
- python - pygame:调整子类所基于的表面大小
- .net-core - 如何解决 HTTP 405 错误 NotAllowedMethod
- php - 如何调试 LARAVEL 419 POST 错误?
- powershell - 如何使用参数列表将参数添加到作业中
- coordinates - MySQL 的点类的 X 和 Y 值可以有多少位小数?
- android - 三星设备上不调用自我管理的连接服务回调
- angularjs - 使用 angularfire2 从 firebase 数据库实时获取新数据
- python - 两个 tf.data.Dataset 是否可以共存并由 tf.cond() 控制