【问题标题】:if statement issue and grep issue in shell script of unix??unix的shell脚本中的if语句问题和grep问题?
【发布时间】:2011-03-08 15:08:34
【问题描述】:

为什么在我的脚本中运行时没有输出?我的 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                 

【问题讨论】:

  • 如果您想知道为什么您的问题格式不正确,请阅读stackoverflow.com/editing-help。
  • 它仍然很不稳定。第一行没有缩进。
  • @chen:如果不尝试调试代码,我猜你永远不会使用 grep。您是否尝试过打开 shell 的调试功能? set -vx 在脚本的顶部,或者在您要检查的 grep 之前应该有帮助。您应该看到您的 grep 行在前面使用 + 执行。祝你好运。

标签: if-statement find grep cut


【解决方案1】:
grep $user $login_users 

正在寻找从未被分配的变量 $login_users。你想用

grep $user $userfile

在 shell 脚本中使用未声明的变量太容易了,您通常根本看不到任何错误。

【讨论】:

    猜你喜欢
    • 2017-05-21
    • 2012-06-30
    • 1970-01-01
    • 2021-07-26
    • 2019-08-15
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    相关资源
    最近更新 更多