user3232310
user3232310

Reputation: 1

Shell script comparing strings

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.

Upvotes: 0

Views: 112

Answers (1)

anubhava
anubhava

Reputation: 786289

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

Upvotes: 1

Related Questions