实验四 LINUX-Shell编程 下载本文

内容发布更新时间 : 2024/5/4 1:36:29星期一 下面是文章的全部内容请认真阅读。

实验四 LINUX-Shell编程(4学时)

一.实验名称:

Shell 简单编程实验和较复杂的考勤模拟shell程序设计 二.实验目的:

理解Shell程序的设计方法;熟悉Shell程序的编辑、运行、调试方法与过程。 三.实验内容:

考勤模拟Shell程序设计

用shell设计一个模拟考勤程序,实现如下功能选择界面: 1:上班签到 2:下班签出 3:缺勤信息查阅

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

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

如果是下班签出,记录签出信息,如果签出时间小于下午6时,则提示用户早退,并记录该早退信息到check.dat。 如果用户选择确信信息查询,则将check.dat中对应的用户迟到早退的信息查询出来并显示。

用户选择功能执行完,Shell程序继续回到功能选择界面等待下一个用户进行操作。 四.实验要求:

1、掌握Shell程序的编辑、运行、调试方法 2、完成实验内容要求的功能

五.简单SHELL实验:请在vi中逐一编辑并执行以下6个shell脚本程序 1.编写一个简单的回显用户名的shell程序。 #vi dat #!/bin/bash #filename:dat

echo \echo `date`

echo Wish you a lucky day! #chmod +x dat #./dat

2.使用if-then-else语句创建一个根据输入的分数判断是否及格的shell程序。 #vi ak

#!/bin/bash #filename:ak

echo -n \

1

read SCORE

echo \if [ $SCORE -ge 60 ]; then

echo -n \else

echo -n \fi

echo -n \read $GOOUT #chmod +x ak #./ak

3.使用for语句创建简单的shell程序。 #vi mm

#!/bin/bash #filename:mm

for ab in 1 2 3 4 do

echo $ab done

#chmod +x mm #./mm

4.使用while语句创建一个计算1-5的平方的shell程序。 #vi zx

#!/bin/bash #filename:zx int=1

while [ $int -le 5 ] do

2

sq=`expr $int \\* $int` echo $sq

int=`expr $int + 1` done

echo \#chmod +x zx #./zx

5.使用while语句创建一个根据输入的数值求累加和(1+2+3+4+…+n)的shell程序。 #vi sum #!/bin/bash #filename:sum

echo -n \read NUM number=0 sum=0

while [ $number -le $NUM ] do

echo number echo \

number=`expr $number + 1 ` echo sum echo \

sum=` expr $sum + $number ` done echo

#chmod +x sum #./sum

3

六.较复杂SHELL实验(使用VI编辑 下面代码)

# vi testshell

#! /bin/bash #filename:shelltest exsig=0 while true; do echo \

echo \欢迎使用本系统----\ echo \上班签到\ echo \下班签出\ echo \考勤信息查询\ echo \退出系统\ echo \ echo \

echo \请输入你的选项:\ read choice case $choice in

1)echo \请输入你的名字:\ read name

echo \请输入你的密码:\ read password

if test -r /home/user/userinfo.dat then

while read fname fpassword do

4

echo \ echo \

if test \ then break fi

done < /home/user/userinfo.dat else

echo System Error:userinfo.dat does not exist! fi

if test \ then

echo \不存在该用户!\

elif test \ then

echo \密码不正确!\ else

hour=`date +%H` if test \ then

echo \你迟到了!\

echo \上班迟到---日期:`date`\ else

echo \早上好,$name!\ fi fi ;;

2)echo \请输入你的名字:\ read name

echo \请输入你的密码:\ read password

if test -r /home/user/userinfo.dat then

while read fname fpassword do

if test \ then break fi

done < /home/user/userinfo.dat else

echo System Error:userinfo.dat does not exist! fi

if test \ then

echo \不存在该用户!\

5