20.1 Shell脚本介绍
1. shell是一种脚本语言 aming_linux blog.lishiming.net
2. 可以使用逻辑判断、循环等语法
3. 可以自定义函数
4. shell是系统命令的集合
5. shell脚本可以实现自动化运维,能大大增加我们的运维效率
20.2 Shell脚本结构和执行
1. 开头(首行)需要加 : #!/bin/bash
2. 以#开头的行作为解释说明 :
3. 脚本的名字以.sh结尾,用于区分这是一个shell脚本
4. 执行.sh脚本方法有两种 :
1) 先给.sh脚本添加x权限:[root@hao-01 ~]# chmod +x 1.sh
.sh脚本的绝对路径回车:[root@hao-01 ~]# /root/1.sh
2) bash(bash=sh)执行.sh脚本 :[root@hao-01 ~]# bash 1.sh
5. 查看脚本执行过程 :[root@hao-01 ~]# sh -x 1.sh
6. 检测脚本是否有语法错误 ?[root@hao-01 ~]# sh -n 1.sh
20.3 date命令用法
1. 查看当前时间 :[root@hao-01 ~]# date
2.年:[root@hao-01 ~]#date +%Y 或 [root@hao-01 ~]#date +%y
月:[root@hao-01 ~]#date +%m
分钟:[root@hao-01 ~]#date +%M
日期:[root@hao-01 ~]# date +%d
月日年:[root@hao-01 ~]# date +%D
年月日:[root@hao-01 ~]# date +%Y%m%d
年-月-日:[root@hao-01 ~]# date +%F
小时:[root@hao-01 ~]# date +%H
秒:[root@hao-01 ~]# date +%S
时间戳(距离1970年1月1日零点零分到现在有多少秒):[root@hao-01 ~]# date +%s
时:分钟:秒:[root@hao-01 ~]# date +%H:%M:%S 或 [root@hao-01 ~]# date +%T
周(星期几):[root@hao-01 ~]# date +%w
今年第几周(第几个星期):[root@hao-01 ~]# date +%W
显示日历:[root@hao-01 ~]# cal
前一天(昨天):[root@hao-01 ~]# date -d "-1 day" +%F
上个月:[root@hao-01 ~]# date -d "-1 month" +%F
前一年(去年):[root@hao-01 ~]# date -d "-1 years" +%F
前一小时:[root@hao-01 ~]# date -d "-1 hour" +%F
前一分钟:[root@hao-01 ~]# date -d "-1 min" +%F
时间戳(当前时间到1970年1月1日零点零分有多少秒):[root@hao-01 ~]# date +%s
通过时间戳,翻译成日期时间:[root@hao-01 ~]# date -d @1505206911
通过日期时间,转换成时间戳:[root@hao-01 ~]# date +%s -d "2017-09-12 17:01:51"
20.4 Shell脚本中的变量
1. 当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
2. 使用条件语句时,常使用变量 if [ $a -gt 1 ]; then ... ; fi
3. 引用某个命令的结果时,用变量替代 n=`wc -l 1.txt`
4. 写和用户交互的脚本时,变量也是必不可少的 read -p "Input a number: " n; echo $n 如果没写这个n,可以直接使用$REPLY
5. 内置变量 $0, $1, $2… $0表示脚本本身,$1 第一个参数,$2 第二个 .... $#表示参数个数
6. 数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]
20.5 Shell脚本中的逻辑判断
格式1:if 条件 ; then 语句; fi
1. 创建if1.sh测试脚本:[root@hao-01 ~]# vi if1.sh
a=5,如果a大于3,满足这个条件,显示ok , 添加内容:
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi
2. 执行if1.sh脚本:[root@hao-01 ~]# sh if1.sh
格式2:if 条件; then 语句; else 语句; fi
1. 创建if2.sh测试脚本:[root@hao-01 ~]# vi if2.sh
a=1,如果a大于3,满足这个条件,显示ok; 不满足这个条件,显示nook
添加内容:
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
else
echo noook
fi
2. 执行if2.sh脚本:[root@hao-01 ~]# sh if2.sh
查看执行过程:[root@hao-01 ~]# sh -x if2.sh
格式3:if …; then … ;elif …; then …; else …; fi
1. 创建if3.sh测试脚本:
a=3,如果a大于4,(不满足继续判断)a大于6; 满足小于6并且大于1,显示nook , 添加内容:
#!/bin/bash
a=3
if [ $a -gt 4 ]
then
echo ">"
elif [ $a -gt 6 ]
then
echo "<6 && >1"
else
echo nook
fi
2. 执行if3.sh脚本:[root@hao-01 ~]# sh if3.sh
查看执行过程:[root@hao-01 ~]# sh -x if3.sh
逻辑判断表达式:
-gt(>) -lt(<) -eq(==) -ne(!=) -ge(>=) -le(<=)
-gt大于 -lt小于 -eq等于 -ne不等于 -ge大于等于 -le小于等于
例:if [ $a -gt $b ] if [ $a -lt 5 ] if [ $b -eq 10 ] # 可以使用 &&并且 ||或者 多个判断条件
例: if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then
20.6 文件目录属性判断
[ -f file ]判断是否是普通文件,且存在
1. 创建file1.sh测试脚本:[root@hao-01 ~]# vi file1.sh
判断/tmp/hao.txt是不是普通文件?是否存在?如果不存在,创建这个文件!添加内容:
#!/bin/bash
f="/tmp/hao.txt"
if [ -f $f ]
then
echo $f exist
else
touch $f # 创建
fi
2. 执行file1.sh脚本(文件不存在的情况下):[root@hao-01 ~]# sh -x file1.sh
执行filel.sh脚本(文件存在的情况下):[root@hao-01 ~]# sh -x file1.sh
[ -d file ] 判断是否是目录,且存在
1. 创建file2.sh测试脚本:[root@hao-01 ~]# vi file2.sh
判断/tmp/hao.txt是不是目录?是否存在?如果不是目录,也不存在这样的目录,创建这个目录! 添加内容:
#!/bin/bash
f="/tmp/hao.txt"
if [ -d $f ]
then
echo $f exist
else
touch $f
fi
2. 执行file2.sh脚本(文件不存在的情况下):[root@hao-01 ~]# sh -x file2.sh
[ -e file ] 判断文件或目录是否存在
1. 创建file3.sh测试脚本:[root@hao-01 ~]# vi file3.sh
判断/tmp/hao.txt不管是目录或文件,只要不存在的,就创建文件或目录!添加内容:
#!/bin/bash
f="/tmp/hao.txt"
if [ -e $f ]
then
echo $f exist
else
touch $f
fi
2. 执行file3.sh脚本[root@hao-01 ~]# sh -x file3.sh
[ -r file ] 判断文件是否可读
1. 创建file4.sh测试脚本:判断/tmp/hao.txt文件是否可读?
[root@hao-01 ~]# vim file4.sh # 添加内容:
#!/bin/bash
f="/tmp/hao.txt"
if [ -r $f ]
then
echo $f readable
fi
2. 执行file4.sh脚本 : [root@hao-01 ~]# sh -x file4.sh
[ -w file ] 判断文件是否可写
1. 创建file5.sh测试脚本:判断/tmp/hao.txt文件是否可写?
[root@hao-01 ~]# vim file5.sh # 添加内容:
#!/bin/bash
f="/tmp/hao.txt"
if [ -w $f ]
then
echo $f writeable
fi
2. 执行file5.sh脚本
[root@hao-01 ~]# sh -x file5.sh #
[ -x file ] 判断文件是否可执行
1. 创建file5.sh测试脚本:判断/tmp/hao.txt文件是否可执行?
[root@hao-01 ~]# vim file6.sh # 添加内容:
#!/bin/bash
f="/tmp/hao.txt"
if [ -x $f ]
then
echo $f exeable
fi
2. 执行file56.sh脚本(没有输出就是不可执行!可是脚本配置也是权限) [root@hao-01 ~]# sh -x file6.sh
20.7 if特殊用法
if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
if [ -n "$a" ] 表示当变量a的值不为空
if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样 取反
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
grep结合if示例:
-q 参数,本意是 Quiet:不打印 -w精准匹配 # 如果root存在就输入存在
20.8 cace判断(上) 20.9 cace判断(下)
格式:在case程序中,可以再条件中使用 | ,表示或的意思
case 变量名 in
value1) ---第一个判断
command
;;
value2) ---第二个判断
command
;;
*) --除此之外
command
;;
esac
#!/bin/bash
read -p "Please input a number: " n ---让用户输入数字,返回值为n
if [ -z "$n" ] ---当为空时
then
echo "Please input a number." --提示请输入值
exit 1 --退出,当用户运行完脚本运行echo$? 的时候会返回1
fi
n1=`echo $n|sed 's/[0-9]//g'` --输出是否为纯数字,如果是数字,则清空,赋值给$n1
if [ -n "$n1" ] --判断$n1是否为空(即$n1不是纯数字)
then
echo "Please input a number." --为空再次提示输入数字
exit 1 --退出
fi
if [ $n -lt 60 ] && [ $n -ge 0 ] ---当数值小于60 且大于等于0 提示tag1
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ] ---当数值大于等于60并且小于80 提示tag2
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ] ---当数值大于等于80且小于90 提示tag3
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ] ---当数值大于等于90且小于100 提示tag4
then
tag=4
else
tag=0
fi
case $tag in --当你的tag值为1/2/3/4、的时候执行如下
1)
echo "not ok" ---tag1提示not ok
;;
2)
echo "ok" --tag2提示 ok
;;
3)
echo "ook" -tag3提示 ook
;;
4)
echo "oook" ---tag4提示 oook
;;
*) --除此之外的执行tag=0
echo "The number range is 0-100." --提示重新输入数值范围为0-100
;;
esac
执行结果100
执行结果101:提示不在此范围
执行过程
20.10 for循环
案例1
1. 编写for循环脚本:计算1到100所有数字和
[root@hao-01 ~]# vi for1.sh # 添加内容 :
#!/bin/bash
sum=0
for i in `seq 1 100`
do
echo "$sum + $i"
sum=$[$sum+$i]
echo $sum
done
echo $sum
2. 执行for1.sh脚本 :[root@hao-01 ~]# sh for1.sh
案例2
1. 文件列表循环(常用)
[root@hao-01 ~]# vim for2.sh # 添加内容:
#!/bin/bash
cd /etc/
for a in ls /etc/
do
if [ -d $a ] # 判断是否是目录,且存在
then
echo $a
ls $a
fi
done
2. 执行for2.sh脚本:
[root@hao-01 ~]# sh -x for2.sh
[root@hao-01 ~]# for i in `ls ./`; do echo $i ; done
20.11 while循环(上)
语法: while 条件; do 内容… ; done
1. 每隔30秒检查系统负载,当负载达到10,发一份邮件 !
[root@hao-01 ~]# vim while1.sh 添加内容:
#!/bin/bash
while true #可以写成 while :
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
if [ $load -gt 10 ]
then
/usr/local/sbin/mail.py [email protected] "load load" "$load"
fi
sleep 30
done
2. 执行while1.sh脚本 :[root@hao-01 ~]# sh -x while1.sh #显示每步执行结果
20.12 while循环(下)
1. 让用户不断的输入纯数字,才停止换算 !
[root@hao-01 ~]# vim while2.sh # 添加内容:
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ] --表示当变量的值为空时会怎么样
then
echo "you need input sth."
continue
fi
n1=`echo $n|sed 's/[-0-9]//g'`
if [ -n "$n1" ] --表示当变量的值不为空
then
echo "you just only input numbers."
continue
fi
break
done
echo $n
2. 执行while2.sh脚本 :[root@hao-01 ~]# sh -x while2.sh
20.13 break跳出循环
1. break跳出循环 :
[root@hao-01 ~]# vim break.sh # 添加内容 :
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
break
fi
echo $i
done
echo aaaaa
2. 执行break.sh脚本 :
[root@hao-01 ~]# sh -x break.sh
[root@hao-01 ~]# sh break.sh
执行过程:可以看到当i=3就跳出以下循环,直接运aaaaa结束
20.14 continue结束本次循环
1. continue结束本次循环 :
[root@hao-01 ~]# vim continue.sh # 添加内容:
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
continue
fi
echo $i
done
echo aaaaa
2. 执行continue.sh脚本 :[root@hao-01 ~]# sh continue.sh
执行过程:可以看到一个3,就没了。continue仅仅跳出本次循环,继续下一次循环
20.15 exit退出整个脚本
1. exit直接退出整个脚本 :
[root@hao-01 ~]# vim exit.sh添加内容:
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
exit
fi
echo $i
done
echo aaaaa
2. 执行exit.sh脚本 :[root@hao-01 ~]# sh exit.sh
执行过程:当i=3 直接退出脚本