Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何正确设置While循环中的If_Linux_Bash_If Statement_While Loop_Scripting - Fatal编程技术网

Linux 如何正确设置While循环中的If

Linux 如何正确设置While循环中的If,linux,bash,if-statement,while-loop,scripting,Linux,Bash,If Statement,While Loop,Scripting,稍后我会将其余的功能添加到程序中,它仍处于早期阶段,但出于某种原因,我似乎无法退出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

稍后我会将其余的功能添加到程序中,它仍处于早期阶段,但出于某种原因,我似乎无法退出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```
同样,您需要
continue=“false”
而不是
$continue=“false”


另外,我建议使用
if[“$choice”='Q']|[“$choice”='Q']
,这样当用户点击CR并且没有输入任何内容时,脚本不会中断。(还要注意,在该语句中,在last
]
之前需要一个空格。)

尽管语法错误已经在注释中突出显示,但我建议使用
break
完全避免
continue
变量。您还可以使用正则表达式组合这两个检查。像这样的

为true时;做
阅读-p“你想做什么?:”选项
如果[[“$choice”=~[Q | Q]];然后
打破
fi
完成
虽然当我看一下你问题中的回声陈述时,你可能最好完全避免使用if,而是使用case陈述

为true时;做
阅读-p“你想做什么?:”选项
中的案例“$choice”
q | q)中断
;; 
a | a)回应“创建用户帐户”
;;
#这还有别的吗
*)回显“未知选项”
;; 
以撒
完成

break
是您要寻找的<代码>帮助中断了解更多信息。此外,如果您所追求的是无限循环,则不需要使用
[]
或任何测试,此处
为true;做已完成
而:;做完成
请参阅
帮助:
对于我的作业,我不允许使用exit或break语句退出while循环太棒了!,那就玩吧。
$continue=false
是一个语法错误。作业的左侧不允许有
$
。在Use
$
中放置一个shebang并粘贴代码,以获取变量的值,而不是设置它。