Shell脚本(二、三)
一、 条件测试操作
1、 条件测试
(1)语法
test 条件表达式 或 [ 条件表达式]
(2)文件测试常见操作选项
-d:测试是否为目录
-e:测试目录或文件是否存在
-f:测试是否为文件
-r:测试当前用户是否有权限读取
-w:测试当前用户是否有权限写入
-x:测试当前用户是否有权限执行
(3)测试目录/media/是否存在,若返回0表示存在,否则表示不存在
[root@centos04 ~]# [ -d /media/ ]
[root@centos04 ~]# echo $?
0
2、 整数值比较
(1)常见操作选项
-eq:等于
-ne:不等于
-gt:大于
-lt:小于
-le:小于或等于
-ge:大于或等于
(2)判断当前已登录的用户数,超过五个时输出“你好”
[root@centos04 ~]# um=`who | wc -l`
[root@centos04 ~]# [ $um -gt 5 ] && echo "你好"
3、 字符串比较
(1)常见操作选项
=:第一个字符串与第二个字符串相同
!=:第一个字符串与第二个字符串不相同
-z:检查字符串是否为空
(2)判断当前系统的语言环境
[root@centos04 ~]# echo $LANG
zh_CN.UTF-8
[root@centos04 ~]# [ $LANG != "en.US" ] && echo "Not en.US"
Not en.US
4、 逻辑测试
(1)常见的逻辑测试选项
&&:逻辑与,表示“而且”,两个条件都成立
||:逻辑或,表示“或者”,其中一个成立
!:逻辑否,表示不,不成立
(2)
判断当前Linux系统的内核版本是否大于3.4
[root@centos04 ~]# uname -r
3.10.0-693.el7.x86_64
[root@centos04 ~]# Mnum=$(uname -r | awk -F. '{print $1}')
[root@centos04 ~]# Snum=$(uname -r | awk -F. '{print $2}')
[root@centos04 ~]# [ $Mnum -ge 3 ] && [ $Snum -gt 4] && echo "符合要求"
符合要求
二、 使用if条件语句
1、 单分支if语句
(1)语法
if 条件测试操作
then
命令序列
fi
(2)判断目录/media/cdrom是否存在,若不存在则新建此目录
[root@centos04 ~]# vi chk.sh
#!/bin/bash
mon="/media/cdrom"
if [ ! -d $mon ]
then
mkdir -p $mon
fi
[root@centos04 ~]# chmod +x chk.sh
[root@centos04~]# ./chk.sh
2、 双分支if语句
(1)语法
if 条件测试操作
then
命令序列1
else
命令序列2
fi
(2)编写ping.sh脚本判断主机是否开启
[root@centos04 ~]# vi ping.sh
#!/bin/bash
ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
if [ $?-eq 0 ]
then
echo "Host $1 is up"
else
echo "Host $1 is down"
fi
[root@centos04 ~]# chmod +x ping.sh
[root@centos04 ~]# ./ping.sh 192.168.100.40
Host 192.168.100.40 is up
[root@centos04 ~]# ./ping.sh 192.168.100.10
Host 192.168.100.10 is down
3、 多分支if语句
(1)语法
if 条件测试操作1
then
命令序列1
elif 条件测试操作2
then
命令序列2
else
命令序列3
fi
(2)
编写脚本grand.sh,判断考试分数
[root@centos04 ~]# vi grand.sh
#!/bin/bash
read -p "请输入您的分数(0~100):" gran
if [ $gran -ge 85 ] && [ $gran -le 100 ]
then
echo "$gran 分,优秀!"
elif [ $gran -ge 70 ] && [ $gran -le 84 ]
then
echo "$gran 分,合格!"
else
echo "$gran 分,不合格!"
fi
[root@centos04 ~]# ./grand.sh
请输入您的分数(0~100):99
99 分,优秀!
[root@centos04 ~]# ./grand.sh
请输入您的分数(0~100):77
77 分,合格!
[root@centos04 ~]# ./grand.sh
请输入您的分数(0~100):66
66 分,不合格!
三、实验案例
1、 编写sysmon.sh脚本
[root@centos04 ~]# vi sysmon.sh
#!/bin/bash
DUG=$(df -Th | grep '/$' | awk '{print $6}' | sed 's/%//') //查看磁盘占用率
CUG=$(mpstat | grep all | awk '{print $11}' | awk -F. '{print $1}') //查看空闲CPU
used=$(free | grep 'Mem' | awk '{print $3}') //查看空闲内存使用率
total=$(free | grep 'Mem' | awk '{print $2}')
MUG=$(expr $used / $total)
#d=$(expr 100 - $CUG)
if [ $DUG -ge 90 ]
then
echo "当前磁盘占用超过80%"
fi
if [ $d -ge 80 ]
then
echo "当前CPU使用率已达到80%"
fi
if [ $MUG -ge 90 ]
then
echo "当前内存占用率已达到90%"
fi
2、 添加执行权限测试
[root@centos04 ~]# chmod +x sysmon.sh
四、使用for循环语句
(1)for语句的语法
for 变量名 in 取值列表
do
命令序列
done
(2)编写脚本uaddfor.sh,从users.txt文件中读取各用户名称,设置初始密码
[root@centos04 ~]# vi users.txt
zhangsan
lisi
lihua
zhangjie
[root@centos04 ~]# vi uaddfor.sh
#!/bin/bash
ul=$(cat users.txt)
for uname in $ul
do
useradd $uname
echo "pwd123" | passwd --stdin $uname &>/dev/null
done
[root@centos04 ~]# chmod +x uaddfor.sh
[root@centos04 ~]# ./uaddfor.sh
[root@centos04 ~]# tail -6 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
test:x:1000:1000:test:/home/test:/bin/bash
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
lihua:x:1003:1003::/home/lihua:/bin/bash
zhangjie:x:1004:1004::/home/zhangjie:/bin/bash
(3)循环删除用户
[root@centos04 ~]# vi udelfor.sh
#!/bin/bash
ul=$(cat users.txt)
for uname in $ul
do
userdel -r $uname &>/dev/null
done
[root@centos04 ~]# chmod +x udelfor.sh
[root@centos04~]# ./udelfor.sh
[root@centos04~]# tail -6 /etc/passwd
[root@centos04 ~]# id zhangsan
id:zhangsan: no such user
五、 使用while循环语句
1、 while循环语句语法
while 条件测试操作
do
命令序列
Done
2、 添加25个用户,名称依次为stu1,stu2,…stu25
[root@centos04 ~]# vi uaddwhile.sh
#!/bin/bash
pr="stu"
i=1
while [ $i -le 25 ]
do
useradd ${pr}$i
echo "pwd1234" | passwd --stdin ${pr}$i &>/dev/null
let i++
done
[root@centos04 ~]# chmod +x uaddwhile.sh
[root@centos04~]# ./uaddwhile.sh
[root@centos04 ~]# grep “stu” /etc/passwd | tail -5
stu21:x:1021:1021::/home/stu21:/bin/bash
stu22:x:1022:1022::/home/stu22:/bin/bash
stu23:x:1023:1023::/home/stu23:/bin/bash
stu24:x:1024:1024::/home/stu24:/bin/bash
stu25:x:1025:1025::/home/stu25:/bin/bash
3、删除uaddwhile.sh脚本添加的用户
[root@centos04 ~]# vi udelwhile.sh
#!/bin/bash
pr="stu"
i=1
while [ $i -le 25 ]
do
userdel -r ${pr}$i
let i++
done
[root@centos04~]# chmod +x udelwhile.sh
[root@centos04~]# ./udelwhile.sh
[root@centos04~]# id stu25
id:stu25: no such user
六、 使用case分支语句
1、 case语句的语法
case 变量名 in
模式 1)
命令序列1
;;
模式 2)
命令序列2
;;
……
*)
默认序列
esac
2、 编写hit.sh脚本,提示用户从键盘输入一个字符,通过case语句判断该字符是否为字母,数字或其他控制字符
[root@centos04 ~]# vi hit.sh
#!/bin/bash
read -p "请输入一个字符,并按Enter键确认:" key
case"$key" in
[a-z] | [A-Z])
echo "您输入的是字母"
;;
[0-9])
echo "您输入的是数字"
;;
*)
echo "您输入的是空格、功能键或者其他控制字符"
esac
[root@centos04~]# chmod +x hit.sh
[root@centos04~]# ./hit.sh
请输入一个字符,并按Enter键确认:5
您输入的是数字
[root@centos04~]# ./hit.sh
请输入一个字符,并按Enter键确认:r
您输入的是字母
[root@centos04~]# ./hit.sh
请输入一个字符,并按Enter键确认:W
您输入的是字母
[root@centos04~]# ./hit.sh
请输入一个字符,并按Enter键确认:
您输入的是空格、功能键或者其他控制字符
3、 编写系统服务脚本
[root@centos04 ~]# vi myprog
#/bin/bash
case
"$1" in
start)
echo -n "正在启动sleep服务"
if sleep 7200 &
then
echo "OK"
fi
;;
stop)
echo -n "正在停止sleep服务"
pkill "sleep" &> /dev/null
echo "OK"
;;
status)
if pgrep "sleep" &>/dev/null
then
echo "sleep服务已经启动"
else
echo
"sleep服务已经停止"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "请使用:$0 {start|stop|status|restart}"
esac
[root@centos04~]# chmod +x myprog
[root@centos04~]# ./myprog start
正在启动sleep服务OK
[root@centos04~]# ./myprog status
sleep服务已经启动
[root@centos04~]# ./myprog stop
正在停止sleep服务OK
[root@centos04~]# ./myprog restart
正在停止sleep服务OK
正在启动sleep服务OK
[root@centos04~]# ./myprog rest
请使用:./myprog {start|stop|status|restart}
七、 实验案例
1、 编写getarp.sh脚本
[root@centos04 ~]# vi getarp.sh
#!/bin/bash
[ -s /etc/ether ] && cp -p /etc/ether /etc/ether
for ip in 192.168.8.{225..255}
do
arping -c 2 -w 1 -q $ip
done
arp -n | awk '/^192/ {print $1,$3}' | grep -v ens32 >>/etc/ether
2、 执行脚本
[root@centos04 ~]# ./getarp.sh
[root@centos04~]# cat /etc/ether
192.168.100.10 00:0c:29:8c:51:73
192.168.100.253 00:50:56:c0:00:01
3、编写scanhost.sh
[root@centos04~]# vi scanhost.sh
#!/bin/bash
for ip in $(awk '{print $1}' /etc/ether)
do
wget -T 1 -t 1 -q ftp://Sip &> /dev/null
if [ $? -eq 0 ]
then
echo "$ip已打开匿名访问"
else
echo "$ip未打开匿名访问"
fi
done
4、 执行scanhost.sh脚本
[root@centos04~]# ./scanhost.sh
192.168.100.10未打开匿名访问
192.168.100.253未打开匿名访问