在 bash 中创建子菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25095714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating sub menu in bash
提问by Zenix
I'm trying to do the following :
我正在尝试执行以下操作:
function main_menu
{
option=0
until [ "$option" = "4" ]; do
echo " 1.) Add user"
echo " 2.) Remove user"
echo " 3.) Update user"
echo " 4.) Quit"
echo -n "Enter choice: "
read option
echo ""
case $option in
1 ) add_user ; press_enter ;;
2 ) remove_user ; press_enter ;;
3 ) update_user ; press_enter ;;
4 ) exit;;
* ) tput setf 4;echo "Please enter 1, 2, 3, or 4";tput setf 4;
esac
done
}
In a switch case(do while option is not 4).The 3rd option will show another sub menu with switch case as follows:
在 switch case 中(do while option is not 4)。第三个选项将显示另一个带有 switch case 的子菜单,如下所示:
function update_user
{
option=0
until [ "$option" = "3"]; do
echo " 1.) Update username"
echo " 2.) Update password"
echo " 3.) Return to menu"
echo -n "Enter choice: "
read option
echo ""
case $option in
1 ) update_username; press_enter ;;
2 ) update_password; press_enter ;;
3 ) main_menu; press_enter ;;
4 ) exit;;
* ) tput setf 3;echo "Please enter 1, 2 or 3";tput setf 3;
esac
done
}
The 3rd option goes back to the main menu, but when I try to quit the sub menu keeps appearing.
第三个选项返回到主菜单,但是当我尝试退出时,子菜单不断出现。
Anyone can advise me on a better way?
任何人都可以建议我更好的方法吗?
采纳答案by Cyrus
Replace two times $selection
with $option
, break
with exit
, remove }
in function update_user
, in function update_user
add }
after done
and replace "$option" = "7"
with "$option" = "4"
.
替换两次$selection
with $option
,break
with exit
,}
在函数update_user
中删除,在函数中update_user
添加}
之后done
并替换"$option" = "7"
为"$option" = "4"
。
#!/bin/bash function update_user { option=0 until [ "$option" = "3"]; do echo " 1.) Update username" echo " 2.) Update password" echo " 3.) Return to menu" echo -n "Enter choice: " read option echo "" case $option in 1 ) update_username; press_enter ;; 2 ) update_password; press_enter ;; 3 ) main_menu; press_enter ;; 4 ) break ;; * ) tput setf 3;echo "Please enter 1, 2 or 3";tput setf 3; esac # } done } function main_menu { option=0 until [ "$option" = "4" ]; do echo " 1.) Add user" echo " 2.) Remove user" echo " 3.) Update user" echo " 4.) Quit" echo -n "Enter choice: " read option echo "" case $option in 1 ) add_user ; press_enter ;; 2 ) remove_user ; press_enter ;; 3 ) update_user ; press_enter ;; 4 ) exit;; * ) tput setf 4;echo "Please enter 1, 2, 3, or 4";tput setf 4; esac done } main_menu
回答by John B
As pointed out by @KonstantinV.Salikhov, menus in bash are what select
loops are for.
正如@KonstantinV.Salikhov 所指出的,bash 中的菜单就是select
循环的用途。
Here's one way you can implement your menu using select
:
这是您可以使用以下方法实现菜单的一种方法select
:
main_menu () {
options=(
"Add user"
"Remove user"
"Update user"
"Quit"
)
select option in "${options[@]}"; do
case $option in
${options[0]})
add_user
break
;;
${options[1]})
remove_user
break
;;
${options[2]})
update_user
break
;;
${options[3]})
exit
;;
*)
echo invalid option
;;
esac
done
}
main_menu