If statement unix shell脚本中的if语句问题和grep问题??

If statement unix shell脚本中的if语句问题和grep问题??,if-statement,find,grep,cut,If Statement,Find,Grep,Cut,为什么在我的脚本中,运行它时没有输出?我的if和grep中有什么问题??多谢各位 这是我的剧本 userfile=~/casestudies/login_users clear echo " Password Recovery " echo "=============================" echo "Username: " ; read user cuser=`grep $user $login_users | cut -d':' -f

为什么在我的脚本中,运行它时没有输出?我的if和grep中有什么问题??多谢各位

这是我的剧本

userfile=~/casestudies/login_users

clear

echo "      Password Recovery      "

  echo "============================="

  echo "Username: " ; read user 

  cuser=`grep $user $login_users | cut -d':' -f1`

     if [ "$cuser" = "$user" ]
         then 
              echo "Then choose a question to answer below"
              echo "(A) What is your 5th favorite color? "
              echo "(B) What is your favorite food? "
              echo "(C) What is the name of your pet? "
              echo "(D) What is the middle name of your mother's maiden name? "
              echo "(E) What is the model number of your laptop/monitor? "
              echo "choice : "         
         else
              echo "Invalid choice. Choose another."
     fi         
              read question || continue
              case $question in
                   [Aa]) echo "answer in question A: " ; read A
                      if [[ "$A" == $(grep $A userfile | tr ':' ' ') ]]  #A is the answer of the user in question A, it is stored in the login_users 
                        then      
                             echo "Correct, your password is: " 
                             echo 
                             grep $passwd $userfile | tr ':' ' '   #passwd is the original password of the user, it is stored in login_users
                        else
                             echo "Incorrect. Choose another question to answer." ;
                      fi ;;                  
                   [Bb]) echo "answer in question B: " ; read B
                      if [[ "$B" == $(grep $B userfile | tr ':' ' ') ]]  #B is the answer of the user in question A, it is stored in the login_users 
                        then      
                             echo "Correct, your password is: " 
                             echo 
                                             grep $passwd $userfile | tr ':' ' '   #passwd is the original password of the user, it is stored in login_users
                        else
                             echo "Incorrect. Choose another question to answer." ;
                      fi ;; 
                   [Cc]) echo "answer in question C: " ; read C
                      if [[ "$C"== $(grep $C userfile | tr ':' ' ') ]]  #C is the answer of the user in question A, it is stored in the login_users 
                        then      
                             echo "Correct, your password is: " 
                             echo 
                             grep $passwd $userfile | tr ':' ' '   #passwd is the original password of the user, it is stored in login_users
                        else
                             echo "Incorrect. Choose another question to answer." ;
                      fi ;;                    
                   [Dd]) echo "answer in question D: " ; read D
                      if [[ "$D"== $(grep $D userfile | tr ':' ' ') ]]  #D is the answer of the user in question A, it is stored in the login_users 
                        then      
                             echo "Correct, your password is: " 
                             echo 
                             grep $passwd $userfile | tr ':' ' '   #passwd is the original password of the user, it is stored in login_users
                        else
                             echo "Incorrect. Choose another question to answer." ;
                      fi ;;                   
                   [Ee]) echo "answer in question E: " ; read E
                      if [[ "$E"== $(grep $E userfile | tr ':' ' ') ]]  #E is the answer of the user in question A, it is stored in the login_users 
                        then      
                             echo "Correct, your password is: " 
                             echo 
                                             grep $passwd $userfile | tr ':' ' '   #passwd is the original password of the user, it is stored in login_users
                        else
                             echo "Incorrect. Choose another question to answer." ;
                      fi ;;                
              esac                 
正在寻找可变的$login_用户,这些用户从未分配过。你想用

grep $user $userfile

在shell脚本中使用未声明的变量太容易了,任何时候你都看不到任何错误。

如果你想知道为什么你的问题格式不正确,请阅读。它仍然不正确。前几行没有缩进。@chen:如果不调试代码,我猜您永远都不会进入grep。您是否尝试打开shell的调试功能?将-vx设置在脚本的顶部,或者在要检查的grep之前,应该会有所帮助。你应该看到你的grep行正在执行,前面有一个+。祝你好运
grep $user $userfile