Linux原理及应用——专题0:Shell实例.ppt_第1页
Linux原理及应用——专题0:Shell实例.ppt_第2页
Linux原理及应用——专题0:Shell实例.ppt_第3页
Linux原理及应用——专题0:Shell实例.ppt_第4页
Linux原理及应用——专题0:Shell实例.ppt_第5页
已阅读5页,还剩44页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、LINUX原理及应用,武汉大学计算机学院 李文海 ,shell程序的应用,shell程序的定义 命令文件:包含一串简单命令 shell程序:包含复杂的命令组合 何时使用shell程序设计语言 当问题的解决需要使用多个标准命令时,可以使用shell命令文件; 如果处理的问题比较复杂,可以使用shell程序设计语言,shell应用实例,假设用户要编写一个菜单驱动应用程序以管理UNIX书籍。用户希望能够更新图书列表,了解指定的图书是否在库中,是否借出,谁以及何时借出。 当然,这是一个典型的数据库应用,但是我们的目标是练习使用一些UNIX命令,给用户一些如何把命令组成有用程序的感受。,UNIX图书库程

2、序-ULIB层次图,ULIB,EDIT,edit菜单,REPORT,report菜单,EDIT,增加一条记录,DISPLAY,显示一条记录,UPDATE,更新记录状态,DELETE,删除一条记录,REPORT_NO,生成报表,主菜单,ULIB程序源代码,$ cat -n ULIB 1 # UNIX library 2 # ULIB: This program is the main driver for the UNIX 3 # library application program. It shows a brief 4 # startup message and then displays

3、 the main menu. 5 # It invokes the appropriate program according to the 6 # user selection. 7 BOLD=tput smso #将粗体显示的终端代码保存在BOLD中 8 NORMAL=tput rmso #将正常显示的终端代码保存在NORMAL中 9 export BOLD NORMAL # 使上述变量成为全局变量 10 # show the title and a brief message before showing,ULIB程序源代码,11 # the main menu 12 tput cle

4、ar #清屏 13 tput cup 5 15 #置光标于5行15列 14 echo ”$BOLDSuper Duper UNIX Library” # 以粗体显示 15 tput cup 12 10 #置光标于12行10列 16 echo ”$NORMALThis is the UNIX library application” 17 tput cup 14 10 ; #置光标于14行10列 18 echo ”Please enter any key to continue._bc” 19 read answer # 读用户输入 20 error_flag=0 #将error_flag初始化

5、为0,ULIB程序源代码,21 while true # 一直循环 22 do 23 if $error_flag -eq 0 #检查错误 24 then 25 tput clear # 清屏 26 tput cup 5 10 27 echo ”UNIX Library - $BOLDMAIN MENU$NORMAL” 28 tput cup 7 20;echo ”0:$BOLDEXIT$NORMAL this program” 29 tput cup 9 20 ; echo ”1: $BOLDEDIT$NORMAL Menu” 30 tput cup 11 20 ; echo ”2: $BO

6、LDREPORTS$NORMAL Menu” 31 error_flag=0 # 重置error_flag 32 fi,ULIB程序源代码,33 tput cup 13 10 ; echo ”Enter your choice_bc” 34 read choice #读用户选择 35 # 36 # case construct for checking the user selection 37 # 38 case $choice in #检查输入 39 0 ) tput clear ; exit 0 ; 40 1 ) EDIT ; # 调用EDIT程序 41 2 ) REPORTS ; #

7、调用REPORT程序 42 * ) ERROR 20 10 # 调用ERROR程序 43 tput cup 20 1 ; tput ed #清除从光标处到屏幕底部的部分 44 error_flag=1 ; # 设置错误标志 45 esac 46 done 47 exit 0 # exit the program,ULIB程序初始屏幕,Super Duper UNIX Library This is the UNIX library application Please enter any key to continue._,ULIB程序主菜单屏幕 UNIX Library MAIN MENU,

8、0:EXIT this program 1:EDIT Menu 2:REPORTS Menu Enter your choice_,ULIB程序的错误信息屏幕UNIX Library MAIN MENU,0:EXIT this program 1:EDIT Menu 2:REPORTS Menu Enter your choice_6 Wrong Input. Try again. Press any key to continue._,ERROR程序源代码,$ cat -n ERROR 1 # 2 # ERROR: This program displays an error message

9、 and 3 # waits for user input to continue. It displays the 4 # message at the specified row and column. 5 # 6 tput cup $1 $2 # 置光标位置 7 echo ”Wrong Input. Try again.” # 显示错误信息 8 echo ”Press any key to continue._bc”# 显示提示 9 read answer # 读用户输入 10 exit 0 $_,EDIT程序源代码,$ cat -n EDIT 1# 2# UNIX library 3#

