**
《网络操作系统》实验预习报告
**
一、实验目的
1.为文件扩展名使用通配符
2.标准输入、标准输出和标准错误的重定向
3.使用管道将一个进程的输出作为输入提供给另一个进程。
4.执行命令分组和行继续
5.编写shell 脚本。
二、实验内容
1、管道(pipe-line)的使用
执行格式: command1|command2
功能:将command1的执行结果送到command2 作为输入
2、标准输入控制
执行格式: command-line<file 将file作为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<file 将file作为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
(2)要求用带有通配符的ls命令来显示文件名,实现:
-以conf结束
*[goutong@localhost etc]$ ls -d conf
-以d 或D开始
[goutong@localhost etc]$ ls -d [dD]*
-在文件名的第五个位置包括一个o
[goutong@localhost etc]$ ls -d ???o*
-包含单词tab
-以数字结尾
[goutong@localhost etc]$ ls -d *[0-9]
-不以数字结尾
[goutong@localhost etc]$ ls -d *[!0-9]
(3)如果你执行命令ls -d ?[!y][e-f]将会发生什么情况?能与之匹配的最短文件名是什么?执行这些命令来验证你的答案。
[goutong@localhost etc]$ ls -d ?[!y][e-f]
2、重定向
(1)使用cat命令和指令来创建一个包含几行文本名为junk的文件。
[goutong@localhost ~]$ cat > junk
[goutong@localhost ~]$ cat junk
(2)使用重定向junk文件中追加几行文本。然后查看junk文件的内容并且检查你刚刚所创建的文本是否都存在。
[goutong@localhost ~]$ cat >> junk
[goutong@localhost ~]$ cat junk
3、管道
(1)统计你当前目录所有文件的数目。使用管道,而不是手动地统计所有文件
[goutong@localhost ~]$ ls | wc -l
(2)ls > tempfile ; wc -l tempfile是否和你刚刚使用的管道功能相同?为什么或者为什么不?
不同,重定向如果没有tempfile会重新创建一个
(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
4、shell变量
(1)显示由你当前进程环境所定义的所有变量。并且显示出当前输出的所有变量。
[goutong@localhost ~]$ env
(2)创建一个变量x并初始化值为10。检查这个变量的值。再次,显示当前进程环境的所有变量。
(3)创建一个subshell.查看变量x在subshell下的值是什么?
[goutong@localhost ~]$ vi subshell
[goutong@localhost ~]$ bash subshell
输出为空
(4)将x设置为500并返回到父进程。当前x的值是什么?为什么?
[user1@localhost goutong]$ x=500
[user1@localhost goutong]$ exit
exit
[goutong@localhost ~]$ echo $x
输出为空
(5)确保子进程继承了x。如何实现?
[goutong@localhost ~]$ export x
[goutong@localhost ~]$ env | less
[goutong@localhost ~]$ bash
[goutong@localhost ~]$ echo $x
[goutong@localhost ~]$ exit
exit
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*
(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
(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
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
(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
(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
四、源程序清单、测试数据及结果
测试结果及具体步骤见步骤三
五、实验小结
略