0

我的代码无法验证 if 语句中给出的一些逻辑标准。我是 bash 的新手,所以任何帮助都会很棒。我的代码运行,但在我的逻辑中失败。另外,我想学习更多编写 bash 脚本的高级方法。任何建议将不胜感激。

function validatePassword ()
        {           
                local stat=1    #assigns 1 to (stat)
                local pass=$1
                LEN=${#pass}    #counts each character in the password length
                echo $LEN       #Prints string length
                        #checks for nums        chesks for lowercase    checks for uppercase            checks if pass is greater than 8
                if  [[ $pass =~ [0-9] ]] && [[ $pass =~ [a-z] ]] && [[ $pass =~ [A-Z] ]]  && [[ "$LEN" -ge 8 ]]; then 
                        stat=$?         #return 1 for false
                fi  
                return $stat
        }   
function encryptPassword ()
        {
                dual=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
                phrase=$encryptedPassword
                rotat=13
                newphrase=$(echo $phrase | tr "${dual:0:26}" "${dual:${rotat}:26}")
                echo "Your encrypted password is ${newphrase}"
        }   

#Promts the user for input storing into variable PASSWORD
read -p "Enter your password:" -e PASSWORD

passwordToCheck=$PASSWORD 
encryptedPassword=$PASSWORD

validatePassword $passwordToCheck       #Calls function passsing value of (PASSWORD)
encryptPassword $encryptedPassword      #Calls function passing value of (PASSWORD)

if [[ $? -ne 0 ]]; then         #(-ne) not equal command  
        echo "Invalid password"
else
        echo "Password is valid"
fi



#! /bin/bash

read -p "输入一个数字:" -e input #Prompt & read string。echo "你的输入是 $input" echo # passwd >= 8 #check uppder #check lower #check for num if echo "$input" | egrep "^.{8,255}" | egrep "[AZ]"| egrep "[az]" | egrep "[0-9]"; 然后 echo "密码有效" else echo "密码无效" fi

回声“$输入”| tr 'A-Za-z0-5' 'N-ZA-Mn-za-m6-9-5'

出口 0

4

1 回答 1

1

确定密码是否有效的唯一指定标准validatePassword存在于. 在提到的函数之后,您不会检查返回码。您改为在encryptPassword.

由于encryptPassword返回退出代码为0,因此return忽略前一个函数中的 。

您可能希望将代码的最后几行更改为:

validatePassword $passwordToCheck       #Calls function passsing value of (PASSWORD)

if [[ $? -ne 0 ]]; then         #(-ne) not equal command  
        echo "Invalid password"
else
        echo "Password is valid"
        encryptPassword $encryptedPassword      #Calls function passing value of (PASSWORD)
fi
于 2013-09-30T08:28:19.200 回答