Linux——实验一:Shell程序设计



[ 实验目的 ]

理解Shell程序的设计方法;熟悉Shell程序的编辑、运行、调试方法与过程。 

[ 实验内容 ] 考勤模拟Shell程序设计

shell设计一个考勤模拟程序,实现如下功能选择界面:

1.上班签到

2.下班签出

3.缺勤信息查阅

4.退出

考勤程序运行后,提示用户输入上述功能选择,并验证用户输入的用户名和密码;用户信息保存在userinfo.dat中。

如果是上班签到,记录签到信息,如果签到时间大于上午8时,则提示用户迟到,并记录该迟到信息到check.dat

如果是下班签出,记录签出信息,如果签出时间小于下午6时,则提示用户早退,并记录该早退信息到check.dat

如果用户选择缺勤信息查询,则将check.dat中对应该用户的迟到早退信息查出并显示。

用户选择功能执行完,shell程序继续回到功能选择界面等待下一个用户进行操作。

 [实验要求 ]

 1、掌握Shell程序的编辑、运行、调试方法

2、完成实验内容要求实现的功能,并且上班签到、下班签出、缺勤信息查阅都要求用函数实现

3、撰写实验报告

 [实验方法 ]

1Shell程序的编辑可使用vi,emacsLinux下的各种文本编辑器。本课程实验可使用Red Hat Linux9.0下的Text Editor

2Shell程序的执行有两种方式:sh  [Shell程序名] ./ [Shell程序名]

例:设Shell程序名称为test.sh,则可以通过sh test.sh ./test.sh。但是要注意在使用./ [Shell程序名]时必须确保对Shell程序具有可执行权限。

3Shell程序的调试可以通过建立多个工作区交互进行。


新建文件Attendance.sh,源码如下:

#! /bin/bash
function show(){
    clear;
    echo "******** Welcome to Attendance System ********";
    echo "********       1.Check in             ********";
    echo "********       2.Check out            ********";
    echo "********       3.The record           ********";
    echo "********       4.Exit                 ********";
    echo "**********************************************";
    echo "Input your choice:";
}

function check_in(){
    echo " Please input your name";
    read name;
    echo " Please input your password";
    read password;
    if test -e /home/userdata.dat
    then 
        while read tem_name tem_password
        do
            #echo "name is $name";
            #echo "Input name is $tem_name";
            #echo "Input password is $tem_password";
            if test "$name" = "$tem_name"
            then
                #echo "Right"; 
                break;
            else
                #echo "False";
                continue;
            fi
        done < /home/userdata.dat
    else 
        echo "No such File,Please Check!";
    fi
    if test "$name" != "$tem_name"
    then 
        echo "No such user! Please check!"; 
    elif test "$password" != "$tem_password"
    then
        echo "Incorrect Password!";
    else
        hour=`date +%k`;
        #echo "$hour";
        if test $hour -ge 8
        then 
            echo "Check in Successfully ^_^ You are late!!";
            echo "$name    ---Late for Work ---Time: `date` " >>/home/check.dat
        else 
            echo "Check in Successfully ^_^ ";
        fi
    fi
}

function check_out(){
    echo " Please input your name";
    read name;
    echo " Please input your password";
    read password;
    if test -e /home/userdata.dat
    then 
        while read temp_name temp_password
        do
            #echo "name is $name";
            #echo "Input name is $tem_name";
            #echo "Input password is $tem_password";
            if test "$name" = "$temp_name"
            then
                #echo "Right"; 
                break;
            else
                #echo "False";
                continue;
            fi
        done < /home/userdata.dat
    else 
        echo "No such File,Please Check!";
    fi
    if test "$name" != "$temp_name"
    then 
        echo "No such user! Please check!"; 
    elif test "$password" != "$temp_password"
    then
        echo "Incorrect Password!";
    else
        hour=`date +%k`;
        #echo "$hour";
        if test $hour -lt 18
        then 
            echo "Check out Successfully ^_^ Leave early!!";
            echo "$name    ---Leave Early   ---Time: `date` " >>/home/check.dat
        else 
            echo "Check out Successfully ^_^ ";
        fi
    fi
}

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

function exit(){
    echo "Thanks for your use ^_^ ";
    isExit="1";
    #exit
}

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


运行结果:

【1】主界面:

【2】签到成功,但是迟到!

【3】用户名错误:

【4】签出成功,但是早退!

【5】查看迟到早退记录:

【6】退出程序,返回主界面:

【7】home文件夹下所有与本程序有关的文件:


题目名称 linux实验-基本指令1 题目关键字 linux实验-基本指令1 题目录入时间 2013-4-1 22:36:02 题目内容 1、root帐号登录,查看/tmp目录,如果/tmp目录下没有子目录myshare,则建立该目录。 2、创建帐号testuser。 3、把myshare目录及其目录下的所有文件和子目录的拥有者该为testuser,工作组改为users。 4、切换至testuser帐号。进入/tmp/myshare目录,采用vim编辑器编写以上程序,程序名称为hello.sh: #!/bin/bash echo "app start" echo -e func (){ echo "hello world!" } func echo -e echo "app end" 5、保存hello.sh后,给予hello.sh拥有者可读、可写和可执行的权限,同组可读可执行,其他人可执行权限。 6、输入./hello.sh,观察程序输出的效果。 7、进入testuser的用户主目录,在这个目录下创建hello.sh的软链接,同时拷贝hello.sh到该目录下并改名为hello.sh.bak,要求拷贝时保留文件属性值。 8、退出testuser帐号,回到root帐号,从/开始查找后缀名为.conf的所有文件,把输出结果重定向到testuser帐号的主目录下的output.txt文件。 9、在上一步操作的.conf文件中找出文件容量最大的和最小那个,并把这两个文件的容量大小输出到output.txt文件中。 10、统计出系统中有多少个用户帐号,把数量输出到output.txt文件中。 11、把output.txt文件转换为windows记事本可正规打开的格式。 12、tar打包压缩testuser帐号主目录下的所有文件。 13、用U盘把上一步打包压缩文件拷贝到U盘上。 14、执行userdel -r testuser,执行rm -fr myshare 题目创建人 邝颖杰 题目注释 把打包压缩文件提交即可。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值