admin管理员组

文章数量:1530518

2024年7月16日发(作者:)

南京信息工程大学

【实验目的】

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

一二三回五六

2 3

4

S

9 10 11 12

16 17 18 19

23 24 25 26

30

[root i^rh 10 0

6 7 8

13 A 15

20 21 22

27 23 29

root]H |

3

、编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果两个 单词相同

则显示“

Match

”,并显示“

End of program

”,如果不同则显示“

End of

program

”。

程序运行过程如下:

(rootHrhlOO root]H cat>ilI

echo —n "word 1:

H

read word 1

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

if test *' $word 1

M

= **$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

read word2

then

echo -n "word 2:

11

if test '*$wordl

M

=

H

Sword2

M

_

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 MENUn

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

directoryn" echo -n

H

Enter a b

t

c or d:

n

read echo

case "Sanswer'

1

in

• ■> ir

/

date

who

• < r f

b)

c)

d)

*)

pwd

鼻•

f I

■ ■ f f

Is

echo "Therp is no

KP

I pct ion : $anfTwcr"

# •

程序运行结果如下:

[rooterhIoo root)# bash menu

COIfHAHD HEHV

a

+

Current date ^nd time

b currently logged in

c * Name of the working directory

d,Contents of the workinq directoryn 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

ts oi the working directoryn 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

[rooterh

LOO

root J # |

6

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

shell

编程代码如下:

[root^rh 100 root |if cat>motiu I echo

pcho echo pcho echo echo echo read

-e

"n

COMMAND HENUXn"

echo

'* a. Currpnt datp and ti

(

nc

hl

'* b,Uaer currently logged in

1

*

"cof thp working directory"

ts of the working directory" '* e*Exit

irtenun

u

-n "Enter a

r

b

r

c

F

d or e:"

answer

while

( do

'"Sannwer

1

* 1=

V ]

case

*'Sanswer" in date ;;

who ; i

H

pwd

;;

»

} j

bl

1 s

c)

echo *'Exit menur'

echo

M

There is no select ion

;;

$answer*'

AHflC

echo

-n "Enter

a

,b

t

c

r

d or e: "*

rpad done echo '*Exit menu ln"

answp

root^rh 100 root JU bash

IDCHU

I

程序运行结果如下:

[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

tu ot th

working directory e^Exit menun

Enter a

r

b c,d or

E

20

5

片:

19 10 : 39

:1 3 (?ST

10

Enter a

F

b

r

c

t

d or e:b

root _ ttyl Nay I9 OS09

root pts/o May 19 0d09

root pts/1 May

oa: 55

EnLe

匚_

a,b,c,d or

e:e

EX i t menu In

[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

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

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

、 在根目录下有四个文件

,用

Shell

编程, 实现

自动创建

m1,m2,m3,m4

四个目录,并将

,,,

四个 文件分别拷

贝到各自相应的目录下

【实验小结】

通过几周的实习,我基本上掌握了

的一些控制语句,测试,

shell的编程,了解和熟悉创建并使用脚本的步骤;

Shell 熟悉bash的控制结构;学会简单的 shell编程;能够通过编程实现一些功能,学会了

Shell变量,Shell的特殊字符等。

但是根据老师的给出的 pdf文件,根

我会去请教老师和同

Linux打下

问题不大,遇到了实在解决不了的问题,

当然了,在实习的过程中肯定会遇到很多的困难,

据上面的说明一步一步的做,

学,把问题解决掉。初步学会了

了坚实的基础!

Linux下Shell的编程,为以后更加系统的学习

本文标签: 输入编程文件用户脚本