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、撰寫實驗報告
[實驗方法 ]
1、Shell程式的編輯可使用vi,emacs等Linux下的各種文字編輯器。本課程實驗可使用Red Hat Linux9.0下的Text Editor 。
2、Shell程式的執行有兩種方式:sh[Shell程式名]
例:設Shell程式名稱為test.sh,則可以通過sh test.sh 或./test.sh。但是要注意在使用./ [Shell程式名]時必須確保對Shell程式具有可執行許可權。
3、Shell程式的除錯可以通過建立多個工作區互動進行。
新建檔案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資料夾下所有與本程式有關的檔案: