810303实验报告Word格式.docx
《810303实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《810303实验报告Word格式.docx(13页珍藏版)》请在冰点文库上搜索。
1、实验目的
1、理解与Shell相关的基本概念,掌握shell程序的执行方式
2、掌握shell变量与环境变量的使用、流程控制语句的使用
3、理解shell函数的定义及调用
2、实验环境
RedHatLinuxServer6.0(64位)
3、实验原理(或要求)
1.能熟练进行Shell程序编辑及运行
2.能正确使用shell变量、流程控制语句
4、实验步骤
1.用vi编辑shell程序
2.改变shell程序文件属性
3.运行shell程序
vie1.sh
键入如下内容:
#!
/bin/sh
echo“Hello,world!
”
保存退出。
shell脚本是按行解释的,每个脚本的第一行总是以#!
/bin/sh开头。
在shell脚本中,以“#”开头的行表示注释,shell将忽略对该行的处理,即不进行解释、显示和执行。
两种执行shell脚本程序的方法。
第一种方法:
直接执行
(1)给脚本e1.sh增加执行权
[root@serverroot]#chmoda+xe1.sh
(2)键入./el.sh直接执行脚本
[root@serverroot]#./e1.sh
Hello,world!
第二种执行shell脚本方法是:
不更改属性,键入sh脚本名即可。
[root@serverroot]#she1.sh
5、记录与处理(实验数据、误差分析、结果分析)
1、编辑如下shell程序并执行,察看并分析结果。
e2.sh
echo$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11
e3.sh
shift
echo$1
shift2
e4.sh
echo"
TheScriptnameis:
$0"
Thereare$#parameters!
"
Thefirstparameteris:
$1"
Thesecondparameteris:
$2"
Thethirdparameteris:
$3"
Thefourthparameteris:
$4"
echo'
$*='
$*
$@='
$@
$$='
$$
e5.sh
*WelcometoBeijing*"
vara="
Hi"
$vara,nicetomeetyou!
e6.sh
Howdoyoudo'
vara='
hi'
'
e7.sh
Hello!
echo-n"
What'
syourname:
"
readyourname
Hello$yourname!
exit0
e10.sh
name="
garfield"
Pleaseinput:
readuservalue
if[$uservalue=$name];
then
echo"
same"
else
difference"
fi
e12.sh
today=`date+%u`
if[$today=1];
TodayisMonday"
TodayisnotMonday"
e13.sh
usefile=$1
if[-d$usefile];
directory"
elif[-f$usefile];
normal"
elif[-b$usefile];
blockdevice"
elif[-c$usefile];
chardevice"
other"
fi
e16.sh
option=$1
filename=$2
case$optionin
r)
chmodu+r$filename
;
;
w)
chmodu+w$filename
x)
chmodu+x$filename
*)
echo"
error!
exit1
esac
e17.sh
Pleaseinputyourchoice[y|n]:
readch
case$chin
y|Y|yes||YES)
goon"
n|N|no|NO)
cancel"
error,youshouldinput[y|n]"
e19.sh
foriin$@
do
echo$i
done
e20.sh
IFS=:
fordirin$PATH
ls-ld$dir
Finished!
e21.sh
for((i=1;
i<
=6;
i++))
for((j=1;
j<
=i;
j++))
do
echo-e"
*\c"
done
echo
e23.sh
counter=1
whilereadline
if!
[${line:
0:
1}="
#"
];
$counter:
$line"
counter=$[counter+1]
fi
done<
$1
e24.sh
selectmyoptioninshoppingfishingcookingquitting
case$myoptionin
shop*)echoIwanttogo$myoptionwithlotsofmoney.
;
fish*)echoIwanttogo$myoptionwithJohn.
cooking)echoIamcookingasmallfish.
quit*)echobye.
break
*)echotryagain!
esac
e25.sh
ps_fun()
{
psaux
}
functionlog_fun
tail/var/log/messages
ls_fun()
ls-la
selectmyactionindisplay_procshow_loglist_dirquit
case$myactionin
display_proc)
ps_fun;
show_log)
log_fun;
list_dir)
ls_fun;
quit)
break;
echoerror;
e26.sh
my_fun()
over=$1
counter=1
while[$counter-le$over]
echo$counter
counter=$[counter+1]
Pleaseinputanumber:
readnum
my_fun$num
6、思考题
1、编写shell程序,使之可以显示奇数位置参数的值,即可显示$1、$3、$5……位置参数的值?
/bin/bash
i=0
forparain$*
leti+=1
num=$((i%2))
[$num-ne0]&
&
$para"
2、编写shell程序abc.sh用来为习题中的第一块网卡设置子接口,且子接口的数量由命令行参数来指定。
执行效果如下:
#abc.sh10
创建10个子接口将IP地址设置为:
192.168.1.1~192.168.1.10
N=1
number=$1
if[$number-lt$N];
exit0
whiletrue
if[$N-gt$number];
break;
ifconfig-seth0:
${N}192.168.1.${N}/24
3、编写shell程序实现:
创建100个名为user1~user100的用户;
创建100个名为dir1~dir100的目录并位于/tmp下;
将上述100个目录分别划归给上述100个用户,即将dir1给user1,再将上述100个用户目录的权限设置为:
所有者拥有全部权限,其他用户拥有执行权限,同组用户拥有读和执行的权限(要考虑程序能够多次正确执行)。
#/bin/bash
Num=1
cd/tmp
groupaddusergroup
while[$Num-le100]
useradd-d"
/tmp/dir$Num"
-gusergroupuser$Num
if[!
-d"
dir$Num"
]
mkdir"
chown-Ruser$Numdir$Num
chmod-R751dir$Num
Num=$(($Num+1))
4、编写一个可以安装系统光盘中的指定软件包的脚本程序。
myinstall.sh:
name=$1
if[-z"
$name"
Usage$0<
package-name>
return1
printf"
PleaseinerttheCDintothedrive,pressenterwhenready......"
read
mount/mnt/cdrom
if[$?
-ne0];
files=/mnt/cdrom/RedHat/RPMS/*$name*.rpm
$files"
Norpmfilesmatched."
forfin$files;
rpm-i$f
umount/mnt/cdrom
7、实验小结
通过本次实验,掌握了shell程序的编辑及执行方式,能编写简单的shell程序。