D 的个人博客

但行好事莫问前程

  menu
417 文章
3446695 浏览
4 当前访客
ღゝ◡╹)ノ❤️

Unix/Linux Shell脚本入门实例[00原创]

这是一个简单了 Shell 实例。
下面是它的要求描述:

实验五 UNIX Shell 程序设计


  1. 实验内容:使用UNIX Shell程序设计语言,编写UNIX Shell 应用程序。


  1. 实验要求:按下列要求编写Shell脚本,把所学的各种UNIX命令串联起来。


1.运行Shell脚本,首先显示类似如下的菜单:

What would you like to do:

List Directory **********************1

Change Directory*********************2

Edit file****************************3

Remove file**************************4

Add messages in a file***************5

Search keyword in a specified file***6

Exit Menu****************************7

Enter your choice:


2.输入1:以长格式列出当前目录内容。

3.输入2:改变工作目录到自己指定的目标目录中。(要求改变后可以用选项1查看该目录中的内容)

4.输入3:提示输入一个文件名(该文件可能不存在),然后调用vi编辑器进行编辑。

5.输入4:删除一个指定的文件。

6.输入5: 提示输入一个文件名,然后将用户在键盘上命令行方式下输入的任何信息都添加并保存到这个文件中,直到用户输入一个空行后才完成该项操作。

注意:该操作不能是只输入一行后就停止,要能不断输入多行信息;不能调用vi编辑器来输入。

例如:一个名为”phone”的文件,原有内容为(也可以是无内容的新文件):

This is a phone list”

现在要在其后添加一些电话薄的字段信息,

选择“5”后,显示:Input the filename that you want to add messages:

然后从键盘输入文件名:phone

接着提示:输入要添加的信息,例如用户要添加如下信息:


David Brown (7771) 91101111 [email protected] …………

Emma Redd (6970) 22292222 [email protected] ………..

Tom Swanson (823) 44474444 [email protected] ………..

Ming Li (0871) 3133333 [email protected] ………..

……………. …… …….. ………… ………..


输入完成后,当用户输入一个空行即完成该项添加信息的操作,并退出该选项。该”phone”文件添加后变为:(可以用选项3查看)


This is a phone list

David Brown (7771) 91101111 [email protected] …………

Emma Redd (6970) 22292222 [email protected] ………..

Tom Swanson (823) 44474444 [email protected] ………..

Ming Li (0871) 3133333 [email protected] ………..

……………. …… …….. ………… ………..


7. 输入6:在指定的文件中查找某个关键字(该文件,并显示找到的关键字所在的行。

要求:查找时忽略大小写,并在每个找到的输出行前显示行号,要判断输入的名字是否为文件而非目录,若是文件则进行查找工作,若文件不存在或是目录则显示出错信息。

8.输入7:退出该脚本程序,回到Shell提示符下。

9.在源程序中加入适当的注释。


注意:该菜单要能够反复显示,即各项选完后要能再次返回菜单。


  1. 实验报告:

1、将自己编制并调试好的程序,用到的各种文件放在自己的用户主目录下。

2、抄写自己的Shell源程序(自己加纸写)

3、写出设计思想。

4、写出上机体会与总结

5、回答问题:执行脚本的方式有哪几种,有何区别?



完成报告如下:

实验五 UNIX Shell 程序设计

1. 源代码如下:

#!/bin/bash




C="0"




#


# Do list directory


#


listDirectory()


{


ls -l ./


return


}




#


# Do change the directory


#


changeDirectory()


{


read -p "Input the New Directory Path : " CDI




# check out wether the directory is exist


if [ -d $CDI ]


then


cd $CDI


else


printf "the Directory is not Exist\n"


fi




return


}






#


# Do edit a file


#


editFile()


{


NFILE=""


read -p "Input the File Name to Edit : " NFILE


vi $NFILE


return


}




#


# Do remove a file


#


removeFile()


{


RFILE=""


read -p "Input the File Name to Remove : " RFILE


if [ -f $RFILE ]


then


rm $RFILE


else


printf "the File is not Exist\n"


fi


return


}




#


# Do input messages


#


addMessages()


{


MFILE=""


read -p "Input the File Name to Add Messages : " MFILE



# start to add new messanges


MMSG=""


read -p "Input the Messages : " MMSG


while [ "$MMSG" != "" ]


do


echo $MMSG >> $MFILE


MMSG=""


read -p "Input the Messages : " MMSG


done


printf "End of the file\n"


return


}






#


# Do search Keyword


#


searchKeyword()


{


SFILE=""


read -p "Input the File Name : " SFILE




# if it is a directory's name


if [ -d $SFILE ]


then


printf "the Name input is a Directory\n"


return


fi




# if the file is not exist


if [ ! -f $SFILE ]


then


printf "the File is not Exist\n"


return


fi





KWORD=""


read -p "Please Input the KeyWord : " KWORD


if [ "$KWORD" != "" ]


then


cat $SFILE | grep -n -i $KWORD


fi


return


}






#


# Do display the menu


#


displayMenu()


{


printf " What Would You Like to Do: \n"


printf "List Directory ....................... 1\n"


printf "Change Directory ..................... 2\n"


printf "Edit File ............................ 3\n"


printf "Remove File .......................... 4\n"


printf "Add Messages in a File ............... 5\n"


printf "Search Keyword in a Specified File ... 6\n"


printf "Exit Menu ............................ 7\n"


return


}






getUserInput()


{


read -p "Enter Your Choice : " C


}






while [ $C != "7" ]


do


displayMenu


getUserInput




case $C in


1) printf "\n" ; listDirectory;;


2) printf "\n" ; changeDirectory;;


3) printf "\n" ; editFile;;


4) printf "\n" ; removeFile;;


5) printf "\n" ; addMessages;;


6) printf "\n" ; searchKeyword;;


7) printf "\nThank You for Using, bye-bye\n";;


*) printf "\bWrong Choice\n";;


esac




done




2. 测试输出:

daniel@daniel-laptop:~/Desktop$ sh lab5.sh


What Would You Like to Do:


List Directory ....................... 1


Change Directory ..................... 2


Edit File ............................ 3


Remove File .......................... 4


Add Messages in a File ............... 5


Search Keyword in a Specified File ... 6


Exit Menu ............................ 7


Enter Your Choice : 1




total 19780


-rw-r--r-- 1 daniel daniel 0 2007-07-02 17:31 133888847954 关文武


-rw-r--r-- 1 daniel daniel 2497 2007-07-03 11:07 lab5.sh


-rw-r--r-- 1 daniel daniel 8220990 2007-07-03 13:40 nuoveXT-1.6.tar.gz


-rw-r--r-- 1 daniel daniel 8605556 2007-07-03 13:32 nuoveXT.2.1.tar.bz2


drwxr-xr-x 3 daniel daniel 4096 2007-07-01 20:22 swt-M20070212-1330-gtk-linux-x86


-rw-r--r-- 1 daniel daniel 3176199 2007-07-01 18:14 swt-M20070212-1330-gtk-linux-x86.zip


drwxr-xr-x 3 daniel daniel 4096 2007-02-12 14:51 swt-M20070212-1330-win32-win32-x86


-rwxrwx--- 1 daniel daniel 191871 2007-05-25 00:22 设计模式迷你手册.chm


-rw-r--r-- 1 daniel daniel 647 2007-06-28 11:47 音频ID技术流程


What Would You Like to Do:


List Directory ....................... 1


Change Directory ..................... 2


Edit File ............................ 3


Remove File .......................... 4


Add Messages in a File ............... 5


Search Keyword in a Specified File ... 6


Exit Menu ............................ 7


Enter Your Choice : 2




Input the New Directory Path : /home/daniel


What Would You Like to Do:


List Directory ....................... 1


Change Directory ..................... 2


Edit File ............................ 3


Remove File .......................... 4


Add Messages in a File ............... 5


Search Keyword in a Specified File ... 6


Exit Menu ............................ 7


Enter Your Choice : 1




total 6324


-rwxr--r-- 1 daniel daniel 5090214 2007-05-29 22:08 1.mp3


-rw-r--r-- 1 daniel daniel 274432 2007-04-18 13:02 BattleLANv0.5.exe


drwxr-xr-x 8 daniel daniel 4096 2007-06-30 01:21 Beautify


-rw-r--r-- 1 daniel daniel 196608 2007-06-11 14:53 DB2 UDB 700 .doc


drwxr-xr-x 4 daniel daniel 4096 2007-07-03 17:53 Desktop


drwxr-xr-x 2 daniel daniel 4096 2007-05-03 12:42 Downloads


-rw-r--r-- 1 daniel daniel 1430 2007-05-29 17:12 Instantiations.license


-rw-r--r-- 1 daniel daniel 172032 2006-11-20 22:32 IPMSG2007.exe


-rw-r--r-- 1 daniel daniel 1114 2007-07-01 23:33 ipmsg.log


-rw-r--r-- 1 daniel daniel 365 2007-06-29 22:46 jws_app_shortcut_1183128375333.desktop


drwxr-xr-x 6 daniel daniel 12288 2007-05-03 16:07 LumaQQ


-rw-r--r-- 1 daniel daniel 409 2007-06-20 10:23 MyQQID


drwxr-xr-x 3 mysql daniel 4096 2007-07-01 15:51 mysql


-rw-r--r-- 1 daniel daniel 434176 2007-07-02 00:57 nautilus-debug-log.txt


drwxr-sr-x 11 root root 4096 2007-06-06 23:39 RealPlayer


drwxr-xr-x 4 daniel daniel 4096 2007-06-18 00:30 Software Install Package


-rw-r--r-- 1 daniel daniel 3405 2007-07-01 21:58 velocity.log


-rw-r--r-- 1 daniel daniel 63 2007-06-19 11:34 War3


drwxr-xr-x 11 daniel daniel 4096 2007-07-02 00:45 WarCraft


drwxr-xr-x 10 daniel daniel 4096 2007-06-29 01:20 Work


-rw-r--r-- 1 daniel daniel 198144 2007-05-02 12:19 系分论文.doc


-rw-r--r-- 1 daniel daniel 93 2007-05-30 22:43 飞鸽传书


What Would You Like to Do:


List Directory ....................... 1


Change Directory ..................... 2


Edit File ............................ 3


Remove File .......................... 4


Add Messages in a File ............... 5


Search Keyword in a Specified File ... 6


Exit Menu ............................ 7


Enter Your Choice : 3




Input the File Name to Edit : testfile


What Would You Like to Do:


List Directory ....................... 1


Change Directory ..................... 2


Edit File ............................ 3


Remove File .......................... 4


Add Messages in a File ............... 5


Search Keyword in a Specified File ... 6


Exit Menu ............................ 7


Enter Your Choice : 5




Input the File Name to Add Messages : testfile


Input the Messages : 6


Input the Messages : 7


Input the Messages : 8


Input the Messages : 9


Input the Messages :


End of the file


What Would You Like to Do:


List Directory ....................... 1


Change Directory ..................... 2


Edit File ............................ 3


Remove File .......................... 4


Add Messages in a File ............... 5


Search Keyword in a Specified File ... 6


Exit Menu ............................ 7


Enter Your Choice : 3




Input the File Name to Edit : testfile


What Would You Like to Do:


List Directory ....................... 1


Change Directory ..................... 2


Edit File ............................ 3


Remove File .......................... 4


Add Messages in a File ............... 5


Search Keyword in a Specified File ... 6


Exit Menu ............................ 7


Enter Your Choice : 6




Input the File Name : testfile


Please Input the KeyWord : 7


7:7


What Would You Like to Do:


List Directory ....................... 1


Change Directory ..................... 2


Edit File ............................ 3


Remove File .......................... 4


Add Messages in a File ............... 5


Search Keyword in a Specified File ... 6


Exit Menu ............................ 7


Enter Your Choice : 7




Thank You for Using, bye-bye


3. 设计思路

需求中的1-5比较简单,这里就不赘述了。主要解释一下67点的需求是如何设计与实现的。

6)需求即对应源代码里的addMessage函数,主要原理是利用重定向操作把用户的输入数据追加到文件末尾,主要代码如下:

while [ "$MMSG" != "" ]


do


echo $MMSG >> $MFILE


MMSG=""


read -p "Input the Messages : " MMSG


done

7)该需求对应源代码里的searchKeyword函数,在判断完用户的输入文件名的合法性与文件是否存在之后,使用grep(带参数 -n回应行号 -i忽略大小写)进行查找。主要代码如下:

KWORD=""


read -p "Please Input the KeyWord : " KWORD


if [ "$KWORD" != "" ]


then


cat $SFILE | grep -n -i $KWORD


fi


return


4. 上机体会与总结

1)在写Shell脚本的时候我们应该注意使用重定向操作符,方便完成对文件的I/O操作

2)应该学会使用grep, awk之类的文本操作工具,这样当我们处理大量文本操作的时候效率将大大提高

3) shell脚本的时候要注意Shell脚本的语法和C语言的语法的区别,不要搞混了


5. 执行Shell脚本的方式

执行一个脚本一般有三种方法:
1)
将该脚本权限设置为可执行(chmod +x filename),然后直接用脚本的名字执行,这种方法相当于一个普通的命令。
2) . filename (
点空格filename,这样就默认使用sh执行该脚本,并且不生成子shell,是在当前shell下运行,不用设置可执行权限。
3) sh filename (
生成子shell,在该种模式下常用于调试脚本,如sh -x filename,也不用设置可执行权限)
当然还可以用exec来执行,但一般不用,因为可能会有点危险。