0

My code fails to validate some of the logical criteria giving in the if statement. I'm new to bash so any help would be great. My code runs, but fail in my logic. Also, I would like to learn more advance ways to write bash script. Any suggestions would be greatly appreciated.

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 "Enter a numbers:" -e input #Prompt & read string. echo "Your input is $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]"; then echo "Password is valid" else echo "Password is invalid" fi

echo "$input" | tr 'A-Za-z0-5' 'N-ZA-Mn-za-m6-9-5'

exit 0

3
  • 1
    What is the question? Commented Sep 30, 2013 at 8:23
  • Don't you mean to place the return-value check after the validation function is called (i.e. between validatePassword and encryptPassword)? Commented Sep 30, 2013 at 8:25
  • I was able to get it working here is my update. #! /bin/bash read -p "Enter a numbers:" -e input #Prompt & read string. echo "Your input is $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]"; then echo "Password is valid" else echo "Password is invalid" fi echo "$input" | tr 'A-Za-z0-5' 'N-ZA-Mn-za-m6-9-5' exit 0 Commented Oct 2, 2013 at 17:37

1 Answer 1

1

The only specified criteria to determine whether the password is valid or not exists in validatePassword. You don't check the return code after the mentioned function. You instead check it after encryptPassword.

Since encryptPassword returns with an exit code of 0, the return in the former function is ignored.

You might want to change the last few lines of your code to:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.