一、实验目的
创建和运行含分支、循环、位置变量等的脚本。
二、实验环境
虚拟Linux主机,远程工具 putty
三、实验过程
(一)准备
1.启动主机Linux,以yanji登录。
(二)比较运算
1.创建脚本
[yanji@localhost ~]$ cat > grade.sh
#! /bin/bash
if [$1 -ge 90];then
echo "A"
elif [$1 -ge 80];then
echo "B"
elif [$1 -ge 70];then
echo "C"
elif [$1 -ge 60];then
echo "D"
else
echo "F"
fi
解释:该脚本为用if 语句,对分数进行分段大于等于90[100,90]分的为A;大于等于80[80,90)分为B;大于等于70[70,80)分为C;大于等于60[60,70)分为D,其他(低于60分)为F等级;
[yanji@localhost ~]$ sh grade.sh 90
A
解释:没有权限,通过sh运行脚本(bash也可以)
[yanji@localhost ~]$ chmod +x grade.sh
[yanji@localhost ~]$ ./grade.sh 89
B
[yanji@localhost ~]$ ./grade.sh 70
C
[yanji@localhost ~]$ ./grade.sh 60
D
[yanji@localhost ~]$ ./grade.sh 55
F
解释:chmod +x 赋予grade.sh脚本可执行权限,用./直接运行权限,上面命令均通过相对路径执行脚本。
(三)文件测试
[yanji@localhost ~]$ cat > file.sh
#! /bin/bash
if [ -f $1 ]; then
echo "$1 is a file."
elif [ -d $1 ]; then
echo "$1 is a directory."
else
echo "$1 dose not exit."
fi
解释:file.sh脚本中 -f:文件是否存在,且为普通文件;-d:文件是否存在且为目录;
[yanji@localhost ~]$ chmod +x file.sh
[yanji@localhost ~]$ mkdir -p doc
[yanji@localhost ~]$ touch cow
[yanji@localhost ~]$ ./file.sh cow
cow is a file.
[yanji@localhost ~]$ ./file.sh doc
doc is a directory.
[yanji@localhost ~]$ ./file.sh yny
yny dose not exit.
解释:首先也是先给file.sh赋予可执行权限,然后创建了一个doc目录、一个cow普通文件,然后调用file.sh脚本,判断cow是一个文件;doc是一个目录,yny 是不存在的(因为我没有创建)。结果与命令如上诉所示。
(四)for循环
[yanji@localhost ~]$ cat > add.sh
#! /bin/bash
declare -i s=0
for x in $*; do
s=s+x
done
echo $s
解释:add.sh脚本是用$*作为参数列表,的、首先定义s=0,然后在外部输入值x,做循环,外部值相加,如下面例子10+20+30+40+100 = 200,输出结果200.
[yanji@localhost ~]$ chmod +x add.sh
[yanji@localhost ~]$ ./add.sh 10 20 30 40 100
200
解释:chmod +x 赋予add.sh可执行条件,然后运行add.sh脚本,输入10 20 30 40 100五个值相加,得出结果200,并输出200.
(五) while循环和函数
(1)创建文件 sailing.txt,内容如下:
[yanji@localhost ~]$ cat > sailing.txt
Sailing
远航
I am sailing, I am sailing
home again 'cross the sea.
I am sailing stormy waters,
to be near you, to be free.
I am flying, I am flying
like a bird 'cross the sky.
I am flying
passing high clouds,
to be near you,
to be free.
Can you hear me,
can you hear me,
through the dark night far away?
I am dying,
forever crying, to be with you;
who can say?
Can you hear me,
can you hear me,
through the dark night far away?
I am dying, forever, crying to be with you;
who can say?
We are sailing,
we are sailing home again '
cross the sea.
We are sailing stormy waters,
to be near you, to be free.
我在远航,我在远航,穿越海洋,重回故乡
我在远航,乘风破浪,向你靠近,获得自由
我在飞翔,我在飞翔,像那鸟儿,展翅翱翔#
我在飞翔,穿过云朵,向你靠近,获得自由
是否听到,我的歌唱,夜色茫茫,道路长长
我命垂危,人世凄凉,有你依偎,宛若天堂
是否听到,我的歌唱,夜色茫茫,道路长长
我命垂危,人世凄凉,有你依偎,宛若天堂
我们远航,我们远航,穿越海洋,重回故乡
我们远航,乘风破浪,向你靠近,获得自由
(2)创建脚本 eng-chi.sh,内容如下:
[yanji@localhost ~]$ vim eng-chi.sh
#! /bin/bash
eng() {
echo $1 | grep -q "^[a-zA-Z]"
}
: > $1.eng
: > $1.chi
while read line; do
if eng $line || test -z "$line"; then
echo $line >> $1.eng
else
echo $line >> $1.chi
fi
done < $1
[yanji@localhost ~]$ chmod +x eng-chi.sh
[yanji@localhost ~]$ ./eng-chi.sh sailing.txt
[yanji@localhost ~]$ cat sailing.txt.eng
Sailing
I am sailing, I am sailing
home again 'cross the sea.
I am sailing stormy waters,
to be near you, to be free.
I am flying, I am flying
like a bird 'cross the sky.
I am flying
passing high clouds,
to be near you,
to be free.
Can you hear me,
can you hear me,
through the dark night far away?
I am dying,
forever crying, to be with you;
who can say?
Can you hear me,
can you hear me,
through the dark night far away?
I am dying, forever, crying to be with you;
who can say?
We are sailing,
we are sailing home again '
cross the sea.
We are sailing stormy waters,
to be near you, to be free.
解释:赋予eng-chi.sh脚本可执行条件,以sailing.txt为例,执行eng-chi.sh脚本,查看sailing.txt.eng,发现里面全是sailing.txt的英文行和空行。
[yanji@localhost ~]$ cat sailing.txt.chi
远航
我在远航,我在远航,穿越海洋,重回故乡
我在远航,乘风破浪,向你靠近,获得自由
我在飞翔,我在飞翔,像那鸟儿,展翅翱翔
我在飞翔,穿过云朵,向你靠近,获得自由
是否听到,我的歌唱,夜色茫茫,道路长长
我命垂危,人世凄凉,有你依偎,宛若天堂
是否听到,我的歌唱,夜色茫茫,道路长长
我命垂危,人世凄凉,有你依偎,宛若天堂
我们远航,我们远航,穿越海洋,重回故乡
我们远航,乘风破浪,向你靠近,获得自由
解释:执行eng-chi.sh脚本,查看sailing.txt.chi,发现里面的内容全是sailing.txt的中文行。
(4)
解释:eng-chi.sh脚本的意思是读取一个文件,匹配文件中以英文开头的句子或是空行的句子是,
把这一行都输入到该文件后缀为.eng的文件里,比如sailing.txt.eng;文件中其他的行输入到该文件后缀为.chi的文件里,
比如sailing.txt.chi; : > $1.eng : > $1.chi的意思是第一次就创建$1.eng和$1.chi这两个文件,
如果是第二次再次运行,就先清空这两个文件。