如果从菜单中选择非选项,则需要脚本来显示使用信息

如果从菜单中选择非选项,则需要脚本来显示使用信息

我设法让我的程序正常运行,除了在用户尝试非菜单选项时发布使用消息。如果用户输入了错误的选项,应该显示的消息是第 75 行"Usage: mgr [Start|Stop|Restart|Help]"

剧本:

#!/bin/sh
#mgr prog
#Offers a choice of start, stop, and restart
#Executes choice then loops back to the menu

echo "==========================================" >> svclog
echo "  mgr" >> svclog
echo " mgr started " >> svclog

if [[ -f .running ]]
   then
      cat svclog
      sleep 10
fi

while [ true ]
do
   clear
   cat <<EOF

                =====
                M G R
                =====

                                $(date)

                  S -- Start svcd
                  X -- Stop svcd
                  R -- Restart svcd
                  H -- Save svclog to history file
                  I -- Help

                  q> quit

                 How may I assist you?

EOF


read ans

case $ans in
   S)  svcd
       cat svclog >> svc-hist
       rm svclog
       echo " Service starting." >> svclog
       ;;
   X)  pkill svcd
       echo " Service shutting down." >> svclog
       ;;
   R)  pkill svcd
       sleep 3
       svcd
       echo " Service has restarted." >> svclog
       ;;
   H)  cat svclog >> svc-hist
       rm svclog
       ;;
   I)  clear
       echo "
 The capital 'S' option will call the svcd file.
 The capital 'X' option will kill the svcd job.
 The capital 'R' option will kill the svcd jod, pause then call the svcd file.
 The capital 'H' option will append the svclog file log to the svc-hist file log.
 The 'I' option displays this message for 15 seconds.
 The lower case 'q' option quits this menu. "
       sleep 15
       ;;

   q)  echo " mgr stopped " >> svclog
       break
       ;;

else
   echo "Usage: mgr [Start|Stop|Restart|Help]" >> svclog

esac
done
clear
echo "
   "Louis, I think this is the beginning of a beautiful friendship."  ~Rick Blaine (Humphrey Bogart)
"

答案1

代替

else
   echo "Usage: mgr [Start|Stop|Restart|Help]" >> svclog
esac

你需要:

*) 
  echo "Usage: mgr [Start|Stop|Restart|Help]" >> svclog
esac

或者如果您只想打印而不是保存到文件:

*) 
  echo "Usage: mgr [Start|Stop|Restart|Help]"
esac

*)case语句中表示除上述之外的任何其他值。

这是关于使用的一个很好的教程case

相关内容