linux脚本程序实验,linux实验3 shell编程

本文介绍了一个网络操作系统实验的预习报告,重点介绍了实验的目的、内容和操作步骤,包括使用通配符、重定向、管道、shell脚本等关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**

《网络操作系统》实验预习报告

**

一、实验目的

1.为文件扩展名使用通配符

2.标准输入、标准输出和标准错误的重定向

3.使用管道将一个进程的输出作为输入提供给另一个进程。

4.执行命令分组和行继续

5.编写shell 脚本。

二、实验内容

1、管道(pipe-line)的使用

执行格式: command1|command2

功能:将command1的执行结果送到command2 作为输入

2、标准输入控制

执行格式: command-line

3、标准输出控制

执行格式一: command>filename

功能:将command的执行结果送至指定的filename中

执行格式二: command>!filename

功能:同上,若filename文件已存在,则强迫重写

执行格式三:command>>filename

功能:将command 的执行结果,附加(append)到filename

4、shell脚本的调试方法:

(1)输入重定向到shell脚本

格式:$ bash < 脚本名

(2)以脚本名作为bash参数

格式:$ bash 脚本名 [参数]

(3)在提示符下直接执行

方法:先利用chmod命令将脚本文件设置为可执行权限;然后,将该脚本所在的目录添加到命令搜索路径中

(4)将shell脚本的权限设置为可执行,然后在提示符下直接执行

方法:

先利用chmod命令将脚本文件设置为可执行权限

三、预习心得

**

《网络操作系统》实验报告

**

一、实验目的

1.为文件扩展名使用通配符

2.标准输入、标准输出和标准错误的重定向

3.使用管道将一个进程的输出作为输入提供给另一个进程。

4.执行命令分组和行继续

5.编写shell 脚本

二、实验内容

1、管道(pipe-line)的使用

执行格式: command1|command2

功能:将command1的执行结果送到command2 作为输入

2、标准输入控制

执行格式: command-line

3、标准输出控制

执行格式一: command>filename

功能:将command的执行结果送至指定的filename中

执行格式二: command>!filename

功能:同上,若filename文件已存在,则强迫重写

执行格式三:command>>filename

功能:将command 的执行结果,附加(append)到filename

4、shell脚本的调试方法:

(1)输入重定向到shell脚本

格式:$ bash < 脚本名

(2)以脚本名作为bash参数

格式:$ bash 脚本名 [参数]

(3)在提示符下直接执行

方法:先利用chmod命令将脚本文件设置为可执行权限;然后,将该脚本所在的目录添加到命令搜索路径中

(4)将shell脚本的权限设置为可执行,然后在提示符下直接执行

方法:

先利用chmod命令将脚本文件设置为可执行权限

三、实验操作

1、通配符的使用

(1)进入/etc目录并且将此处所有文件以列表显示

[goutong@localhost ~]$ cd /etc

[goutong@localhost etc]$ ls

c262c472771d2d2aa743b57bfb2636eb.png

(2)要求用带有通配符的ls命令来显示文件名,实现:

-以conf结束

*[goutong@localhost etc]$ ls -d conf

a16cc322207be8f1561ea43d015d96e3.png

-以d 或D开始

[goutong@localhost etc]$ ls -d [dD]*

ef7c67af4dc5edd52e94f7a88d23d727.png

-在文件名的第五个位置包括一个o

[goutong@localhost etc]$ ls -d ???o*

c2bb4bf90df8b936b5622e3554288240.png

-包含单词tab

2d43c77d56048ebfc65738e0f257ce43.png

18117d6905e631cd260d541a7654c5dd.png

-以数字结尾

[goutong@localhost etc]$ ls -d *[0-9]

9fd30a4c1ac82de0958a3d7d8ec4fffb.png

-不以数字结尾

[goutong@localhost etc]$ ls -d *[!0-9]

183a7254808255cfd126bceef40c3589.png

(3)如果你执行命令ls -d ?[!y][e-f]将会发生什么情况?能与之匹配的最短文件名是什么?执行这些命令来验证你的答案。

[goutong@localhost etc]$ ls -d ?[!y][e-f]

5fea58af2cfdabde0d9156f5faa1c262.png

2、重定向

(1)使用cat命令和指令来创建一个包含几行文本名为junk的文件。

[goutong@localhost ~]$ cat > junk

e7629dd1a9f52b2db22c749d460160b0.png

[goutong@localhost ~]$ cat junk

484a3dbc98ccb0768ffc0b6d25a2086a.png

(2)使用重定向junk文件中追加几行文本。然后查看junk文件的内容并且检查你刚刚所创建的文本是否都存在。

[goutong@localhost ~]$ cat >> junk

[goutong@localhost ~]$ cat junk

543fa0b7b5a60fd66873d891e2ed0d1e.png

3、管道

(1)统计你当前目录所有文件的数目。使用管道,而不是手动地统计所有文件

[goutong@localhost ~]$ ls | wc -l

fdc2bcdb7b3bfd239e2fe7c06bc79c47.png