10、 EDIT: This program is the main driver for the EDIT 4# program.It shows the EDIT menu and invokes the 5# appropriate program according to the user selection. 6# 7 error_flag=0 # 初始化error_flag,EDIT程序源代码,8 while true 9 do 10 if $error_flag -eq 0 # 检查错误代码 11 then 12 tput clear ; tput cup 5 10 13 echo ”

11、UNIX Library - $BOLDEDIT MENU$NORMAL” 14 tput cup 7 20 15 echo ”0: $BOLDRETURN$NORMALTo the Main Menu” 16 tput cup 9 20; echo ”1: $BOLDADD$NORMAL” 17 tput cup 11 20; echo ”2: $BOLDUPDATE STATUS$NORMAL” 18 tput cup 13 20; echo ”3: $BOLDDISPLAYNORMAL” 19 tput cup 15 20; echo ”4: $BOLDDELETE$NORMAL” 20

12、 fi 21 error_flag=0 # 重置错误标志 22 tput cup 17 10; echo ”Enter your choice_bc” 23 read choice # 读用户选择,EDIT程序源代码,24 # 25 # case construct for checking the user selection 26 # 27 case $choice in # 检查用户输入 28 0) exit 0; # 返回主菜单 29 1) ADD; # 调用ADD程序 30 2) UPDATE; # 调用UPDATE程序 31 3) DISPLAY; # 调用DISPLAY程序 32

13、 4) DELETE; # 调用DELETE程序 33 *) ERROR 20 10 # 调用ERROR程序 34 tput cup 20 1; tput ed #清除从光标处到屏幕底部的部分 35 error_flag=1; # 设置error_flag 36 esac 37 done 38 exit 0 # exit the program $_,EDIT菜单:ULIX Library EDIT MENU,0: RETURN To the Main Menu l: ADD 2: UPDATE STATUS 3:DISPLAY 4:DELETE Enter your choice_,ADD程

14、序源代码,假设记录所有UNIX图书信息的库文件名为ULIB_FILE;同时还假设在ULIB_FILE中每本书按照下述记录格式保存信息: *Title(书名):例如UNIX Unbound *Author(作者):例如Afzal Amir *Category(种类):例如Textbook 假设有三种有效的类别: *System books(系统书):简写为sys *Reference books(参考书):简写为ref *Textbooks(教科书):简写为tb *Status(状态):in表明在库中,out表明借走。 *Borrowers name(借阅者姓名):若状态为in则该字段为空,否则

15、该字段为借阅者姓名。 *Date(日期):若状态为in则该字段为空,否则该字段为借出的时间。,ADD程序源代码,$ cat -n ADD 1 # UNIX library 2 # ADD: This program adds a record to the library 3 # file(ULIB_FILE).It asks the title,author,and 4 # category of the book. After adding the 5 # information to the ULIB_FILE file,it prompts 6 # the user for the n

16、ext record. 7 answer=y # 初始化answer为yes,ADD程序源代码,8 while ”$answer” = y 9 do 10 tput clear 11 tput cup 5 10 ; echo ”UNIX Library - $BOLDADD MODE” 12 echo ”$NORMAL 13 tput cup 7 23 ; echo ”Title:” 14 tput cup 9 22 ; echo ”Author:” 15 tput cup 11 20 ; echo ”Category:” 16 tput cup 12 20;echo”sys;system,r

17、ef:reference,tb: textbook” 17 tput cup 7 30 ; read title 18 tput cup 9 30 ; read author 19 tput cup 11 30 ; read category 20 status=in # 设置状态为in 21echo ”$title:$author;$category:$status:$bname:$date” ULIB_FILE 22 tput cup 14 10 ; echo ”Any more to add? (Y)es or (N)o_bc”,ADD程序源代码,23 read answer 24 ca

18、se $answer in 25 Yy* ) answer=y ; #以Y或y开头的字表示yes 26 * ) answer=n ; #其他字表示no 27 esac 28 done 29 exit 0 $_,ADD程序产生的屏幕,UNIX Library - ADD MODE Title: Author: Category: sys:system, ref: reference, tb: textbook,信息输入且记录已保存后的屏幕,UNIX Library - ADD MODE Title:UNIX Unbounded Author:Afzal Amir Category:tb sys:

19、system,ref:reference,tb:textbook Any more to add? (Y)es or(N)o_ 若刚才的记录是文件中惟一的记录,ULIB_FILE文件的内容如下: UNIX Unbounded:Afzal Amir:tb:in: 行尾的两个冒号是变量bname和date的占位符。,DISPLAY程序源代码,$ cat -n DISPLAY 1 # UNIX library 2 # DISPLAY: This program displays a specified 3 # record from the ULIB_FILE. It asks the 4 # Au

