20.1 shell脚本介绍
shell本并不能作为正式的编程语言,因为它是在 Linux的shell中运行的,所以称为shell脚本。事实上,shell本就是一些命令的集合。比如,我想实现这样的操作
(1)进人/tmp/目录;
2)列出当前目录中所有的文件名;
(3)把所有当前的文件复制到root目录下;
(4)删除当前目录下所有的文件。
完成以上简单的4步需要在shell窗口中输入4次命令,按4次回车,这不算太难。但如果是输入复杂的命令,一次一次敲键盘会很麻烦。我们不妨把所有的操作都记录到一个文档中,然后去调用文档中的命令,这样一步操作就可以完成。其实这个文档就是shell脚本,只是这个shel本有它特殊的格式。
shell脚本能帮助我们很方便地管理服 务器,因为我们可以指定一个任务计划,定时去执行某个 shell脚本以满足需求。这对于 Linux系统管理员来说是一件非常值得自豪的事情。现在的139邮箱很好用,发邮件的同时还可以发一条邮件通知短信给用户。利用这点,我们就可以在 Linux服务器上部署监控的shell脚本。比如,网卡流量出现异常或者Web服务器停止服务,就可以发一封邮件给管理员,同时发送一条告警短信给管理员。这样可以让管理员及时知道服务器出问题了。
在正式编写she脚本之前,阿铭老师建议凡是自定义的脚本都放到 usr/local/sbin/目录下。这样做的目的是:一来可以更好地管理文档;二来以后接管你工作的管理员都知道自定义脚本放在哪里,方便维护
20.2 shell脚本结构和执行
1.首先创建一个脚本
[root@liuhongwei-01 shell]# vi 1.sh
#!/bin/bash
echo "123"
w
ls
shell脚本通常都以.sh为后缀名。这并不是说不加.sh的脚本就不能执行,只是大家的一个习惯面已所以,以后如果发现了以.shh为后缀的文件,那么它可能是一个shell脚本。本例中,脚本文件1.sh的第1行要以#!/bin/bash开头,表示该文件使用的是bash语法。如果不设置该行,你的shell脚本也可以执行,但是不符合规范。#表示注释,后面跟一些该脚本的相关注释内容,以及作者、创建日期或者版本等。当然,这些注释并非必需的,不建议省略。因为随着工作时间的逐渐过渡,写的shell脚本也会越来越多,如果有一天你回头查看自己写过的某个脚本,很有可能忘记该脚本是用来干什么的以及什么时候写的。所以写上注释是有必要的。另外,系统管理员并非只有你一个,写上注释有助于其他管理员查看你的脚本。
2.然后我们执行一下
[root@liuhongwei-01 shell]# sh 1.sh
123
20:56:21 up 5 min, 1 user, load average: 0.04, 0.34, 0.21
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.93.1 20:52 5.00s 0.04s 0.00s sh 1.sh
1.sh
3.还有一种执行脚本的命令
[root@liuhongwei-01 shell]# ./1.sh
-bash: ./1.sh: 权限不够
[root@liuhongwei-01 shell]# chmod +x 1.sh
[root@liuhongwei-01 shell]# ./1.sh
123
21:05:07 up 14 min, 1 user, load average: 0.00, 0.06, 0.12
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.93.1 20:52 3.00s 0.05s 0.02s w
1.sh
[root@liuhongwei-01 shell]#
第一次的时候提示权限不够,那么我们给脚本加上一个x权限,就可以执行了。使用该方法运行shell脚本的前提是脚本本身有执行权限,所以需要给脚本加一个x权限。
[root@liuhongwei-01 shell]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 960376 11月 20 2015 /bin/bash
[root@liuhongwei-01 shell]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 7月 1 22:21 /bin/sh -> bash
[root@liuhongwei-01 shell]#
上面的二个文件其实是一个文件
4.上面提到#!/bin/bash开头是该文件的使用的是bash语法,但是下面接着写#号开头的,表示的是注释说明,可以看一下network脚本
5.查看脚本执行过程
[root@liuhongwei-01 shell]# sh -x 1.sh
+ echo 123
123
+ w
21:17:25 up 26 min, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.93.1 20:52 5.00s 0.06s 0.00s sh -x 1.sh
+ ls
1.sh
每一个加号 表示执行的过程
6.检查语法有没有错误
[root@liuhongwei-01 shell]# vi 1.sh
#!/bin/bash
echo "123"
for i in 'sep 1 10'
w
ls
~
~
[root@liuhongwei-01 shell]# sh -n 1.sh
1.sh:行5: 语法错误: 未预期的文件结尾
会提示报错,,因为写for 语句需要加上do done
[root@liuhongwei-01 shell]# vi 1.sh
#!/bin/bash
echo "123"
for i in 'sep 1 10'
do
echo $i
done
[root@liuhongwei-01 shell]# sh -n 1.sh
[root@liuhongwei-01 shell]#
不显示报错了。
20.3 date命令用法
date命令在shell脚本中常用的几个用法如下:
date +%Y :表示以四位数字格式打印年份
[root@liuhongwei-01 shell]# date +%Y
2018
date +%y :表示以二位数字格式打印年份
[root@liuhongwei-01 shell]# date +%y
18
date +%m:表示月份
[root@liuhongwei-01 shell]# date +%m
07
date +%d :表示日期
[root@liuhongwei-01 shell]# date +%d
27
date +%H:表示小时
[root@liuhongwei-01 shell]# date +%H
21
date +%M:表示分钟
[root@liuhongwei-01 shell]# date +%M
37
date +%S:表示秒
[root@liuhongwei-01 shell]# date +%S
28
date +%w:表示星期。结果显示0则表示周日
[root@liuhongwei-01 shell]# date +%w
5
date %D:表示年月日
07/27/18
date +%F:也表示年月日
2018-07-27
date +%T:表示时间
21:30:15
date+%s:表示时间戳
1532698249
date +%W:表示今年的第几周
[root@liuhongwei-01 shell]# date +%W
30
date +%h:表示月份
[root@liuhongwei-01 shell]# date +%h
7月
date +"%Y-%m-%d %H:%M:%S"
2018-07-27 21:43:04
date -d "-1 day" +%d
表示一天前
date -d "-1 hour" +%H
表示一小时前
date -d “-1 min”+%M
一分钟前
date -d “-1 mouth”+%F
上个月
date -d “-1 year”+%F
上一年
时间的表示有二种
[root@liuhongwei-01 shell]# date -d@1532698249
2018年 07月 27日 星期五 21:30:49 CST
[root@liuhongwei-01 shell]# date +%s -d "2018-07-27 21:49:30"
1532699370
[root@liuhongwei-01 shell]#
还有一种查看日历的命令
[root@liuhongwei-01 shell]# cal
七月 2018
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
[root@liuhongwei-01 shell]#
20.4 shell脚本中的变量
20.5 shell脚本中的逻辑判断
格式1:if 条件 ; then 语句; fi
首先写一个脚本,详细介绍
[root@liuhongwei-01 shell]# vim 2.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi
首先定义一个变量a 如果a 大于3 接着就执行ok 然后fi结尾,这里的gt表示大于
[root@liuhongwei-01 shell]# sh 2.sh
ok
[root@liuhongwei-01 shell]# sh -x 2.sh
+ a=5
+ '[' 5 -gt 3 ']'
+ echo ok
ok
格式2:if 条件; then 语句; else 语句; fi
还是写一个脚本说明
[root@liuhongwei-01 shell]# vim 2.sh
#!/bin/bash
a=5
if [ $a -gt 6 ]
then
echo ok
else
echo no ok
fi
就是比上面的例子多个else,else表示,如果上面的if判断成功,就执行ok,除此之外,if判断不成功,就执行no ok
[root@liuhongwei-01 shell]# sh -x 2.sh
+ a=5
+ '[' 5 -gt 6 ']'
+ echo no ok
no ok
[root@liuhongwei-01 shell]# sh 2.sh
no ok
[root@
if …; then … ;elif …; then …; else …; fi
[root@liuhongwei-01 shell]# vim 2.sh
#!/bin/bash
a=3
if [ $a -lt 1 ]
then
echo ">1"
elif [ $a -lt 6 ]
then
echo "<6 && >1"
else
echo no ok
fi
这个例子多一个elif 意思是在if条件的基础上,在满足一个条件,然后执行结果,这里的lt表示小于,这里的elif可以写多个。
[root@liuhongwei-01 shell]# sh -x 2.sh
+ a=3
+ '[' 3 -lt 1 ']'
+ '[' 3 -lt 6 ']'
+ echo '<6 && >1'
<6 && >1
[root@liuhongwei-01 shell]# vim 2.sh
[root@liuhongwei-01 shell]# sh 2.sh
<6 && >1
还有一种格式是if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]
[root@liuhongwei-01 shell]# vim 2.sh
#!/bin/bash
a=2 b=3
if [ $a -gt $b ]
then
echo "error"
elif [ $a -lt $b ]
then
echo "okok"
else
echo no ok
fi
[root@liuhongwei-01 shell]# sh -x 2.sh
+ a=2
+ b=3
+ '[' 2 -gt 3 ']'
+ '[' 2 -lt 3 ']'
+ echo okok
okok
[root@liuhongwei-01 shell]# vim 2.sh
[root@liuhongwei-01 shell]# sh 2.sh
okok
这里涉及到了大于 等于 小于 下面详细说明
-gt (>):大于
-lt(<):小于
-ge(>=):大于等于
-le(<=):小于等于
-eq(==):等于
-ne(!=):不等于
还有一种格式是&& 和|| 并且和或者 举例说明
[root@liuhongwei-01 shell]# vim 2.sh
#!/bin/bash
a=2 b=3
if [ $a -gt 1 ] && [ $a -lt 0 ]
then
echo "大于1小于0"
elif [ $a -lt 3 ] && [ $a -gt 1 ]
then
echo "小于3大于1"
else
echo no ok
fi
if条件后面使用用&& 并且的意思,要满足这个二个条件。
下面是执行过程
[root@liuhongwei-01 shell]# sh 2.sh
小于3大于1
[root@liuhongwei-01 shell]# sh -x 2.sh
+ a=2
+ b=3
+ '[' 2 -gt 1 ']'
+ '[' 2 -lt 0 ']'
+ '[' 2 -lt 3 ']'
+ '[' 2 -gt 1 ']'
+ echo $'\345\260\217\344\272\2163\345\244\247\344\272\2161'
小于3大于1
20.6 文件目录属性判断
• [ -f file ]判断是否是普通文件,且存在
• [ -d file ] 判断是否是目录,且存在
• [ -e file ] 判断文件或目录是否存在
• [ -r file ] 判断文件是否可读
• [ -w file ] 判断文件是否可写
• [ -x file ] 判断文件是否可执行
1.举例说上面的各个用法
[root@liuhongwei-01 shell]# vi file1.sh
#!/bin/bash
f="/tmp/zhangsan"
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
首先写一下脚本文件,-f判断这个是不是普通文件且存在,存在就退出,不存在就创建文件
[root@liuhongwei-01 shell]# sh -x file1.sh
+ f=/tmp/zhangsan
+ '[' -f /tmp/zhangsan ']'
+ touch /tmp/zhangsan
那我们再次执行这个脚本就会显示文件存在,直接退出了
[root@liuhongwei-01 shell]# sh -x file1.sh
+ f=/tmp/zhangsan
+ '[' -f /tmp/zhangsan ']'
+ echo /tmp/zhangsan exist
/tmp/zhangsan exist
2.使用-d查看这个目录是否存在
[root@liuhongwei-01 shell]# vi file1.sh
#!/bin/bash
f="/tmp/yangying"
if [ -d $f ]
then
echo $f exist
else
mkdir $f
fi
~
执行过程
[root@liuhongwei-01 shell]# sh -x file1.sh
+ f=/tmp/yangying
+ '[' -d /tmp/yangying ']'
+ mkdir /tmp/yangying
[root@liuhongwei-01 shell]# ls -l /tmp
总用量 28
-rw-r--r-- 1 root root 0 7月 28 11:29 aminglinux
srw-rw-rw- 1 root root 0 7月 28 10:18 aming.sock
-rw-r--r--. 1 root root 1257 7月 18 13:43 db1.sql
drwxr-xr-x. 2 root root 6 7月 23 22:33 hsperfdata_root
-rw-r--r-- 1 root root 3598 7月 21 15:48 keepalived.conf
-rw-r--r-- 1 root root 583 7月 23 19:20 keepalived+nginx.conf
srwxrwxrwx 1 mysql mysql 0 7月 28 10:18 mysql.sock
srw-rw-rw- 1 root root 0 7月 28 10:18 php-fcgi.sock
drwx------ 3 root root 16 7月 28 10:18 systemd-private-1731efe7ddd4431c85e505b18602883f-httpd.service-HyxHEi
-rw-r--r-- 1 root root 0 7月 21 15:35 test.com.log
-rw-r--r--. 1 root root 1258 7月 18 13:35 test.sql
drwxr-xr-x 2 root root 6 7月 28 11:36 yangying
-rw-r--r-- 1 root root 0 7月 28 11:32 zhangsan
-rw-r--r--. 1 root root 10169 7月 18 13:43 zrlog.sql
[root@liuhongwei-01 shell]#
再次执行这个脚本,会提示目录已经存在了
[root@liuhongwei-01 shell]# sh -x file1.sh
+ f=/tmp/yangying
+ '[' -d /tmp/yangying ']'
+ echo /tmp/yangying exist
/tmp/yangying exist
3.判断一个文件是否可读
[root@liuhongwei-01 shell]# vi file1.sh
#!/bin/bash
f="/tmp/yangying"
if [ -r $f ]
then
echo "$f 可读"
fi
执行过程
[root@liuhongwei-01 shell]# sh -x file1.sh
+ f=/tmp/yangying
+ '[' -r /tmp/yangying ']'
+ echo '/tmp/yangying 可读'
/tmp/yangying 可读
[root@liuhongwei-01 shell]# sh file1.sh
/tmp/yangying 可读
在判断文件上也可以使用并且符号&&
[root@liuhongwei-01 shell]# vi file1.sh
#!/bin/bash
f="/tmp/yangying"
if [ -f $f ]
then
rm -f $f
fi
[ -f $f ] && rm -f $f
上面的写法加上&& 等同于上面脚本中最下面的写法
也可以加上! 取反的意思
[root@liuhongwei-01 shell]# vi file1.sh
#!/bin/bash
f="/tmp/yangying"
if [ ! -f $f ]
20.7 if特殊用法
1.首先编写脚本
[root@liuhongwei-01 shell]# vim 3.sh
#!/bin/bash
n=`wc -l /tmp/lala
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo dasdas
fi
~
脚本中使用-z来判断文档中的值是否为空,不为空怎样,为空怎样。在使用-z的时候 后面的$N要加上双引号。
执行过程
[root@liuhongwei-01 shell]# sh -x 3.sh
++ wc -l /tmp/lala
wc: /tmp/lala: 没有那个文件或目录
+ n=
+ '[' -z '' ']'
+ echo error
error
+ exit
2.使用-n 选项,判断文档的值不为空,可以不用编写脚本,直接写一行命令。
[root@liuhongwei-01 shell]# if [ -n 1.sh ]; then echo ok; fi
ok
3.也可以使用正则来作为判断条件
[root@liuhongwei-01 shell]# if grep -wq 'root' /etc/passwd;then echo "root exist"; fi
root exist
4.[ ] 中不能使用<, >, ==, !=, >=, <=这样的符号
5.还有一种就是或者和并且
if [ $a -gt 5 -o $a -lt 10 ] -o是或者的意思
if [ $a -gt 5 -a $a -lt 10 ] -a是并且的意思
20.8/20.9 case判断
在shell脚本中,除了用if来判断逻辑外,还有一种常用的方式-----case,具体格式如下:
• 格式 case 变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
上面的结构中,不限制value的个数,*代表其他值。 下面写一个判断分数的脚本
#!/bin/bash
read -p "Please input a number: " n 表示输入一个数字 n 表示捕获的变量
if [ -z "$n" ] 判断输入的数值是否为空
then
echo "Please input a number." 如果数值为空就还提示输入一个数字
exit 1
fi
n1=`echo $n|sed 's/[0-9]//g'` 然后把输入值做个判断是否是纯数字,不是纯数字就清空一下数字
if [ -n "$n1" ] 然后-n判断数值为空时 怎么怎么样
then
echo "Please input a number."
exit 1
fi
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo "不优秀"
;;
2)
echo "优秀"
;;
3)
echo "非常优秀"
;;
4)
echo "特别优秀"
;;
*)
echo "The number range is 0-100."
;;
esac
我们输入一个数字看一下执行过程
[root@liuhongwei-01 shell]# sh -x 4.sh
+ read -p 'Please input a number: ' n
Please input a number: 67
+ '[' -z 67 ']'
++ echo 67
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 67 -lt 60 ']'
+ '[' 67 -ge 60 ']'
+ '[' 67 -lt 80 ']'
+ tag=2
+ case $tag in
+ echo $'\350\211\257\345\245\275'
良好
[root@liuhongwei-01 shell]# sh -x 4.sh
+ read -p 'Please input a number: ' n
Please input a number: 98
+ '[' -z 98 ']'
++ echo 98
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 98 -lt 60 ']'
+ '[' 98 -ge 60 ']'
+ '[' 98 -lt 80 ']'
+ '[' 98 -ge 80 ']'
+ '[' 98 -lt 90 ']'
+ '[' 98 -ge 90 ']'
+ '[' 98 -le 100 ']'
+ tag=4
+ case $tag in
+ echo $'\351\235\236\345\270\270\344\274\230\347\247\200'
非常优秀
[root@liuhongwei-01 shell]# sh 4.sh
Please input a number: 98
非常优秀
20.10 for循环
for语句的 语法:for 变量名 in 条件; do …; done
首先写一个脚本
需求是求出1-100的和
[root@liuhongwei-01 shell]# vim 5.sh
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$sum=$i]
done
echo $sum
执行过程
[root@liuhongwei-01 shell]# sh -x 5.sh
+ sum=0
++ seq 1 100
+ for i in '`seq 1 100`'
+ sum=1
+ for i in '`seq 1 100`'
+ sum=3
+ for i in '`seq 1 100`'
+ sum=6
+ for i in '`seq 1 100`'
+ sum=10
+ for i in '`seq 1 100`'
+ sum=15
+ for i in '`seq 1 100`'
+ sum=21
+ for i in '`seq 1 100`'
+ sum=28
+ for i in '`seq 1 100`'
+ sum=36
+ for i in '`seq 1 100`'
+ sum=45
+ for i in '`seq 1 100`'
+ sum=55
+ for i in '`seq 1 100`'
+ sum=66
+ for i in '`seq 1 100`'
+ sum=78
+ for i in '`seq 1 100`'
+ sum=91
+ for i in '`seq 1 100`'
+ sum=105
+ for i in '`seq 1 100`'
+ sum=120
+ for i in '`seq 1 100`'
+ sum=136
+ for i in '`seq 1 100`'
+ sum=153
+ for i in '`seq 1 100`'
+ sum=171
+ for i in '`seq 1 100`'
+ sum=190
+ for i in '`seq 1 100`'
+ sum=210
+ for i in '`seq 1 100`'
+ sum=231
+ for i in '`seq 1 100`'
+ sum=253
+ for i in '`seq 1 100`'
+ sum=276
+ for i in '`seq 1 100`'
+ sum=300
+ for i in '`seq 1 100`'
+ sum=325
+ for i in '`seq 1 100`'
+ sum=351
+ for i in '`seq 1 100`'
+ sum=378
+ for i in '`seq 1 100`'
+ sum=406
+ for i in '`seq 1 100`'
+ sum=435
+ for i in '`seq 1 100`'
+ sum=465
+ for i in '`seq 1 100`'
+ sum=496
+ for i in '`seq 1 100`'
+ sum=528
+ for i in '`seq 1 100`'
+ sum=561
+ for i in '`seq 1 100`'
+ sum=595
+ for i in '`seq 1 100`'
+ sum=630
+ for i in '`seq 1 100`'
+ sum=666
+ for i in '`seq 1 100`'
+ sum=703
+ for i in '`seq 1 100`'
+ sum=741
+ for i in '`seq 1 100`'
+ sum=780
+ for i in '`seq 1 100`'
+ sum=820
+ for i in '`seq 1 100`'
+ sum=861
+ for i in '`seq 1 100`'
+ sum=903
+ for i in '`seq 1 100`'
+ sum=946
+ for i in '`seq 1 100`'
+ sum=990
+ for i in '`seq 1 100`'
+ sum=1035
+ for i in '`seq 1 100`'
+ sum=1081
+ for i in '`seq 1 100`'
+ sum=1128
+ for i in '`seq 1 100`'
+ sum=1176
+ for i in '`seq 1 100`'
+ sum=1225
+ for i in '`seq 1 100`'
+ sum=1275
+ for i in '`seq 1 100`'
+ sum=1326
+ for i in '`seq 1 100`'
+ sum=1378
+ for i in '`seq 1 100`'
+ sum=1431
+ for i in '`seq 1 100`'
+ sum=1485
+ for i in '`seq 1 100`'
+ sum=1540
+ for i in '`seq 1 100`'
+ sum=1596
+ for i in '`seq 1 100`'
+ sum=1653
+ for i in '`seq 1 100`'
+ sum=1711
+ for i in '`seq 1 100`'
+ sum=1770
+ for i in '`seq 1 100`'
+ sum=1830
+ for i in '`seq 1 100`'
+ sum=1891
+ for i in '`seq 1 100`'
+ sum=1953
+ for i in '`seq 1 100`'
+ sum=2016
+ for i in '`seq 1 100`'
+ sum=2080
+ for i in '`seq 1 100`'
+ sum=2145
+ for i in '`seq 1 100`'
+ sum=2211
+ for i in '`seq 1 100`'
+ sum=2278
+ for i in '`seq 1 100`'
+ sum=2346
+ for i in '`seq 1 100`'
+ sum=2415
+ for i in '`seq 1 100`'
+ sum=2485
+ for i in '`seq 1 100`'
+ sum=2556
+ for i in '`seq 1 100`'
+ sum=2628
+ for i in '`seq 1 100`'
+ sum=2701
+ for i in '`seq 1 100`'
+ sum=2775
+ for i in '`seq 1 100`'
+ sum=2850
+ for i in '`seq 1 100`'
+ sum=2926
+ for i in '`seq 1 100`'
+ sum=3003
+ for i in '`seq 1 100`'
+ sum=3081
+ for i in '`seq 1 100`'
+ sum=3160
+ for i in '`seq 1 100`'
+ sum=3240
+ for i in '`seq 1 100`'
+ sum=3321
+ for i in '`seq 1 100`'
+ sum=3403
+ for i in '`seq 1 100`'
+ sum=3486
+ for i in '`seq 1 100`'
+ sum=3570
+ for i in '`seq 1 100`'
+ sum=3655
+ for i in '`seq 1 100`'
+ sum=3741
+ for i in '`seq 1 100`'
+ sum=3828
+ for i in '`seq 1 100`'
+ sum=3916
+ for i in '`seq 1 100`'
+ sum=4005
+ for i in '`seq 1 100`'
+ sum=4095
+ for i in '`seq 1 100`'
+ sum=4186
+ for i in '`seq 1 100`'
+ sum=4278
+ for i in '`seq 1 100`'
+ sum=4371
+ for i in '`seq 1 100`'
+ sum=4465
+ for i in '`seq 1 100`'
+ sum=4560
+ for i in '`seq 1 100`'
+ sum=4656
+ for i in '`seq 1 100`'
+ sum=4753
+ for i in '`seq 1 100`'
+ sum=4851
+ for i in '`seq 1 100`'
+ sum=4950
+ for i in '`seq 1 100`'
+ sum=5050
+ echo 5050
5050
最后的值是5050
我们还可以把每次加出的值列出来
[root@liuhongwei-01 shell]# vim 5.sh
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$sum+$i]
echo "$sum + $i = $sum"
done
echo $sum
[root@liuhongwei-01 shell]# sh -x 5.sh
+ sum=0
++ seq 1 100
+ for i in '`seq 1 100`'
+ sum=1
+ echo '1 + 1 = 1'
1 + 1 = 1
+ for i in '`seq 1 100`'
+ sum=3
+ echo '3 + 2 = 3'
3 + 2 = 3
+ for i in '`seq 1 100`'
+ sum=6
+ echo '6 + 3 = 6'
6 + 3 = 6
+ for i in '`seq 1 100`'
+ sum=10
+ echo '10 + 4 = 10'
10 + 4 = 10
+ for i in '`seq 1 100`'
+ sum=15
+ echo '15 + 5 = 15'
15 + 5 = 15
+ for i in '`seq 1 100`'
+ sum=21
+ echo '21 + 6 = 21'
21 + 6 = 21
+ for i in '`seq 1 100`'
+ sum=28
+ echo '28 + 7 = 28'
28 + 7 = 28
+ for i in '`seq 1 100`'
+ sum=36
+ echo '36 + 8 = 36'
36 + 8 = 36
+ for i in '`seq 1 100`'
+ sum=45
+ echo '45 + 9 = 45'
45 + 9 = 45
+ for i in '`seq 1 100`'
+ sum=55
+ echo '55 + 10 = 55'
55 + 10 = 55
+ for i in '`seq 1 100`'
+ sum=66
+ echo '66 + 11 = 66'
66 + 11 = 66
+ for i in '`seq 1 100`'
+ sum=78
+ echo '78 + 12 = 78'
78 + 12 = 78
+ for i in '`seq 1 100`'
+ sum=91
+ echo '91 + 13 = 91'
91 + 13 = 91
+ for i in '`seq 1 100`'
+ sum=105
+ echo '105 + 14 = 105'
105 + 14 = 105
+ for i in '`seq 1 100`'
+ sum=120
+ echo '120 + 15 = 120'
120 + 15 = 120
+ for i in '`seq 1 100`'
+ sum=136
+ echo '136 + 16 = 136'
136 + 16 = 136
+ for i in '`seq 1 100`'
+ sum=153
+ echo '153 + 17 = 153'
153 + 17 = 153
+ for i in '`seq 1 100`'
+ sum=171
+ echo '171 + 18 = 171'
171 + 18 = 171
+ for i in '`seq 1 100`'
+ sum=190
+ echo '190 + 19 = 190'
190 + 19 = 190
+ for i in '`seq 1 100`'
+ sum=210
+ echo '210 + 20 = 210'
210 + 20 = 210
+ for i in '`seq 1 100`'
+ sum=231
+ echo '231 + 21 = 231'
231 + 21 = 231
+ for i in '`seq 1 100`'
+ sum=253
+ echo '253 + 22 = 253'
253 + 22 = 253
+ for i in '`seq 1 100`'
+ sum=276
+ echo '276 + 23 = 276'
276 + 23 = 276
+ for i in '`seq 1 100`'
+ sum=300
+ echo '300 + 24 = 300'
300 + 24 = 300
+ for i in '`seq 1 100`'
+ sum=325
+ echo '325 + 25 = 325'
325 + 25 = 325
+ for i in '`seq 1 100`'
+ sum=351
+ echo '351 + 26 = 351'
351 + 26 = 351
+ for i in '`seq 1 100`'
+ sum=378
+ echo '378 + 27 = 378'
378 + 27 = 378
+ for i in '`seq 1 100`'
+ sum=406
+ echo '406 + 28 = 406'
406 + 28 = 406
+ for i in '`seq 1 100`'
+ sum=435
+ echo '435 + 29 = 435'
435 + 29 = 435
+ for i in '`seq 1 100`'
+ sum=465
+ echo '465 + 30 = 465'
465 + 30 = 465
+ for i in '`seq 1 100`'
+ sum=496
+ echo '496 + 31 = 496'
496 + 31 = 496
+ for i in '`seq 1 100`'
+ sum=528
+ echo '528 + 32 = 528'
528 + 32 = 528
+ for i in '`seq 1 100`'
+ sum=561
+ echo '561 + 33 = 561'
561 + 33 = 561
+ for i in '`seq 1 100`'
+ sum=595
+ echo '595 + 34 = 595'
595 + 34 = 595
+ for i in '`seq 1 100`'
+ sum=630
+ echo '630 + 35 = 630'
630 + 35 = 630
+ for i in '`seq 1 100`'
+ sum=666
+ echo '666 + 36 = 666'
666 + 36 = 666
+ for i in '`seq 1 100`'
+ sum=703
+ echo '703 + 37 = 703'
703 + 37 = 703
+ for i in '`seq 1 100`'
+ sum=741
+ echo '741 + 38 = 741'
741 + 38 = 741
+ for i in '`seq 1 100`'
+ sum=780
+ echo '780 + 39 = 780'
780 + 39 = 780
+ for i in '`seq 1 100`'
+ sum=820
+ echo '820 + 40 = 820'
820 + 40 = 820
+ for i in '`seq 1 100`'
+ sum=861
+ echo '861 + 41 = 861'
861 + 41 = 861
+ for i in '`seq 1 100`'
+ sum=903
+ echo '903 + 42 = 903'
903 + 42 = 903
+ for i in '`seq 1 100`'
+ sum=946
+ echo '946 + 43 = 946'
946 + 43 = 946
+ for i in '`seq 1 100`'
+ sum=990
+ echo '990 + 44 = 990'
990 + 44 = 990
+ for i in '`seq 1 100`'
+ sum=1035
+ echo '1035 + 45 = 1035'
1035 + 45 = 1035
+ for i in '`seq 1 100`'
+ sum=1081
+ echo '1081 + 46 = 1081'
1081 + 46 = 1081
+ for i in '`seq 1 100`'
+ sum=1128
+ echo '1128 + 47 = 1128'
1128 + 47 = 1128
+ for i in '`seq 1 100`'
+ sum=1176
+ echo '1176 + 48 = 1176'
1176 + 48 = 1176
+ for i in '`seq 1 100`'
+ sum=1225
+ echo '1225 + 49 = 1225'
1225 + 49 = 1225
+ for i in '`seq 1 100`'
+ sum=1275
+ echo '1275 + 50 = 1275'
1275 + 50 = 1275
+ for i in '`seq 1 100`'
+ sum=1326
+ echo '1326 + 51 = 1326'
1326 + 51 = 1326
+ for i in '`seq 1 100`'
+ sum=1378
+ echo '1378 + 52 = 1378'
1378 + 52 = 1378
+ for i in '`seq 1 100`'
+ sum=1431
+ echo '1431 + 53 = 1431'
1431 + 53 = 1431
+ for i in '`seq 1 100`'
+ sum=1485
+ echo '1485 + 54 = 1485'
1485 + 54 = 1485
+ for i in '`seq 1 100`'
+ sum=1540
+ echo '1540 + 55 = 1540'
1540 + 55 = 1540
+ for i in '`seq 1 100`'
+ sum=1596
+ echo '1596 + 56 = 1596'
1596 + 56 = 1596
+ for i in '`seq 1 100`'
+ sum=1653
+ echo '1653 + 57 = 1653'
1653 + 57 = 1653
+ for i in '`seq 1 100`'
+ sum=1711
+ echo '1711 + 58 = 1711'
1711 + 58 = 1711
+ for i in '`seq 1 100`'
+ sum=1770
+ echo '1770 + 59 = 1770'
1770 + 59 = 1770
+ for i in '`seq 1 100`'
+ sum=1830
+ echo '1830 + 60 = 1830'
1830 + 60 = 1830
+ for i in '`seq 1 100`'
+ sum=1891
+ echo '1891 + 61 = 1891'
1891 + 61 = 1891
+ for i in '`seq 1 100`'
+ sum=1953
+ echo '1953 + 62 = 1953'
1953 + 62 = 1953
+ for i in '`seq 1 100`'
+ sum=2016
+ echo '2016 + 63 = 2016'
2016 + 63 = 2016
+ for i in '`seq 1 100`'
+ sum=2080
+ echo '2080 + 64 = 2080'
2080 + 64 = 2080
+ for i in '`seq 1 100`'
+ sum=2145
+ echo '2145 + 65 = 2145'
2145 + 65 = 2145
+ for i in '`seq 1 100`'
+ sum=2211
+ echo '2211 + 66 = 2211'
2211 + 66 = 2211
+ for i in '`seq 1 100`'
+ sum=2278
+ echo '2278 + 67 = 2278'
2278 + 67 = 2278
+ for i in '`seq 1 100`'
+ sum=2346
+ echo '2346 + 68 = 2346'
2346 + 68 = 2346
+ for i in '`seq 1 100`'
+ sum=2415
+ echo '2415 + 69 = 2415'
2415 + 69 = 2415
+ for i in '`seq 1 100`'
+ sum=2485
+ echo '2485 + 70 = 2485'
2485 + 70 = 2485
+ for i in '`seq 1 100`'
+ sum=2556
+ echo '2556 + 71 = 2556'
2556 + 71 = 2556
+ for i in '`seq 1 100`'
+ sum=2628
+ echo '2628 + 72 = 2628'
2628 + 72 = 2628
+ for i in '`seq 1 100`'
+ sum=2701
+ echo '2701 + 73 = 2701'
2701 + 73 = 2701
+ for i in '`seq 1 100`'
+ sum=2775
+ echo '2775 + 74 = 2775'
2775 + 74 = 2775
+ for i in '`seq 1 100`'
+ sum=2850
+ echo '2850 + 75 = 2850'
2850 + 75 = 2850
+ for i in '`seq 1 100`'
+ sum=2926
+ echo '2926 + 76 = 2926'
2926 + 76 = 2926
+ for i in '`seq 1 100`'
+ sum=3003
+ echo '3003 + 77 = 3003'
3003 + 77 = 3003
+ for i in '`seq 1 100`'
+ sum=3081
+ echo '3081 + 78 = 3081'
3081 + 78 = 3081
+ for i in '`seq 1 100`'
+ sum=3160
+ echo '3160 + 79 = 3160'
3160 + 79 = 3160
+ for i in '`seq 1 100`'
+ sum=3240
+ echo '3240 + 80 = 3240'
3240 + 80 = 3240
+ for i in '`seq 1 100`'
+ sum=3321
+ echo '3321 + 81 = 3321'
3321 + 81 = 3321
+ for i in '`seq 1 100`'
+ sum=3403
+ echo '3403 + 82 = 3403'
3403 + 82 = 3403
+ for i in '`seq 1 100`'
+ sum=3486
+ echo '3486 + 83 = 3486'
3486 + 83 = 3486
+ for i in '`seq 1 100`'
+ sum=3570
+ echo '3570 + 84 = 3570'
3570 + 84 = 3570
+ for i in '`seq 1 100`'
+ sum=3655
+ echo '3655 + 85 = 3655'
3655 + 85 = 3655
+ for i in '`seq 1 100`'
+ sum=3741
+ echo '3741 + 86 = 3741'
3741 + 86 = 3741
+ for i in '`seq 1 100`'
+ sum=3828
+ echo '3828 + 87 = 3828'
3828 + 87 = 3828
+ for i in '`seq 1 100`'
+ sum=3916
+ echo '3916 + 88 = 3916'
3916 + 88 = 3916
+ for i in '`seq 1 100`'
+ sum=4005
+ echo '4005 + 89 = 4005'
4005 + 89 = 4005
+ for i in '`seq 1 100`'
+ sum=4095
+ echo '4095 + 90 = 4095'
4095 + 90 = 4095
+ for i in '`seq 1 100`'
+ sum=4186
+ echo '4186 + 91 = 4186'
4186 + 91 = 4186
+ for i in '`seq 1 100`'
+ sum=4278
+ echo '4278 + 92 = 4278'
4278 + 92 = 4278
+ for i in '`seq 1 100`'
+ sum=4371
+ echo '4371 + 93 = 4371'
4371 + 93 = 4371
+ for i in '`seq 1 100`'
+ sum=4465
+ echo '4465 + 94 = 4465'
4465 + 94 = 4465
+ for i in '`seq 1 100`'
+ sum=4560
+ echo '4560 + 95 = 4560'
4560 + 95 = 4560
+ for i in '`seq 1 100`'
+ sum=4656
+ echo '4656 + 96 = 4656'
4656 + 96 = 4656
+ for i in '`seq 1 100`'
+ sum=4753
+ echo '4753 + 97 = 4753'
4753 + 97 = 4753
+ for i in '`seq 1 100`'
+ sum=4851
+ echo '4851 + 98 = 4851'
4851 + 98 = 4851
+ for i in '`seq 1 100`'
+ sum=4950
+ echo '4950 + 99 = 4950'
4950 + 99 = 4950
+ for i in '`seq 1 100`'
+ sum=5050
+ echo '5050 + 100 = 5050'
5050 + 100 = 5050
+ echo 5050
5050
添加了一行在脚本中,表示每次的执行过程都列出来
第二种是文件列表循环
需求:列出etc下所有的目录,编写脚本
[root@liuhongwei-01 shell]# vim 6.sh
#!/bin/bash
cd /etc/
for a in `ls /etc/`
do
if [ -d $a ]
then
echo $a
ls $a
fi
done
执行过程
[root@liuhongwei-01 shell]# sh 6.sh
alternatives
ld mta-aliasesman mta-newaliases mta-rmail zabbix-server
libnssckbi.so.x86_64 mta-mailq mta-newaliasesman mta-sendmail zabbix-web-font
mta mta-mailqman mta-pam mta-sendmailman
audisp
audispd.conf plugins.d
audit
auditd.conf audit.rules audit.rules.prev rules.d
avahi
avahi-autoipd.action
bash_completion.d
iprutils yum yummain.py
binfmt.d
chkconfig.d
cron.d
0hourly sysstat
cron.daily
0yum-daily.cron logrotate man-db.cron
cron.hourly
0anacron 0yum-hourly.cron
cron.monthly
cron.weekly
dbus-1
session.conf session.d system.conf system.d
default
grub nss useradd
depmod.d
dist.conf
dhcp
dhclient.d
dnsmasq.d
dracut.conf.d
90-eno-fix.conf
exports.d
firewalld
firewalld.conf icmptypes lockdown-whitelist.xml services zones
fonts
conf.d
gcrypt
gnupg
groff
site-font site-tmac
grub.d
00_header 01_users 20_linux_xen 30_os-prober 41_custom
00_tuned 10_linux 20_ppc_terminfo 40_custom README
gss
mech.d
gssproxy
24-nfs-server.conf 99-nfs-client.conf gssproxy.conf
httpd
conf conf.d conf.modules.d logs modules run
init.d
functions mariadb mysqld netconsole network nginx php-fpm README
iproute2
ematch_map group rt_dsfield rt_protos rt_realms rt_scopes rt_tables
keepalived
keepalived.conf
kernel
postinst.d
krb5.conf.d
ld.so.conf.d
kernel-3.10.0-327.el7.x86_64.conf mariadb-x86_64.conf
libnl
classid pktloc
logrotate.d
httpd ppp pure-ftpd syslog vsftpd wpa_supplicant yum zabbix-agent zabbix-server
lvm
archive backup cache lvm.conf lvmlocal.conf profile
modprobe.d
lockd.conf mlx4.conf
modules-load.d
my.cnf.d
mysql-clients.cnf
NetworkManager
conf.d dispatcher.d dnsmasq.d NetworkManager.conf system-connections VPN
ntp
keys step-tickers
openldap
certs ldap.conf
opt
pam.d
chfn login postlogin runuser sshd system-auth-ac
chsh other postlogin-ac runuser-l su systemd-user
config-util passwd ppp smartcard-auth sudo vlock
crond password-auth pure-ftpd smartcard-auth-ac sudo-i vsftpd
fingerprint-auth password-auth-ac pure-ftpwho smtp su-l
fingerprint-auth-ac polkit-1 remote smtp.postfix system-auth
php.d
bcmath.ini fileinfo.ini ldap.ini mysql.ini pdo_sqlite.ini wddx.ini xsl.ini
curl.ini gd.ini mbstring.ini pdo.ini phar.ini xmlreader.ini zip.ini
dom.ini json.ini mysqli.ini pdo_mysql.ini sqlite3.ini xmlwriter.ini
pkcs11
modules
pki
CA ca-trust java nssdb pure-ftpd rpm-gpg rsyslog tls
plymouth
plymouthd.conf
pm
config.d power.d sleep.d
polkit-1
localauthority localauthority.conf.d rules.d
popt.d
postfix
access canonical generic header_checks main.cf master.cf relocated transport virtual
ppp
chap-secrets eaptls-server ip-down.ipv6to4 ip-up.ipv6to4 ipv6-up pap-secrets
eaptls-client ip-down ip-up ipv6-down options peers
prelink.conf.d
fipscheck.conf grub2.conf nss-softokn-prelink.conf
profile.d
256term.csh colorgrep.csh colorls.csh lang.csh less.csh vim.csh which2.csh
256term.sh colorgrep.sh colorls.sh lang.sh less.sh vim.sh which2.sh
pure-ftpd
pure-ftpd.conf pureftpd-ldap.conf pureftpd-mysql.conf pureftpd.passwd pureftpd.pdb pureftpd-pgsql.conf
python
cert-verification.cfg
rc0.d
K21nginx K36mysqld K50netconsole K50php-fpm K90network
rc1.d
K21nginx K36mysqld K50netconsole K50php-fpm K90network
rc2.d
K21nginx K50netconsole S10network S50php-fpm S64mysqld
rc3.d
K21nginx K50netconsole S10network S50php-fpm S64mysqld
rc4.d
K21nginx K50netconsole S10network S50php-fpm S64mysqld
rc5.d
K21nginx K50netconsole S10network S50php-fpm S64mysqld
rc6.d
K21nginx K36mysqld K50netconsole K50php-fpm K90network
rc.d
init.d rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d rc.local
rdma
mlx4.conf rdma.conf sriov-vfs
repos.d_bak
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo
request-key.d
id_resolver.conf
rpm
macros.dist macros.perl
rsyslog.d
listen.conf
rwtab.d
sasl2
smtpd.conf
security
access.conf console.handlers group.conf namespace.conf opasswd sepermit.conf
chroot.conf console.perms limits.conf namespace.d pam_env.conf time.conf
console.apps console.perms.d limits.d namespace.init pwquality.conf
selinux
config final semanage.conf targeted tmp
skel
ssh
moduli sshd_config ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
ssh_config ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
ssl
certs
statetab.d
sudoers.d
sysconfig
authconfig firewalld ip6tables-config kdump network rpcbind selinux
cbq grub iptables keepalived network-scripts rpc-rquotad sshd
console htcacheclean iptables-config kernel nfs rsyncd sysstat
cpupower httpd iptables.save man-db ntpdate rsyslog sysstat.ioconf
crond init ipvsadm-config modules rdisc run-parts wpa_supplicant
ebtables-config ip6tables irqbalance netconsole readonly-root saslauthd
sysctl.d
99-sysctl.conf
systemd
bootchart.conf coredump.conf journald.conf logind.conf system system.conf user user.conf
terminfo
tmpfiles.d
python.conf
tuned
active_profile bootcmdline tuned-main.conf
udev
hwdb.bin rules.d udev.conf
vsftpd
ftpusers user_list vsftpd.conf vsftpd_conf_migrate.sh vsftpd_login vsftpd_login.db vsftpd_user_conf
wpa_supplicant
wpa_supplicant.conf
X11
applnk fontpath.d xorg.conf.d
xdg
autostart systemd
xinetd.d
pure-ftpd
yum
fssnap.d pluginconf.d protected.d vars version-groups.conf yum-cron.conf yum-cron-hourly.conf
yum.repos.d
CentOS7-Base-163.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo epel-testing.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo epel.repo.1 zabbix.repo
zabbix
web zabbix_agentd.conf zabbix_agentd.d zabbix_server.conf
[root@liuhongwei-01 shell]# sh -x 6.sh
+ cd /etc/
++ ls /etc/
+ for a in '`ls /etc/`'
+ '[' -d adjtime ']'
+ for a in '`ls /etc/`'
+ '[' -d aliases ']'
+ for a in '`ls /etc/`'
+ '[' -d aliases.db ']'
+ for a in '`ls /etc/`'
+ '[' -d alternatives ']'
+ ls alternatives
ld mta-aliasesman mta-newaliases mta-rmail zabbix-server
libnssckbi.so.x86_64 mta-mailq mta-newaliasesman mta-sendmail zabbix-web-font
mta mta-mailqman mta-pam mta-sendmailman
+ for a in '`ls /etc/`'
+ '[' -d anacrontab ']'
+ for a in '`ls /etc/`'
+ '[' -d asound.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d audisp ']'
+ ls audisp
audispd.conf plugins.d
+ for a in '`ls /etc/`'
+ '[' -d audit ']'
+ ls audit
auditd.conf audit.rules audit.rules.prev rules.d
+ for a in '`ls /etc/`'
+ '[' -d avahi ']'
+ ls avahi
avahi-autoipd.action
+ for a in '`ls /etc/`'
+ '[' -d bash_completion.d ']'
+ ls bash_completion.d
iprutils yum yummain.py
+ for a in '`ls /etc/`'
+ '[' -d bashrc ']'
+ for a in '`ls /etc/`'
+ '[' -d binfmt.d ']'
+ ls binfmt.d
+ for a in '`ls /etc/`'
+ '[' -d centos-release ']'
+ for a in '`ls /etc/`'
+ '[' -d centos-release-upstream ']'
+ for a in '`ls /etc/`'
+ '[' -d chkconfig.d ']'
+ ls chkconfig.d
+ for a in '`ls /etc/`'
+ '[' -d cron.d ']'
+ ls cron.d
0hourly sysstat
+ for a in '`ls /etc/`'
+ '[' -d cron.daily ']'
+ ls cron.daily
0yum-daily.cron logrotate man-db.cron
+ for a in '`ls /etc/`'
+ '[' -d cron.deny ']'
+ for a in '`ls /etc/`'
+ '[' -d cron.hourly ']'
+ ls cron.hourly
0anacron 0yum-hourly.cron
+ for a in '`ls /etc/`'
+ '[' -d cron.monthly ']'
+ ls cron.monthly
+ for a in '`ls /etc/`'
+ '[' -d crontab ']'
+ for a in '`ls /etc/`'
+ '[' -d cron.weekly ']'
+ ls cron.weekly
+ for a in '`ls /etc/`'
+ '[' -d crypttab ']'
+ for a in '`ls /etc/`'
+ '[' -d csh.cshrc ']'
+ for a in '`ls /etc/`'
+ '[' -d csh.login ']'
+ for a in '`ls /etc/`'
+ '[' -d dbus-1 ']'
+ ls dbus-1
session.conf session.d system.conf system.d
+ for a in '`ls /etc/`'
+ '[' -d default ']'
+ ls default
grub nss useradd
+ for a in '`ls /etc/`'
+ '[' -d depmod.d ']'
+ ls depmod.d
dist.conf
+ for a in '`ls /etc/`'
+ '[' -d dhcp ']'
+ ls dhcp
dhclient.d
+ for a in '`ls /etc/`'
+ '[' -d DIR_COLORS ']'
+ for a in '`ls /etc/`'
+ '[' -d DIR_COLORS.256color ']'
+ for a in '`ls /etc/`'
+ '[' -d DIR_COLORS.lightbgcolor ']'
+ for a in '`ls /etc/`'
+ '[' -d dnsmasq.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d dnsmasq.d ']'
+ ls dnsmasq.d
+ for a in '`ls /etc/`'
+ '[' -d dracut.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d dracut.conf.d ']'
+ ls dracut.conf.d
90-eno-fix.conf
+ for a in '`ls /etc/`'
+ '[' -d e2fsck.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d environment ']'
+ for a in '`ls /etc/`'
+ '[' -d ethertypes ']'
+ for a in '`ls /etc/`'
+ '[' -d exports ']'
+ for a in '`ls /etc/`'
+ '[' -d exports.d ']'
+ ls exports.d
+ for a in '`ls /etc/`'
+ '[' -d favicon.png ']'
+ for a in '`ls /etc/`'
+ '[' -d filesystems ']'
+ for a in '`ls /etc/`'
+ '[' -d firewalld ']'
+ ls firewalld
firewalld.conf icmptypes lockdown-whitelist.xml services zones
+ for a in '`ls /etc/`'
+ '[' -d fonts ']'
+ ls fonts
conf.d
+ for a in '`ls /etc/`'
+ '[' -d fstab ']'
+ for a in '`ls /etc/`'
+ '[' -d gcrypt ']'
+ ls gcrypt
+ for a in '`ls /etc/`'
+ '[' -d GeoIP.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d GeoIP.conf.default ']'
+ for a in '`ls /etc/`'
+ '[' -d gnupg ']'
+ ls gnupg
+ for a in '`ls /etc/`'
+ '[' -d GREP_COLORS ']'
+ for a in '`ls /etc/`'
+ '[' -d groff ']'
+ ls groff
site-font site-tmac
+ for a in '`ls /etc/`'
+ '[' -d group ']'
+ for a in '`ls /etc/`'
+ '[' -d group- ']'
+ for a in '`ls /etc/`'
+ '[' -d grub2.cfg ']'
+ for a in '`ls /etc/`'
+ '[' -d grub.d ']'
+ ls grub.d
00_header 01_users 20_linux_xen 30_os-prober 41_custom
00_tuned 10_linux 20_ppc_terminfo 40_custom README
+ for a in '`ls /etc/`'
+ '[' -d gshadow ']'
+ for a in '`ls /etc/`'
+ '[' -d gshadow- ']'
+ for a in '`ls /etc/`'
+ '[' -d gss ']'
+ ls gss
mech.d
+ for a in '`ls /etc/`'
+ '[' -d gssproxy ']'
+ ls gssproxy
24-nfs-server.conf 99-nfs-client.conf gssproxy.conf
+ for a in '`ls /etc/`'
+ '[' -d host.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d hostname ']'
+ for a in '`ls /etc/`'
+ '[' -d hosts ']'
+ for a in '`ls /etc/`'
+ '[' -d hosts.allow ']'
+ for a in '`ls /etc/`'
+ '[' -d hosts.deny ']'
+ for a in '`ls /etc/`'
+ '[' -d httpd ']'
+ ls httpd
conf conf.d conf.modules.d logs modules run
+ for a in '`ls /etc/`'
+ '[' -d idmapd.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d init.d ']'
+ ls init.d
functions mariadb mysqld netconsole network nginx php-fpm README
+ for a in '`ls /etc/`'
+ '[' -d inittab ']'
+ for a in '`ls /etc/`'
+ '[' -d inputrc ']'
+ for a in '`ls /etc/`'
+ '[' -d iproute2 ']'
+ ls iproute2
ematch_map group rt_dsfield rt_protos rt_realms rt_scopes rt_tables
+ for a in '`ls /etc/`'
+ '[' -d issue ']'
+ for a in '`ls /etc/`'
+ '[' -d issue.net ']'
+ for a in '`ls /etc/`'
+ '[' -d kdump.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d keepalived ']'
+ ls keepalived
keepalived.conf
+ for a in '`ls /etc/`'
+ '[' -d kernel ']'
+ ls kernel
postinst.d
+ for a in '`ls /etc/`'
+ '[' -d krb5.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d krb5.conf.d ']'
+ ls krb5.conf.d
+ for a in '`ls /etc/`'
+ '[' -d ld.so.cache ']'
+ for a in '`ls /etc/`'
+ '[' -d ld.so.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d ld.so.conf.d ']'
+ ls ld.so.conf.d
kernel-3.10.0-327.el7.x86_64.conf mariadb-x86_64.conf
+ for a in '`ls /etc/`'
+ '[' -d lftp.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d libaudit.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d libnl ']'
+ ls libnl
classid pktloc
+ for a in '`ls /etc/`'
+ '[' -d libuser.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d locale.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d localtime ']'
+ for a in '`ls /etc/`'
+ '[' -d login.defs ']'
+ for a in '`ls /etc/`'
+ '[' -d logrotate.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d logrotate.d ']'
+ ls logrotate.d
httpd ppp pure-ftpd syslog vsftpd wpa_supplicant yum zabbix-agent zabbix-server
+ for a in '`ls /etc/`'
+ '[' -d lvm ']'
+ ls lvm
archive backup cache lvm.conf lvmlocal.conf profile
+ for a in '`ls /etc/`'
+ '[' -d machine-id ']'
+ for a in '`ls /etc/`'
+ '[' -d magic ']'
+ for a in '`ls /etc/`'
+ '[' -d mailcap ']'
+ for a in '`ls /etc/`'
+ '[' -d makedumpfile.conf.sample ']'
+ for a in '`ls /etc/`'
+ '[' -d man_db.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d mime.types ']'
+ for a in '`ls /etc/`'
+ '[' -d mke2fs.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d modprobe.d ']'
+ ls modprobe.d
lockd.conf mlx4.conf
+ for a in '`ls /etc/`'
+ '[' -d modules-load.d ']'
+ ls modules-load.d
+ for a in '`ls /etc/`'
+ '[' -d motd ']'
+ for a in '`ls /etc/`'
+ '[' -d mtab ']'
+ for a in '`ls /etc/`'
+ '[' -d my.cnf ']'
+ for a in '`ls /etc/`'
+ '[' -d my.cnf.d ']'
+ ls my.cnf.d
mysql-clients.cnf
+ for a in '`ls /etc/`'
+ '[' -d netconfig ']'
+ for a in '`ls /etc/`'
+ '[' -d NetworkManager ']'
+ ls NetworkManager
conf.d dispatcher.d dnsmasq.d NetworkManager.conf system-connections VPN
+ for a in '`ls /etc/`'
+ '[' -d networks ']'
+ for a in '`ls /etc/`'
+ '[' -d nfs.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d nfsmount.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d nsswitch.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d nsswitch.conf.bak ']'
+ for a in '`ls /etc/`'
+ '[' -d ntp ']'
+ ls ntp
keys step-tickers
+ for a in '`ls /etc/`'
+ '[' -d odbcinst.ini ']'
+ for a in '`ls /etc/`'
+ '[' -d openldap ']'
+ ls openldap
certs ldap.conf
+ for a in '`ls /etc/`'
+ '[' -d opt ']'
+ ls opt
+ for a in '`ls /etc/`'
+ '[' -d os-release ']'
+ for a in '`ls /etc/`'
+ '[' -d pam.d ']'
+ ls pam.d
chfn login postlogin runuser sshd system-auth-ac
chsh other postlogin-ac runuser-l su systemd-user
config-util passwd ppp smartcard-auth sudo vlock
crond password-auth pure-ftpd smartcard-auth-ac sudo-i vsftpd
fingerprint-auth password-auth-ac pure-ftpwho smtp su-l
fingerprint-auth-ac polkit-1 remote smtp.postfix system-auth
+ for a in '`ls /etc/`'
+ '[' -d passwd ']'
+ for a in '`ls /etc/`'
+ '[' -d passwd- ']'
+ for a in '`ls /etc/`'
+ '[' -d php.d ']'
+ ls php.d
bcmath.ini fileinfo.ini ldap.ini mysql.ini pdo_sqlite.ini wddx.ini xsl.ini
curl.ini gd.ini mbstring.ini pdo.ini phar.ini xmlreader.ini zip.ini
dom.ini json.ini mysqli.ini pdo_mysql.ini sqlite3.ini xmlwriter.ini
+ for a in '`ls /etc/`'
+ '[' -d php.ini ']'
+ for a in '`ls /etc/`'
+ '[' -d pkcs11 ']'
+ ls pkcs11
modules
+ for a in '`ls /etc/`'
+ '[' -d pki ']'
+ ls pki
CA ca-trust java nssdb pure-ftpd rpm-gpg rsyslog tls
+ for a in '`ls /etc/`'
+ '[' -d plymouth ']'
+ ls plymouth
plymouthd.conf
+ for a in '`ls /etc/`'
+ '[' -d pm ']'
+ ls pm
config.d power.d sleep.d
+ for a in '`ls /etc/`'
+ '[' -d polkit-1 ']'
+ ls polkit-1
localauthority localauthority.conf.d rules.d
+ for a in '`ls /etc/`'
+ '[' -d popt.d ']'
+ ls popt.d
+ for a in '`ls /etc/`'
+ '[' -d postfix ']'
+ ls postfix
access canonical generic header_checks main.cf master.cf relocated transport virtual
+ for a in '`ls /etc/`'
+ '[' -d ppp ']'
+ ls ppp
chap-secrets eaptls-server ip-down.ipv6to4 ip-up.ipv6to4 ipv6-up pap-secrets
eaptls-client ip-down ip-up ipv6-down options peers
+ for a in '`ls /etc/`'
+ '[' -d prelink.conf.d ']'
+ ls prelink.conf.d
fipscheck.conf grub2.conf nss-softokn-prelink.conf
+ for a in '`ls /etc/`'
+ '[' -d printcap ']'
+ for a in '`ls /etc/`'
+ '[' -d profile ']'
+ for a in '`ls /etc/`'
+ '[' -d profile.d ']'
+ ls profile.d
256term.csh colorgrep.csh colorls.csh lang.csh less.csh vim.csh which2.csh
256term.sh colorgrep.sh colorls.sh lang.sh less.sh vim.sh which2.sh
+ for a in '`ls /etc/`'
+ '[' -d protocols ']'
+ for a in '`ls /etc/`'
+ '[' -d pure-ftpd ']'
+ ls pure-ftpd
pure-ftpd.conf pureftpd-ldap.conf pureftpd-mysql.conf pureftpd.passwd pureftpd.pdb pureftpd-pgsql.conf
+ for a in '`ls /etc/`'
+ '[' -d python ']'
+ ls python
cert-verification.cfg
+ for a in '`ls /etc/`'
+ '[' -d rc0.d ']'
+ ls rc0.d
K21nginx K36mysqld K50netconsole K50php-fpm K90network
+ for a in '`ls /etc/`'
+ '[' -d rc1.d ']'
+ ls rc1.d
K21nginx K36mysqld K50netconsole K50php-fpm K90network
+ for a in '`ls /etc/`'
+ '[' -d rc2.d ']'
+ ls rc2.d
K21nginx K50netconsole S10network S50php-fpm S64mysqld
+ for a in '`ls /etc/`'
+ '[' -d rc3.d ']'
+ ls rc3.d
K21nginx K50netconsole S10network S50php-fpm S64mysqld
+ for a in '`ls /etc/`'
+ '[' -d rc4.d ']'
+ ls rc4.d
K21nginx K50netconsole S10network S50php-fpm S64mysqld
+ for a in '`ls /etc/`'
+ '[' -d rc5.d ']'
+ ls rc5.d
K21nginx K50netconsole S10network S50php-fpm S64mysqld
+ for a in '`ls /etc/`'
+ '[' -d rc6.d ']'
+ ls rc6.d
K21nginx K36mysqld K50netconsole K50php-fpm K90network
+ for a in '`ls /etc/`'
+ '[' -d rc.d ']'
+ ls rc.d
init.d rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d rc.local
+ for a in '`ls /etc/`'
+ '[' -d rc.local ']'
+ for a in '`ls /etc/`'
+ '[' -d rdma ']'
+ ls rdma
mlx4.conf rdma.conf sriov-vfs
+ for a in '`ls /etc/`'
+ '[' -d redhat-release ']'
+ for a in '`ls /etc/`'
+ '[' -d repos.d_bak ']'
+ ls repos.d_bak
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo
+ for a in '`ls /etc/`'
+ '[' -d request-key.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d request-key.d ']'
+ ls request-key.d
id_resolver.conf
+ for a in '`ls /etc/`'
+ '[' -d resolv.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d rpc ']'
+ for a in '`ls /etc/`'
+ '[' -d rpm ']'
+ ls rpm
macros.dist macros.perl
+ for a in '`ls /etc/`'
+ '[' -d rsyncd.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d rsyslog.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d rsyslog.d ']'
+ ls rsyslog.d
listen.conf
+ for a in '`ls /etc/`'
+ '[' -d rwtab ']'
+ for a in '`ls /etc/`'
+ '[' -d rwtab.d ']'
+ ls rwtab.d
+ for a in '`ls /etc/`'
+ '[' -d sasl2 ']'
+ ls sasl2
smtpd.conf
+ for a in '`ls /etc/`'
+ '[' -d securetty ']'
+ for a in '`ls /etc/`'
+ '[' -d security ']'
+ ls security
access.conf console.handlers group.conf namespace.conf opasswd sepermit.conf
chroot.conf console.perms limits.conf namespace.d pam_env.conf time.conf
console.apps console.perms.d limits.d namespace.init pwquality.conf
+ for a in '`ls /etc/`'
+ '[' -d selinux ']'
+ ls selinux
config final semanage.conf targeted tmp
+ for a in '`ls /etc/`'
+ '[' -d services ']'
+ for a in '`ls /etc/`'
+ '[' -d sestatus.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d shadow ']'
+ for a in '`ls /etc/`'
+ '[' -d shadow- ']'
+ for a in '`ls /etc/`'
+ '[' -d shells ']'
+ for a in '`ls /etc/`'
+ '[' -d skel ']'
+ ls skel
+ for a in '`ls /etc/`'
+ '[' -d ssh ']'
+ ls ssh
moduli sshd_config ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
ssh_config ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
+ for a in '`ls /etc/`'
+ '[' -d ssl ']'
+ ls ssl
certs
+ for a in '`ls /etc/`'
+ '[' -d statetab ']'
+ for a in '`ls /etc/`'
+ '[' -d statetab.d ']'
+ ls statetab.d
+ for a in '`ls /etc/`'
+ '[' -d sudo.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d sudoers ']'
+ for a in '`ls /etc/`'
+ '[' -d sudoers.d ']'
+ ls sudoers.d
+ for a in '`ls /etc/`'
+ '[' -d sudo-ldap.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d sysconfig ']'
+ ls sysconfig
authconfig firewalld ip6tables-config kdump network rpcbind selinux
cbq grub iptables keepalived network-scripts rpc-rquotad sshd
console htcacheclean iptables-config kernel nfs rsyncd sysstat
cpupower httpd iptables.save man-db ntpdate rsyslog sysstat.ioconf
crond init ipvsadm-config modules rdisc run-parts wpa_supplicant
ebtables-config ip6tables irqbalance netconsole readonly-root saslauthd
+ for a in '`ls /etc/`'
+ '[' -d sysctl.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d sysctl.d ']'
+ ls sysctl.d
99-sysctl.conf
+ for a in '`ls /etc/`'
+ '[' -d systemd ']'
+ ls systemd
bootchart.conf coredump.conf journald.conf logind.conf system system.conf user user.conf
+ for a in '`ls /etc/`'
+ '[' -d system-release ']'
+ for a in '`ls /etc/`'
+ '[' -d system-release-cpe ']'
+ for a in '`ls /etc/`'
+ '[' -d tcsd.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d terminfo ']'
+ ls terminfo
+ for a in '`ls /etc/`'
+ '[' -d tmpfiles.d ']'
+ ls tmpfiles.d
python.conf
+ for a in '`ls /etc/`'
+ '[' -d trusted-key.key ']'
+ for a in '`ls /etc/`'
+ '[' -d tuned ']'
+ ls tuned
active_profile bootcmdline tuned-main.conf
+ for a in '`ls /etc/`'
+ '[' -d udev ']'
+ ls udev
hwdb.bin rules.d udev.conf
+ for a in '`ls /etc/`'
+ '[' -d vconsole.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d vimrc ']'
+ for a in '`ls /etc/`'
+ '[' -d virc ']'
+ for a in '`ls /etc/`'
+ '[' -d vsftpd ']'
+ ls vsftpd
ftpusers user_list vsftpd.conf vsftpd_conf_migrate.sh vsftpd_login vsftpd_login.db vsftpd_user_conf
+ for a in '`ls /etc/`'
+ '[' -d wgetrc ']'
+ for a in '`ls /etc/`'
+ '[' -d wpa_supplicant ']'
+ ls wpa_supplicant
wpa_supplicant.conf
+ for a in '`ls /etc/`'
+ '[' -d X11 ']'
+ ls X11
applnk fontpath.d xorg.conf.d
+ for a in '`ls /etc/`'
+ '[' -d xdg ']'
+ ls xdg
autostart systemd
+ for a in '`ls /etc/`'
+ '[' -d xinetd.d ']'
+ ls xinetd.d
pure-ftpd
+ for a in '`ls /etc/`'
+ '[' -d yum ']'
+ ls yum
fssnap.d pluginconf.d protected.d vars version-groups.conf yum-cron.conf yum-cron-hourly.conf
+ for a in '`ls /etc/`'
+ '[' -d yum.conf ']'
+ for a in '`ls /etc/`'
+ '[' -d yum.repos.d ']'
+ ls yum.repos.d
CentOS7-Base-163.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo epel-testing.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo epel.repo.1 zabbix.repo
+ for a in '`ls /etc/`'
+ '[' -d zabbix ']'
+ ls zabbix
web zabbix_agentd.conf zabbix_agentd.d zabbix_server.conf
[root@liuhongwei-01 shell]#
还有一种情况就是
首先我们创建一个目录,然后创建文件1 2 在创建一个3 4文件(3-4之前有空格)
[root@liuhongwei-01 hongwei]# touch 1 2
[root@liuhongwei-01 hongwei]# touch 3\ 4.txt
[root@liuhongwei-01 hongwei]# ls
1 2 3 4.txt
[root@liuhongwei-01 hongwei]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 7月 29 11:09 1
-rw-r--r-- 1 root root 0 7月 29 11:09 2
-rw-r--r-- 1 root root 0 7月 29 11:09 3 4.txt
然后我们使用for循环
[root@liuhongwei-01 hongwei]# for i in `ls ./`;do echo $i;done
1
2
3
4.txt
会看到把3空格4文件给拆分了3 和 4.txt,这是因为for循环会以空格或者回车作为分隔符
20.11/20.12 while循环
语法 while 条件; do … ; done
案例需求是,每隔30秒检测系统的负载,如果高于某个值就发一封邮件,这是一封监控脚本
[root@liuhongwei-01 shell]# vim while11.sh
#!/bin/bash
while :
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
if [ $load -gt 10 ]
then
top|mail -s "load is high: $load" asldkfls@11.com
fi
sleep 30
done
上面例子中:表示真的意思,可以写成1 或者true,然后定义一个load变量,w查看负载,列出第一行,使用awk截取load average字段,打印第二段的值,并且用切割第一个数值,用空格分开,然后当这个值大于10的时候就发一封邮件,然后加上时间30秒查一次
案例2
[root@liuhongwei-01 shell]# vim 11.sh
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "你需要输入东西"
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "你必须输入一个纯数字"
continue
fi
break
done
echo $n
这个脚本的意思是输入一个数字怎么样,输入空怎么样,输入非数字怎么样,continue表示继续你上面的命令去循环,下面的代码不管,break表示跳出上面的循环,然后输出结果。
下面执行一下看看
[root@liuhongwei-01 shell]# sh -x 11.sh
+ :
+ read -p 'Please input a number: ' n
Please input a number:
+ '[' -z '' ']'
+ echo $'\344\275\240\351\234\200\350\246\201\350\276\223\345\205\245\344\270\234\350\245\277'
你需要输入东西
+ continue
+ :
+ read -p 'Please input a number: ' n
Please input a number: erer
+ '[' -z erer ']'
++ echo erer
++ sed 's/[0-9]//g'
+ n1=erer
+ '[' -n erer ']'
+ echo $'\344\275\240\345\277\205\351\241\273\350\276\223\345\205\245\344\270\200\344\270\252\347\272\257\346\225\260\345\255\227'
你必须输入一个纯数字
+ continue
+ :
+ read -p 'Please input a number: ' n
Please input a number: 23
+ '[' -z 23 ']'
++ echo 23
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ break
+ echo 23
23
[root@liuhongwei-01 shell]#
[root@liuhongwei-01 shell]# sh 11.sh
Please input a number:
你需要输入东西
Please input a number: qqq
你必须输入一个纯数字
Please input a number: 12
12
[root@liuhongwei-01 shell]#
20.13 break跳出循环
break表示跳出这个循环,也就是满足这个条件就退出循环
20.14 continue结束本次循环
continue表示继续上面的循环,就是上面的代码再来一遍,下面的代码不管。
20.15 exit退出整个脚本
满足所有条件,或者不满足时就退出整个脚本,不执行了。