(2)ls > tempfile ; wc -l tempfile是否和你刚刚使用的管道功能相同?为什么或者为什么不?

不同,重定向如果没有tempfile会重新创建一个

e4ed88cf616d169c32f08720fef3706e.png

(3)将多个用户文件联合为一个大文件。

[goutong@localhost ~]$ cat > file1

aaaaaaaaaa[goutong@localhost ~]$

[goutong@localhost ~]$ cat > file2

bbbbbbbbb

[goutong@localhost ~]$ cat > file3

ccccccccccccccc

[goutong@localhost ~]$ cat file1 file2 file3 > file4

[goutong@localhost ~]$ cat file4

6206249d3153487ec2db92b81a9e2faf.png

d920d3fef1d572a68ac6859d52a83a3d.png

4、shell变量

(1)显示由你当前进程环境所定义的所有变量。并且显示出当前输出的所有变量。

[goutong@localhost ~]$ env

ae37956eae338b01952812972eea391b.png

(2)创建一个变量x并初始化值为10。检查这个变量的值。再次,显示当前进程环境的所有变量。

52dbdbe384d47a093b079c0c9a6493c1.png

1353ce06a093219ce40cbdbcc94e1abe.png

2bcf1dbe43c298f17d20b1f006b26cc6.png

(3)创建一个subshell.查看变量x在subshell下的值是什么?

[goutong@localhost ~]$ vi subshell

[goutong@localhost ~]$ bash subshell

输出为空

bf31e9625b087ebe377f549b70fbf5f7.png

(4)将x设置为500并返回到父进程。当前x的值是什么?为什么?

[user1@localhost goutong]$ x=500

[user1@localhost goutong]$ exit

exit

[goutong@localhost ~]$ echo $x

6329e17fba02a237576a8eeeb435bea6.png

输出为空

(5)确保子进程继承了x。如何实现?

[goutong@localhost ~]$ export x

[goutong@localhost ~]$ env | less

[goutong@localhost ~]$ bash

[goutong@localhost ~]$ echo $x

[goutong@localhost ~]$ exit

exit

4e5e14d8311c27e95f57883571a30e52.png

5、建立下面的脚本,运行并分析输出结果,并给出代码注释。

(1)ex1

#!/bin/bash

#shell special character

echo "current directory is pwd"

echo “current directory is ‘pwd’”

echo "home directory is H

O

M

E

"

e

c

h

o

"

f

i

l

e

.

?

"

t

o

d

a

y

=

d

a

t

e

e

c

h

o

T

o

d

a

y

i

s

:

HOME" echo "file*.?" today=`date` echo Today is :HOME"echo"file∗.?"today=‘date‘echoTodayis:today

ls ex*

68a9861b98e37ea499e4f3b1f6f6e3ee.png

e3d174e642e011d1ad8a9a745eb85701.png

(2)ex2

#!/bin/bash

#bash script programming

if test $# == 0

then

ls -l|grep ‘^d’

else

for i

do

ls -l $i|grep ‘^d’

done

fi

17a182d151995cc8e27cbef845b7ff0f.png

6f49037d8cb613468226ee74a5763dbf.png

(3)ex3

case $1 in

-b) count=grep ^b $2 | wc -l

echo “The number of lines in $2 that start with b is $count.”;;

-s) count=grep ^s $2 | wc -l

echo “The number of lines in $2 that start with s is $count.”;;

*) echo “That option is not recognized.”;;

esac

0df0e19c115fca0307ed93fc5b744d15.png

be91ada0e78196e97275225213d8bf27.png

6、编写脚本

(1)编写shell 脚本,计算1-100 的和;

[goutong@localhost ~]$ vi shell1

[goutong@localhost ~]$ cat shell1

#!/bin/bash

sum=0

for ((i=1;i<=100;i++));

do

sum=(

(

((((i+$sum))

done

echo $sum

1a15bf554c127e87d84bf5745912cb63.png

(2)编写shell 脚本,要求输入一个数字,然后计算出从1 到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止;

[goutong@localhost ~]$ vi shell2

[goutong@localhost ~]$ cat shell2

#! /bin/bash

i=0

while [ $i -lt 1 ];do

read -p “the number must be larger than 1:” i

done

sum=0

for n in seq 1 $i;do

sum=[

[[sum+$n]

done

echo s

u

m

[

g

o

u

t

o

n

g

@

l

o

c

a

l

h

o

s

t

]

sum [goutong@localhost ~]sum[goutong@localhost] bash shell2

the number must be larger than 1:5

15

7def469d98e0209f4f833df3014ed726.png

(3)编写shell 脚本,把/home目录下的所有目录拷贝到/tmp目录下;

[goutong@localhost ~]$ vi shell3

[goutong@localhost ~]$ cat shell3

#! /bin/bash

cd /home

for i in ls;do

if [ -d $i ] ; then cp -r $i /tmp/

fi

done

43952b90ca02d767e540cbd562dcba6b.png

7310112994b860fc43a5279a928b7957.png

四、源程序清单、测试数据及结果

测试结果及具体步骤见步骤三

五、实验小结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值