Shell脚本比较字符串

时间:2014-01-24 16:10:43

标签: shell

这是一个shellcript的一部分我正在编写检查存储在文件中的密码(以及名称):

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

grep部分从文件中获取正确的密码,但无论我输入什么内容,“访问被拒绝”始终都会运行。

1 个答案:

答案 0 :(得分:1)

  

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

可能是由于$VALID_PASSWORD中存在空格。

尝试将第一行更改为:

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

提示:使用cat -vte

检查两个变量的内容
echo "VALID_PASSWORD" | cat -vte
echo "PASSWORD" | cat -vte
相关问题