Linux实验2——shell模拟考勤程序

用shell设计一个模拟考勤程序,实现如下功能选择:
1:上班签到
2:下班签出
3:缺勤信息查阅
4:用户信息维护
考勤程序运行后,提示用户输入上述功能选择,并验证用户输入的用户名和密码;用户信息保存在userinfo.dat中。
用户信息维护:提供用户添加、删除功能如果是上班签到,记录签到信息,如果签到时间大于上午8时,则提示用户迟到,并记录该迟到信息到check.dat中。
如果是下班签出,记录签出信息,如果签出时间小于下午6时,则提示用户早退,并记录该早退信息到check.dat。
如果用户选择考勤信息查询,则将check.dat中对应的用户迟到早退的信息查询出来并显示。
用户选择功能执行完,Shell程序继续回到功能选择界面等待下一个用户进行操作。

#! /bin/bash
function homepage(){
    clear;
    echo "———————— Employee attendance Homepage ————————";
    echo "|              1.Sign in                     |";
    echo "|              2.Sign out                    |";
    echo "|              3.The record                  |";
    echo "|              4.User Maintenance            |";
    echo "|              5.Exit                        |";
    echo "——————————————————————————————————————————————";
    echo "Please select:";
}

function sign_in(){
    echo " Please input your username";
    read name;
    echo " Please input your password";
    read password;
    if test -e /home/liuhui/userinfo.dat   #判断文件是否存在
    then 
        while read e_name e_password
        do
            if test "$name" = "$e_name"
            then
                break;
            else
                continue;
            fi
        done < /home/liuhui/userinfo.dat
    else 
        echo "The file was not found!";
    fi
    if test "$name" != "$e_name"
    then 
        echo "The User not found!"; 
    elif test "$password" != "$e_password"
    then
        echo "Incorrect Password!";
    else
        hour=`date +%k`;
        if test $hour -ge 8     #签到时间晚于8点,迟到
        then 
            echo "Punching is successful and you are late!";
            echo "$name    ---Late for Work ---Time: `date` " >>/home/liuhui/check.dat
        else 
            echo "Sign in Successfully!";
        fi
    fi
}

function sign_out(){
    echo " Please input your name";
    read name;
    echo " Please input your password";
    read password;
    if test -e /home/liuhui/userinfo.dat
    then 
        while read temp_name temp_password
        do
            if test "$name" = "$temp_name"
            then
                break;
            else
                continue;
            fi
        done < /home/liuhui/userinfo.dat
    else 
        echo "The file was not found!";
    fi
    if test "$name" != "$temp_name"
    then 
        echo "The User not found!"; 
    elif test "$password" != "$temp_password"
    then
        echo "Incorrect Password!";
    else
        hour=`date +%k`;        #获取系统时间
        if test $hour -lt 18    #签退时间早于18点,早退
        then 
            echo "Successfully signed out, early departure!";
            echo "$name    ---Leave Early   ---Time: `date` " >>/home/liuhui/check.dat
        else 
            echo "Sign out Successfully! ";
        fi
    fi
}

function display_record(){
    if test $# -ne 0
    then
        echo "$1,Here are your records:";
        grep "$1" /home/liuhui/check.dat;
    else 
        echo "You do not have permissions, please log in first!";
    fi
}

function user_maintenance(){
    clear;
    echo "————————————————————————————————————";
    echo "|            1.addUser             |";
    echo "|            2.deleteUser          |";
    echo "|            3.exit                |";
    echo "————————————————————————————————————";
    while test "1"="1"
    do
        echo "Please select: ";
        read choice;
        if test $[choice] -eq 1                    
        then
            echo "Please input your username:";
            read username;
            echo "Please input your password:";
            read password;

            #查询当前用户是否已经在文件中存在
            if test -e /home/liuhui/userinfo.dat
            then
                while read e_name e_password
                do
                    if test "$username" = "$e_name"
                    then
                        break;
                    else
                        continue;
                    fi
                done < /home/liuhui/userinfo.dat
            else
                echo "The file was not found!";
            fi
            if test "$username" = "$e_name"
            then
                echo "The user has already existed.";
            else
                echo "$username $password" >> /home/liuhui/userinfo.dat;
                echo "Add Successfully!";
            fi
        elif test $[choice] -eq 2
        then
            echo "Please input the username and password:";
            read username;
            read password;

            if test -e /home/liuhui/userinfo.dat
            then
                while read e_name e_password
                do
                    if test "$username" = "$e_name"
                    then
                        break;
                    else
                        continue;
                    fi
                done < /home/liuhui/userinfo.dat
            else
                echo "The file was not found!";
            fi
            if test "$username" = "$e_name"
            then
                if test "$password" = "$e_password"
                then
                    tmpinfo=""$username" "$password"";
                    sed -i '/'"$tmpinfo"'/d' userinfo.dat;
                    echo "Delete Successfully! ";
                else
                    echo "Password is wrong! Please re-enter it! ";
            fi
            else
                echo "User is not exist!";
            fi
        elif test $[choice] -eq 3
        then
            clear;
            break;
        else
            echo "Please input correct number!";
        fi
    done
}

function exit(){
    echo "Welcome to use next time!";
    isExit="1";
}

function main(){
    while test "1"="1"
    do 
        homepage;
        read choice;
        case $choice in
            1)sign_in;;
            2)sign_out;;
            3)display_record $name;;
            4)user_maintenance;;
	        5)exit;;
            *)echo "Incorrect input!";;
        esac
        read delay;
        if test "$isExit" = "1"
        then
            clear; 
            break;
        fi
    done
}
main;

你可能感兴趣的:(Linux实验,linux)