0

Here's part of a shellscript I'm writing checking passwords stored in a file (along with names):

VALID_PASSWORD=`grep "Karl Marks" hiddenpasswords.txt|cut -f2 -d,`
echo $VALID_PASSWORD
echo enter password1
echo "Please enter the password"
read PASSWORD
if test "$PASSWORD" = "$VALID_PASSWORD"
then
echo "you have access"
else
echo "access denied"
fi

The grep part takes the correct password from the file, however "access denied" is always run no matter what I type in.

3
  • Chances are that there is a space after VALID_PASSWORD. Try saying echo "${VALID_PASSWORD}." Commented Jan 24, 2014 at 16:15
  • 1
    What happens when you run the script with sh -x? Does that tell you what is going wrong? Have you tried echoing echo "[$VALID_PASSWORD]" with the square brackets to indicate the limits of the variable? Have you tried echo "[$PASSWORD]" to see what that yields? Commented Jan 24, 2014 at 16:16
  • 1
    If you don't want the entered password to be visible as the user types it, use read -s PASSWORD (assuming you use bash) Commented Jan 24, 2014 at 16:19

1 Answer 1

1

The grep part takes the correct password from the file, however "access denied" is always run no matter what I type in.

It could be due to presence of whitespaces in your $VALID_PASSWORD.

Try changing first line to:

VALID_PASSWORD=$(awk -F '[, ]+' '/Karl Marks/ {print $2}' hiddenpasswords.txt)

TIP: Check content of both variables using cat -vte

echo "VALID_PASSWORD" | cat -vte
echo "PASSWORD" | cat -vte
Sign up to request clarification or add additional context in comments.

5 Comments

+1 good advice. another method to view the contents of the variables: od -c <<<"$VALID_PASSWORD"
Yes agreed, od -c is even more detailed info.
When I add the echo "VALID_PASSWORD" | cat -vte echo "PASSWORD" | cat -vte
When I add the echo "VALID_PASSWORD |cat -vte
Not getting your 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.