我刚开始学习shell脚本,我正在尝试找出一种方法来添加一个选项,使用户返回菜单。例如:
5) echo "Return to the menu"
echo "Return back to the menu? ";;
听到的是手头的剧本:
echo "1. I'm number one"
echo "2. I'm number two"
echo "3. I'm number three"
echo "4. Exit from menu "
echo -n "Enter one the following numbers:"
while :
do
read choice
case $choice in
1) echo "You have selected the number one"
echo "Selected number is one";;
2) echo "You have selected the number two"
echo "Selected number is two";;
3) echo "You have selected the number three"
echo "Selected number is three";;
4) echo "Exiting after the information have been received by both devices" # Referring to the TCP/IP protocol. The information has to be established by both client and device to act like a server before data is sent. Ok I'm showing of here :)
exit;
esac
echo -n "Enter one of the four options"
done # Sorry if there are errors in the this code, I wrote it on the fly :)
3条答案
按热度按时间yr9zkbsy1#
也可以使用
select
创建菜单bjg7j2ky2#
要添加返回菜单功能,请尝试以下操作:添加一个变量accessMenu,将其设置为true,并将while循环条件更新为so,然后在循环中首先将accessMenu设置为false ......
最后,如果您希望能够返回菜单,请将变量设置为true,它将在while循环中传递条件以再次运行。
de90aj5v3#
我最近做了一个函数来实现这个功能,您所需要做的就是
break
返回到上一个函数https://stackoverflow.com/a/74793424/793088