20、thor/Title of the book, and displays the 5 # specified book is not found in the file. 6 # 7 OLD_IFS=”$IFS” # 保存老分隔符 8 answer=y # 初始化answer为y,DISPLAY程序源代码,9 while ”$answer” = y 10 do 11 tput clear;tput cup 3 5;echo ”Enter the Author/Title_bc” 12 read response 13 grep -i ”$response” ULIB_FILETEMP #查找指

21、定书籍 14 if -s TEMP # 若找到 15 then 16 IFS=”:” # 设置分隔符为冒号 17 read title author category status bname date TEMP 18 tput cup 5 10 19 echo ”UNIX Library - $BOLDDISPLAY MODE$NORMAL” 20 tput cup 7 23 ; echo ”Title: $title” 21 tput cup 8 22 ; echo ”Author: $author”,DISPLAY程序源代码,22 case $category in # check th

22、e category 23 TtBb) word=textbook ; 24 SsYySs) word=system ; 25 RrEeFf) word=reference ; 26 *) word=undefined ; 27 esac 28 tput cup 9 20 ; echo ”Category: $word” 29 tput cup 10 22 ; echo ”Status:$status” 30 if ”$status” = ”out” # 若借出 31 then # 显示其他信息 32 tput cup 11 14 ; echo ”Checked out by: $bname”

23、 33 tput cup 12 24 ; echo ”Date: $date” 34 fi,DISPLAY程序源代码,35 else # 未找到书 36 tput cup 7 10 ; echo ”$response not found” 37 fi 38 tput cup 15 10 ; echo ”Any more to look for? (Y)es or (N)o_bc 39 read answer # 读用户回答 40 case $answer in 41 Yy*) answer=y ; 42 *) answer=n ; 43 esac 44 done 45 IFS=”$OLD_IF

24、S” # 恢复分隔符旧值 46 exit 0 $_,DISPLAY程序提示信息屏幕,Enter the Author/Title_,DISPLAY程序的显示屏幕,UNIX Library DISPLAY MODE Title: UNIX Unbounded Author: Afzal Amir Category:textbook Status: in Any more to look for? (Y)es or (N)o_,DISPLAY程序的显示错误信息屏幕,Enter the Author/TitleXYZ XYZ not found Any more to look for? (Y)es

25、 or (N)o_,UPDATE程序源代码,$ cat -n UPDATE 1 # UNIX library 2 # UPDATE: This program updates the status of a 3 # specified book. It asks the Author/Title of the 4 # book,and changes the status of the specified 5 # book from in (checked in) to out (checked out), 6 # or from out to in.If the book is not fo

26、und in 7 # the file, an error message is displayed. 8 OLD_IFS=”$IFS” # 保存分隔符 9 answer=y,UPDATE程序源代码,10 while ”$answer” = y 11 do 12 new_status= ; new_bname= ; new_date= # 说明空变量 13 tput clear # 清屏 14 tput cup 3 5 ; echo ”Enter the Author/Title_bc” 15 read response 16 grep -i ”$response” ULIB_FILETTEM

27、P #查找指定书 17 if -s TTEMP # 找到 18 then 19 IFS=”:” #设置IFS为冒号 20 read title author category status bname date TTEMP 21 tput cup 5 10 22 echo ”UNIX Library - $BOLDUPDATE STATUS MODE$NORMAL” 23 tput cup 7 23 ; echo ”Title: $title” 24 tput cup 8 22 ; echo ”Author: $author”,UPDATE程序源代码,25 case $category in

28、26 TtBb) word=textbook ; 27 SsYySs) word=system ; 28 RrEeFf) word=reference ; 29 *) word=undefined ; 30 esac 31 tput cup 9 20 ; echo ”Category: $word”#显示类别 32 tput cup 10 22 ; echo ”Status: $status” 33 if ”$status” = ”in” 34 then 35 new_status=out 36 tput cup 11 18 ; echo ”New status: $new_status” 3

29、7 tput cup 12 14 ; echo ”Checked out by: _bc” 38 read new_bname 39 new_date=date+%D 40 tput cup 13 24 ; echo ”Date: $new_date”,UPDATE程序源代码,41 else 42 new_status=in 43 tput cup 11 14 ; echo ”Checked out by: $bname” 44 tput cup 12 24 ; echo ”Date: $date” 45 tput cup 15 18 ; echo ”New status: $new_stat

30、us” 46 fi 47 grep -iv “$title:$author:$category:$status:$bname: $date” ULIB_FILETEMP #挑出与指定模式不匹配的行 48 cp TEMP ULIB_FILE 49 echo “$title:$author:$category:$new_status: $new_bname: $new_dateULIB_FILE 50 else 51 tput cup 7 10 ; echo ”$response not found” 52 fi,UPDATE程序源代码,53 tput cup 16 10 ; echo ”Any

