一次作业

Linux


#!/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

标签: none

已有 3 条评论

  1. 看的我热血沸腾啊https://www.ea55.com/

  2. 文章的确不错啊https://www.cscnn.com/

  3. 每次看到你的文章,我都觉得时间过得好快。 https://www.yonboz.com/video/31208.html

添加新评论