【发布时间】:2021-06-23 14:58:25
【问题描述】:
稍后我将把其余的功能添加到程序中,它仍处于早期阶段,但由于某种原因,我似乎无法退出 while 循环,即使 if 语句在其中并且在菜单。这是到目前为止的代码。
continue="true"
#while loop to keep the code running till the user inputs Q
while [ $continue = "true" ]
#loop start
do
#clearing screen before showing the menu
clear
echo "A to Create a user account"
echo "B to Delete a user account"
echo "C to Change Supplementary Group for a user account"
echo "D to Create a user account"
echo "E to Delete a user account"
echo "F to Change Supplementary Group for a user account"
echo "Q to Quit"
read -p "What would you like to do?:" choice
#Test to end the program
if [ $choice = 'Q' ] || [ $choice = 'q']
then
$continue="false"
fi
#loop end
done```
【问题讨论】:
-
break是您正在寻找的。help break了解更多信息。此外,您不需要[ ]或任何测试,如果无限循环是您所追求的,这里while true; do...; done或while :; do ....; done见help : -
对于我的分配,我不允许使用 exit 或 break 语句退出 while 循环
-
太棒了!那玩得开心。
-
$continue=false是语法错误。您不能在作业的左侧使用$。放一个shebang并将您的代码粘贴到shellcheck.net -
使用
$获取变量的值,而不是设置它。
标签: linux bash if-statement while-loop scripting