我的代码无法验证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
读取-p“输入数字:” - 输入#Prompt&读取字符串。 echo“你的输入是$ input” echo#passwd> = 8 #check uppder #check lower #check for num 如果回显“$ input”| egrep“^。{8,255}”| egrep“[A-Z]”| egrep“[a-z]”| egrep“[0-9]”;然后 echo“密码有效” 其他 echo“密码无效” 网络
echo“$ input”| tr'A-Za-z0-5''N-ZA-Mn-za-m6-9-5'
退出0
答案 0 :(得分: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