【发布时间】:2013-09-30 08:19:33
【问题描述】:
我的代码无法验证 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 如果回显“$输入”| egrep "^.{8,255}" | egrep "[A-Z]"| egrep "[a-z]" | egrep "[0-9]";然后 echo "密码有效" 别的 echo "密码无效" 菲
回显 "$input" | tr 'A-Za-z0-5' 'N-ZA-Mn-za-m6-9-5'
退出 0
【问题讨论】:
-
问题是什么?
-
您不是说在调用验证函数之后(即在
validatePassword和encryptPassword之间)进行返回值检查吗? -
我能够让它在这里工作是我的更新。 #! /bin/bash read -p "Enter a numbers:" -e input #Prompt & read string. echo "你的输入是 $input" echo # passwd >= 8 #check uppder #check lower #check for num if echo "$input" | egrep "^.{8,255}" | egrep "[A-Z]"| egrep "[a-z]" | egrep "[0-9]";然后 echo "密码有效" else echo "密码无效" fi echo "$input" | tr 'A-Za-z0-5' 'N-ZA-Mn-za-m6-9-5' 出口 0
标签: bash encryption passwords logic