31、more to update? (Y)es or (N)o_bc 54 read answer #读用户输入 55 case $answer in 56 Yy*) answer=y ; 57 *) answer=n ; 58 esac 59 done 60 IFS=”$OLD_IFS” #恢复分隔符旧值 61 rm TEMP TTEMP 62 exit 0 $_,UPDATE程序显示屏幕,UNIX Library - UPDATE STATDS MODE Title: UNIX Unbounded Author: Afzal Amir Category: textbook Status: in

32、 New status: out Checked out by: Steve Fraser Date: 12/12/05 Any more to update?(Y)es or (N)o_,DELETE程序源代码,$ cat -n DELETE 1 # UNIX library 2 # DELETE: This program deletes a specified record 3 # from the ULIB_FILE. It asks the Author/Title of 4 # the book, and displays the specified book, and 5 # d

33、eletes it after confirmation, or shows an error 6 # message if the book is not found in the file. 7 # 8 OLD_IFS=”$IFS” # save the IFS setting 9 answer=y,DELETE程序源代码,10 while ”$answer” = y 11 do 12 tput clear ; tput cup 3 5 ; echo ”Enter the Author/Title_bc” 13 read response 14 grep -i ”$response” UL

34、IB_FILETEMP #查找指定书籍 15 if -s TEMP #找到 16 then 17 IFS=”:” 18 read title author category status bname date TEMP 19 tput cup 5 10 20 echo ”UNIX Library - $BOLDDELETE MODE$NORMAL” 21 tput cup 7 23 ; echo ”Title: $title” 22 tput cup 8 22 ; echo ”Author: $author”,DELETE程序源代码,23 case $category in 24 TtBb)

35、word=textbook ; 25 SsYySs) word=system ; 26 RrEeFf) word=reference ; 27 *) word=undefined ; 28 esac 29 tput cup 9 20 ; echo ”Category: $word” 30 tput cup 10 22 ; echo ”Status: $status” 31 if ”$status” = ”out” 32 then 33 tput cup 11 14 ; echo ”Checked out by: $bname” 34 tput cup 12 24 ; echo ”Date: $

36、date” 35 fi,DELETE程序源代码,36 tput cup 13 20 ; echo ”Delete this book?(Y)es or (N)o _bc” 37 read answer 38 if $answer = y -o $answer = Y 39 then 40 grep -iv “$title:$author:$category:$status: $bname:$date” ULIB_FILETEMP #挑出与指定模式不匹配的行 41 mv TEMP ULIB_FILE 42 fi 43 else # if book not found 44 tput cup 7

37、10 ; echo ”response not found” 45 fi,DELETE程序源代码,46 tput cup 15 10 ; echo ”Any more to delete? (Y)es or (N)o _bc 47 read answer 48 case $answer in 49 Yy*) answer=y ; 50 *) answer=n ; 51 esac 52 done 53 IFS=”$OLD_IFS” 54 rm TEMP 55 exit 0 $_,DELETE程序的显示,UNIX Library DELETE MODE Title: UNIX Unbounded

38、Author: Afzal Amir Category: textbook Status: out Checked out by: Steve Fraser Date: 12/12/05 Delete this book? (Y)es or (N)oY Any more to delete? (Y)es or (N)o_,REPORTS程序源代码,$ cat -n REPORTS 1 # UNIX library 2 # REPORTS: This program is the main driver for 3 # the REPORTS menu. It shows the reports

39、 menu and 4 # invokes the appropriate program according to 5 # the user selection. 6 # 7 error_flag=0 #初始化错误标志,REPORTS程序源代码,8 while true 9 do 10 if $error_flag -eq 0 11 then 12 tput clear ; tput cup 5 10 13 echo ”UNIX Library - $BOLDREPORTS MENU $NORMAL” 14 tput cup 7 20 15 echo ”0: $BOLDRETURN$NORM

40、ALTo the Main Menu” 16 tput cup 9 20 ; echo ”1: Sorted by $BOLDTITLES $NORMAL” 17 tput cup 11 20 ; echo ”2: Sorted by $BOLDAUTHOR $NORMAL” 18 tput cup 13 20 ; echo ”3: Sorted by $BOLDCATEGORY $NORMAL” 19 fi 20 error_flag=0 21 tput cup 17 10 ; echo ”Enter your choice _bc”,REPORTS程序源代码,22 read choice

41、23 # 24 # case construct for checking the user selection 25 # 26 case $choice in 27 0) exit 0; # 返回 main menu 28 1) REPORT_NO 1; #调用REPORT_NO程序,参数为1 29 2) REPORT_NO 2; #调用REPORT_NO程序,参数为2 30 3) REPORT_NO 3; #调用REPORT_NO程序,参数为3 31 *) ERROR 20 10 #调用error程序 32 tput cup 20 1 ; tput ed #从光标处清除屏幕 33 error_flag=1; #设置错误标志 34 esac 35 done 36 exit 0 $_,REPORTS菜单,UNIX Library REPORTS MENU 0:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论