Linux实验一:Shell实现签到签退
一次作业
#!/bin/bash
# Created by FancyKing
# Verify the username and password entered by the user
# Check if the file exists
# Create a new file if the specified file does NOT exist
# Code 1 indicates that the user authentication is SUCCESSFUL
# Code 2 indicates that the user password check failed
# COde 3 indicates that the current user has NOT been queried
ASD() {
asd=-1;
have_user=-1;
if [ ! -f ./userinfo.dat ];
then
touch ./userinfo.dat;
echo -e "\t[\033[37;31;5m\e[1;31mERROR\e[0m\033[39;49;0m] Database does NOT exist";
else
while read -r uname passwd _; do
# User information found based on username
if [[ $name = $uname ]];
then
have_user=1;
# User password verfication succeeded
if [ $password = $passwd ];
then
asd=1;
break;
else
asd=2;
fi
else
continue;
fi
done < ./userinfo.dat
fi
if [ $have_user -eq 0 ];
then
asd=3;
fi
echo $asd;
}
# The program boot interface
display() {
echo "";
echo -e "\t################Simulated attendance################";
echo -e "\tPlease select the serial number to call the function";
echo -e "\t 1. Check in";
echo -e "\t 2. Check out";
echo -e "\t 3. Absence information";
echo -e "\t 4. Exit";
echo -e -n "\tNow your choice is :";
}
readinfo() {
# Get username and password
# For security, password entry is NOT visible
read -p " Please input your name:" name;
read -p " Please input your password:" -s password;
echo $name;
echo $password;
}
check_in() {
info=$(readinfo);
name=$(echo $info | cut -d ' ' -f 1);
password=$(echo $info | cut -d ' ' -f 2);
echo "";
asd=$(ASD $name $password);
if [ $asd -eq 1 ];
then
hour=`date +"%k"`;
if [[ hour -ge 8 ]];
then
echo -e "\t[\033[37;31;5m\e[1;31mWarning\e[0m\033[39;49;0m] Sorry $name, you check in too late";
echo "$name in `date` check IN late" >> ./check.dat;
else
echo -e "\t[\033[37;31;5m\e[1;32mSuccess\e[0m\033[39;49;0m] Check in success";
fi
elif [ $asd -eq 2 ];
then
echo -e "\t[\033[37;31;5m\e[1;31mWarning\e[0m\033[39;49;0m] The password you entered is incorrect";
else
echo -e "\t[\033[37;31;5m\e[1;31mWarning\e[0m\033[39;49;0m] The user you are looking for does NOT exist";
fi
}
check_out() {
info=$(readinfo);
name=$(echo $info | cut -d ' ' -f 1);
password=$(echo $info | cut -d ' ' -f 2);
echo "";
asd=$(ASD $name $password);
if [ $asd -eq 1 ];
then
hour=`date +"%k"`;
if [[ hour -lt 17 ]];
then
echo -e "\t[\033[37;31;5m\e[1;31mWarning\e[0m\033[39;49;0m] Sorry $name, you check out too early";
echo "$name in `date` check OUT early" >> ./check.dat;
else
echo -e "\t[\033[37;31;5m\e[1;32mSuccess\e[0m\033[39;49;0m] Check out success";
fi
elif [ $asd -eq 2 ];
then
echo -e "\t[\033[37;31;5m\e[1;31mWarning\e[0m\033[39;49;0m] The password you entered is incorrect";
else
echo -e "\t[\033[37;31;5m\e[1;31mWarning\e[0m\033[39;49;0m] The user you are looking for does NOT exist";
fi
}
get_info() {
info=$(readinfo);
name=$(echo $info | cut -d ' ' -f 1);
password=$(echo $info | cut -d ' ' -f 2);
echo "";
have_output=0;
asd=$(ASD $name $password);
if [ $asd -eq 1 ];
then
while read line; do
if [[ $line =~ ^$name.* ]];
then
echo $line;
else
continue;
fi
done < ./check.dat
elif [ $asd -eq 2 ];
then
echo -e "\t[\033[37;31;5m\e[1;31mWarning\e[0m\033[39;49;0m] The password you entered is incorrect";
else
echo -e "\t[\033[37;31;5m\e[1;31mWarning\e[0m\033[39;49;0m] The user you are looking for does NOT exist";
fi
}
exit() {
echo -e "\t[Message] EXIT";
isexit=1;
}
error() {
echo -e "\t[\033[37;31;5m\e[1;31mWarning\e[0m\033[39;49;0m] You entered an incorrect command";
}
main() {
isexit=0;
while [ "1" = "1" ]; do
display;
read choice;
case $choice in
1)check_in;;
2)check_out;;
3)get_info;;
4)exit;;
*)error;;
esac
if [ $isexit -eq 1 ];
then
clear;
break;
fi
done
}
main;
```shell
看的我热血沸腾啊https://www.ea55.com/
文章的确不错啊https://www.cscnn.com/
每次看到你的文章,我都觉得时间过得好快。 https://www.yonboz.com/video/31208.html
文字流畅如丝,语言优美动人,读来令人心旷神怡。
?内容类评语?
价值导向积极,彰显社会责任意识。
文章的叙述风格独特,用词精准,让人回味无穷。
新盘 上车集合 留下 我要发发 立马进裙
新车上路,只带前10个人coinsrore.com
2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
新车首发,新的一年,只带想赚米的人coinsrore.com
新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
新车上路,只带前10个人coinsrore.com
新盘首开 新盘首开 征召客户!!!coinsrore.com
新项目准备上线,寻找志同道合的合作伙伴coinsrore.com
新车即将上线 真正的项目,期待你的参与coinsrore.com
新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com