有一个文件ff,#cat ff 内容如下(第1列为用户名,第2列为密码):
lilei abc123
jack qwe634
kate dbu173
编写脚本,判断用户名密码是否正确,如用户名或密码不正确,会给出相应提示,如:
./func.sh jak 13de4 --会提示用户名错误
./func.sh jack 13de4 --会提示密码错误
./func.sh jacj qwe634 --会提示验证通过
#!/bin/bash
file="ff"
curdir=`cd -P $(dirname $0); pwd`
cd $curdir
function judgeUserPass(){
username=$1
password=$2
while read line
do
uname=`echo $line |awk '{print $1}'`
upass=`echo $line |awk '{print $2}'`
if [[ $uname == $username ]];then
if [[ $upass == $password ]];then
echo "验证通过"
exit
else
echo "密码错误"
exit
fi
fi
done <"$file"
echo "用户名错误"
}
judgeUserPass $1 $2