【问题标题】:Creating Menus Shell Scripting创建菜单 Shell 脚本
【发布时间】:2023-01-04 01:12:54
【问题描述】:

我开始学习 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 :)

【问题讨论】:

  • 这将有助于解释您的代码当前的作用以及您希望输出如何不同。选项 1-3 应该退出循环吗?

标签: bash shell scripting menu


【解决方案1】:

要添加返回菜单功能,请尝试以下操作: 添加一个变量 accessMenu,将其设置为 true,并照此更新 while 循环条件,并在循环中首先将 accessMenu 设置为 false ...

accessMenu=true
while [[ $accessMenu -eq true ]]; do
    accessMenu=false
    ... (code)
done

最后,如果要启用返回菜单,将变量设置为true,它会在while循环中传递条件再次运行。

【讨论】: