文档视界 最新最全的文档下载
当前位置:文档视界 › Shell编程实验报告

Shell编程实验报告

Shell编程实验报告
Shell编程实验报告

南京信息工程大学实验(实习)报告【实验目的】

1、了解和熟悉创建并使用脚本的步骤。

2、熟悉bash的控制结构。

3、学会简单的shell编程。

【实验内容】

1、创建一个简单的列目录和日期的shell脚本并运行之。

步骤:

⑴输入下列命令,创建一个新文件:

cat >n ew_script

⑵输入下列行:

echo Your files are ”

ls

echo today is ”

date

按回车键将光标移到一个新行,按Ctrl+D键保存并退出。

⑶检查文件内容,确保它是正确的:

cat n ew_script

⑷运行月脚本,输入它的文件名:

n ew_script

该脚本不运行。

⑸输入下列命令,显示文件的权限:

ls - l new _script

权限表明该文件不是可执行。要通过简单调用文件名来运行脚本,必须有权限。

⑹输入下列命令,使new_script变成可执行文件。

chmod +x n ew_script

⑺要查看新的权限,输入:

ls - l

现在拥有文件的读、写和执行权限。

⑻输入新脚本的名字以执行它:

n ew_script

所有输入到文件的命令都执行,并输出到屏幕上。

⑼如果接收到错误信息,比如:

comma nd not found

输入下列命令:

./n ew_script

该命令行通知shell到哪里寻找shell脚本new_script,即您的当前目录“.” 程序运行过程如下:一

2、用Shell 语言编制一 Shell 程序,该程序在用户输入年、月之后,自动打印 数出该年该月的日历。

程序运行过程如下:

[root (?rhl00 root 1H cat>timee echo M Please input tl )e month: read nitonth echo R Pl ease input tl )c ycai': M read year

cal $fnonth $year [root(3rh 100 root] H bash times PloatJR input thp month: 9 PlpaHR input, thp ypar: 2012 九片 2012 0 一二三回五六

6 7 8 13 \A 15 20 21 22 27 23 29 root]H |

3、编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果两个 单词相同则显示“ Match ”,并显示“ End of program ”,如果不同则显示“ End of

program ”。

程序运行过程如下: 2 3 4 S 9 10 11 12 16 17 18 19 23 24 25 26 30 [root i^rh 10 0

(rootHrhlOO root]H cat>il I

echo —n "word 1:H read word 1

echo —n "word 2 : read wot'd 2

if test *' $word 1M = **$word2**

then

echo "Match"

f i

echo *' End of program^ **

[root^rhl00 root]H bash if 1

word 1:good

word 2:good

Hatch

End of pirogratn.

(root^rh100 root]# bash if 1

word 1 : good

word 2:bad

End ot progratn.

[rootyrhICO root]# |

4、修改上述程序,编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果两个单词相同显示“ Match” ,不同则显示“ Not match ”,最后显示“ End of program ”。<编程提示>请使用if , then, else 控制结构。

程序运行过程如下:

I[root@rhl00 root]|| cat>if2

echo —n u word 1:11

read word 1

echo -n "word 2:11

read word2

if test '*$wordl M = H Sword2M_ then

echo "Match"

el se echo '*Kot match'* f i echo **End of program," [root^rh100 root]tf bash if2 word i:good word 2:good

Match

End of program*

(root feirh 100 root ) || ba^h it 2

word 1:

word 2:no

Hot match

End ot program?

[root^rhioo root]0 |

5、编程使用case结构创建一个简单的菜单,屏幕显示菜单:

a. Current date and time

b. User curre ntly logged in

c. Name of the worki ng directory

d. Contents of the worki ng directory

En ter a,b,c or d:

根据用户输入选项做相应操作。

shell编程代码如下:

echo -e '*\n COHMAIW MENU\n M echo " Current date and t line" echo 11 b^User currently

logged in" echo " c,Name of the working directory,i echo '* d+contents of the working directory\n" echo -n H Enter a b t c or d:n read echo

case "Sanswer'1 in

date

? ■> ir

/

b)

