我正在linux中创建一个新的菜单驱动的Shell脚本,我将我的表简化为hello and bye使这更简单,下面是我的基本菜单布局
# Menu Shell Script
#
echo ----------------
echo menu
echo ----------------
echo [1] hello
echo [2] bye
echo [3] exit
echo ----------------
基本上我有菜单,我最近一直在玩一些东西,但似乎没有任何工作,因为我是新手,我想下一行将是
`read -p "Please Select A Number: " menu_choice`
但我不知道如何处理变量,什么不行。 我想知道是否有人可以帮助我使用下一段代码来简单地让它在我按下时打个招呼,当按下2时再见,当用户按下3时退出3.这将是非常值得赞赏的,因为我有几天来一直在尝试不同的方式,似乎无法让它发挥作用。
答案 0 :(得分:2)
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
修改强>
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;
答案 1 :(得分:0)
这是一个示例
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
或使用案例
case $menu_choice in
1) echo hello
;;
2) echo bye
;;
3) exit 0
;;
esac