我只是创建了一个新的 shell 脚本,但遇到了一些小问题。以下是我的脚本:-
function mainMenu(){
while :
do
echo -e "\nMain Menu:"
echo " A. Process Managements Utilities"
echo " B. Memory Managements Utilities"
echo " C. Exit"
read -p "Select :" menuSelect
echo
case $menuSelect in
a|A) processMgmt;;
b|B) memoryMgmt;;
c|C) exit 0;;
*)echo "Invalid Input"
echo
;;
esac
done
}
当用户输入“INVALID INPUT”时,该功能将打印所有mainMenu()
输出:-
Main Menu:
A. Process Managements Utilities
B. Memory Managements Utilities
C. Exit
Select :e
Invalid Input
Main Menu:
A. Process Managements Utilities
B. Memory Managements Utilities
C. Exit
Select :
如何仅select:
在用户输入无效时打印?
Select:e
Invalid input
Select:s
Invalid input
答案1
由于您只想第一次打印有关选项 A、B 等的信息,while
打印后开始循环它:
function mainMenu(){
echo -e "\nMain Menu:"
echo " A. Process Managements Utilities"
echo " B. Memory Managements Utilities"
echo " C. Exit"
while :
do
read -p "Select :" menuSelect
echo
case $menuSelect in
a|A) processMgmt;;
b|B) memoryMgmt;;
c|C) exit 0;;
*)echo "Invalid Input"
echo
;;
esac
done
}
示例输出(即预期的来自问题):
Main Menu:
A. Process Managements Utilities
B. Memory Managements Utilities
C. Exit
Select :q
Invalid Input
Select :q
Invalid Input
通过这种方式,当调用函数时,会打印一次有关选择选择的信息,mainMenu
然后while
循环read
输入并case
执行您想要的工作。如果输入无效,while
再次循环询问read -p "Select"
。