who

? < r f

c)

pwd

鼻?

f I

d)

Is

■ ■ f f

*)

echo "Therp is no KP I pct ion : $anfTwcr"

# ?

程序运行结果如下:

[rooterh I oo root)# bash menu

COIfHAHD HEHV

a+Current date ^nd time

b 4.Uuer currently logged in

c * Name of the working directory

d,Contents of the work inq directory\n Ente上a f b f c or d:a

三亏貝19 10:03:16 CST 2010

[root^rh L 00 root)# bash mpnu

conriAHD nmu

a * Current date ^nd time

b * User curieiit ly logqed in

c ^Naiwe of the working directory

d.Contents oi the working directory\n Enter a r b t c or d:b

root root root ttyi May 19 08:09 pts/0 May 19 08:09 pts/1 May 19 08:55

[rooter h LOO root J # |

6修改上题,使用户可以连续选择直到想退出时才退出

shell 编程代码如下:

-e "\n COMMAND HENUXn" '* a. Currpnt datp and ti (nc hl '* b,Uaer currently logged in 1* "cof thp working directory" d.contents of the working directory" '* e*Exit irtenu\n u -n "Enter a r b r c F d or e:" answer

程序运行结果如下:

[root^rhICO root)# bash menul

COMI 1A ND ME HU

a .Current date and tiiw

b ?User currently logged in

c + Name of the working directory

d.Contcntu ot th 芒 working directory e^Exit menu\n Enter a r b c,d or E :

曰 三 5片:19 10 : 39 :1 3 (?ST 20 10 Enter a F b r c t d or e:b root _ ttyl Nay I9 OS 09 root pts/o May 19 0d 09 root pts/1 May 沖 oa: 55

EnLe 匚_a,b,c,d or e:e

EX i t menu I\n [roat@rh100 root]# | 7、编程使用select 结构生成一个菜单如下:

1) apple 3)blueberry 5)orange 7)STOP

2) banana 4)kiwi 6)watermel on

Choose your favorite fruit from these possibilities:

用户输入所选项,如1显示:

You chose apple as you favorite.

That is choice nu mber 1.

shell 编程代码及运行结果如下:

[root^rh 100 root |if cat>motiu I echo pcho echo pcho echo echo echo read echo while ( do '"Sannwer 1* 1= V ]

case H ?

bl c) *'Sanswer" in date ;; who ; i pwd ;; 1 s } j echo *'Exit menur' ;;

echo M There is no select ion AHflC -n "Enter a ,b t c r d or e: "* answp 『 $answer*' echo rpad done echo '*Exit menu l\n" \ root^rh 100 root JU bash IDCHU I

8、上机编写如下代码,分析下面的程序,简要说明整体功能,并解释每条语句。#!/bi n/sh

val=1

while (test $val -It 6) do touch file$val date>>file$val val='expr $val + 1' done

9、编写一段shell程序,根据执行时候获取的当前时间显示出不同的问候信息。

10、用Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到/dev目录下

11、在根目录下有四个文件m1.txt,m2.txt,m3.txt,m4.txt,用Shell编程,实现自动创建m1,m2,m3,m4四个目录,并将m1.txt,m2.txt,m3.txt,m4.txt 四个文件分别拷贝到各自相应的目录下

【实验小结】

通过几周的实习,我基本上掌握了shell的编程,了解和熟悉创建并使用脚本的步骤;

熟悉bash的控制结构;学会简单的shell编程;能够通过编程实现一些功能,学会了Shell 的一些控制语句,测试,Shell变量,Shell的特殊字符等。

当然了,在实习的过程中肯定会遇到很多的困难,但是根据老师的给出的pdf文件,根

据上面的说明一步一步的做,问题不大,遇到了实在解决不了的问题,我会去请教老师和同

学,把问题解决掉。初步学会了Linux下Shell的编程,为以后更加系统的学习Linux打下

了坚实的基础!

相关文档