#! /bin/bash
# expriment_1.sh check.dat sign_in.dat userinfo.dat
login_in(){
echo "******************************************"
echo "用户名:"
read username
echo "密码:"
read password
echo ""
echo ""
echo ""
}
check(){
echo "******************************************"
echo "1: 上班签到"
echo "2: 下班签出"
echo "3:缺勤信息查阅"
echo ""
echo "输入选项:"
read choice
time=`date +%H`
fulltime=`date +%F%r`
case $choice in
1)
echo "用户 ${username} 上班时间 ${fulltime}" >> sign_in.dat #重定向,>是覆盖,>>是追加
if [ $time -ge 8 ]
then
echo "上班迟到!"
echo "用户 ${username} 上班迟到 ${fulltime}" >> check.dat
else
echo "上班签到成功!"
fi
;;
2)
echo "用户 ${username} 下班时间 ${fulltime}" >> sign_in.dat
if [ $time -lt 18 ]
then
echo "下班早退!"
echo "用户 ${username} 下班早退 ${fulltime}" >> check.dat
else
echo "下班签出成功!"
fi
;;
3)
while read line
do
arr=($line) #将一个字符串按空格分开,变成一个数组
if [ ${arr[1]} = $username ]
then
echo $line
fi
done< ./check.dat
;;
*)
echo "你应该选择1,2,或3,请正确选择!"
;;
esac
}
while true
do
login_in
messege="${username} ${password}"
flag=0; #赋值时不能有空格
while read line
do
if [ "$line" = "$messege" ] #有空格的字符串比较时,加""
then
flag=1;
fi
done < ./userinfo.dat #read 被重定向,如果在里面使用read,将在重定向处读入
if [ $flag -eq 1 ] #两边要有空格
then
check
fi
done