我最近在尝试使用对话框和bash脚本进行实验。我创建了一个对话框菜单,其中包括三个选项:用户、密码和组,还有一个退出选项。我试图在case语句中为每个选项运行函数,但无论我选择哪个选项,它似乎总是落入到通用的最后一个语句,并输出“Something Else…”语句,而不是运行相关函数中的echo语句,例如echo "Entering Users sub-menu"。
我已经尝试打开调试模式来运行此脚本:
我得到了以下输出:
我还在努力弄清楚“Expected a box option”的含义,这似乎与对话框有关,但我也在想我的 Bash 的 if 语句或 case 语句是否有问题。
我已经尝试打开调试模式来运行此脚本:
bash -x myscript
我得到了以下输出:
+ choice='Users
Error: Expected a box option.
Use --help to list options.'
+ '[' 'Users
Error: Expected a box option.
Use --help to list options.' '!=' ' Quit ']'
+ case $choice in
+ echo 'Something else. Where Am I?'
Something else. Where Am I?
+ exit 0
我还在努力弄清楚“Expected a box option”的含义,这似乎与对话框有关,但我也在想我的 Bash 的 if 语句或 case 语句是否有问题。
我的脚本代码:
#!/bin/bash
function quit {
exit 0
}
function menu {
choice=$(dialog --backtitle "Rob Graves's Bash Scripting" \
--title "Main Menu" --menu "Choose one" 30 50 4 "Users" \
"- Do something with users" "Passwords"\
"- Do stuff with passwords" "Groups" "- Do things with groups" \
"Quit" "- Exit to desktop" --clear --nocancel 3>&1 1>&2 2>&3)
if [ "$choice" != "Quit" ]; then
case $choice in
Users)
users #calls users function
;;
Passwords)
passwords #calls passwords function
;;
Groups)
groups #calls groups function
;;
*)
echo "Something else. Where Am I?"
;;
esac
else
echo "Quitting..."
quit
fi
}
function users {
echo "Entering Users sub-menu"
}
function passwords {
echo "Entering Passwords sub-menu "
}
function groups {
echo "Entering Groups sub-menu"
menu
exit 0