2017-10-13 172 views
0

以下while循环旨在不断询问用户输入,直到输入指定的“break”字符。但是,由于某些原因,无论输入什么文本,都会触发IF语句。IF语句触发时不包含真正的语句

while true; do 
    #Prompt user for Input 
    printf "Please insert value. Insert the letter 'D' when you are done\n"; 
    #Read User Input 
    read User_Input; 
    echo "DEBUG: Input is: $User_Input"; 
    #Check User Input for Break Command 
     if [ "$User_Input"=="D" ]; then 
      break 
     fi 

done 

而且脚本中的我有用户输入变量至于我可以告诉if语句是正确的,该脚本不抛出任何错误,当运行宣布为User_Input="";

+0

如果您阅读[Bash手册页](http://man7.org/linux/man-pages/man1/bash.1.html),您会看到'=='运算符是Bash - 用于双括号表达式的特定扩展(即'[[...]]')。对于单支架,您应该阅读[test'手册页](http://man7.org/linux/man-pages/man1/test.1.html)。 –

回答

3
if [ "$User_Input"=="D" ]; then 

DEBUG: Input is: a 
+ '[' a==D ']' 
+ break 

应该写成

if [ "$User_Input" == "D" ]; then 

DEBUG: Input is: e 
+ '[' e == D ']' 
+ true 
+ printf 'Please insert value. Insert the letter '\''D'\'' when you are done\n' 
Please insert value. Insert the letter 'D' when you are done 
+ read User_Input 
D 
+ echo 'DEBUG: Input is: D' 
DEBUG: Input is: D 
+ '[' D == D ']' 
+ break 

空间之前,需要和后==

2

if [ "$User_Input"=="D" ]; then 

# space 
if [ "$User_Input" == "D" ]; then 

因为它调用命令[与4参数。

由于bash buildins的手册页声明:“每个运算符和操作数 必须是单独的参数。”