bash 菜单的 Shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15386623/
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
Shell Script for Menu
提问by user2157995
I am making a new Menu Driven Shell Script in linux, I have simplified my table to just hello and bye to make this simpler, below is my basic menu layout
我正在 linux 中创建一个新的菜单驱动的 Shell 脚本,我已经将我的表格简化为你好和再见以使其更简单,下面是我的基本菜单布局
# Menu Shell Script
#
echo ----------------
echo menu
echo ----------------
echo [1] hello
echo [2] bye
echo [3] exit
echo ----------------
Basically I have the menu, I have been playing around with a few things recently but cant seem to get anything working as I am new to this, I think then next line would be
基本上我有菜单,我最近一直在玩一些东西,但似乎无法让任何东西工作,因为我是新手,我想下一行是
`read -p "Please Select A Number: " menu_choice`
but I am not sure what to do with the variable and what not. I was wondering if anyone could help me with the next bit of code to simply get it to say hello when I press one, bye when 2 is pressed and exit when 3 when the user presses 3. It would be so much appreciated as I have been trying different ways for days and can't seem to get it to work.
但我不确定如何处理变量,不知道如何处理。我想知道是否有人可以帮助我编写下一段代码,以便在我按下一个时简单地打个招呼,按下 2 时再见,当用户按下 3 时退出 3。我将不胜感激几天来一直在尝试不同的方法,但似乎无法让它发挥作用。
采纳答案by Kent
you don't need those backticks for echo...and read
你不需要那些反引号echo...和read
echo "----------------"
echo " menu"
echo "----------------"
echo "[1] hello"
echo "[2] bye"
echo "[3] exit"
echo "----------------"
read -p "Please Select A Number: " mc
if [[ "$mc" == "1" ]]; then
echo "hello"
elif [[ "$mc" == "2" ]]; then
echo "bye"
else
echo "exit"
fi
Edit
编辑
showMenu(){
echo "----------------"
echo " menu"
echo "----------------"
echo "[1] hello"
echo "[2] bye"
echo "[3] exit"
echo "----------------"
read -p "Please Select A Number: " mc
return $mc
}
while [[ "$m" != "3" ]]
do
if [[ "$m" == "1" ]]; then
echo "hello"
elif [[ "$m" == "2" ]]; then
echo "bye"
fi
showMenu
m=$?
done
exit 0;
回答by uba
Here is a sample
这是一个示例
if [ $menu_choice -eq 1 ]
then
echo hello
elif [ $menu_choice -eq 2 ]
then
echo bye
elif [ $menu_choice -eq 3 ]
then
exit 0
fi
or using case
或使用案例
case $menu_choice in
1) echo hello
;;
2) echo bye
;;
3) exit 0
;;
esac

