正则表达式------流程控制

单分支if语句
注意:要用vim编辑器颜色鲜明更容易排错
在这里插入图片描述

基本语句:

在这里插入图片描述

Ø 例:判断分区使用率

	○ [root@localhost ~]# df -h
	Filesystem               Size  Used Avail Use% Mounted on
	/dev/mapper/centos-root   36G  5.6G   30G  16% /
	devtmpfs                 3.9G     0  3.9G   0% /dev
	tmpfs                    3.9G     0  3.9G   0% /dev/shm
	tmpfs                    3.9G  8.7M  3.9G   1% /run
	tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
	/dev/sdb                  50G   15G   33G  32% /kvm_data
	/dev/sda1                497M  126M  372M  26% /boot
	tmpfs                    783M     0  783M   0% /run/user/0
	○ [root@localhost ~]# df -h |grep /dev/mapper/centos-root
	/dev/mapper/centos-root   36G  5.6G   30G  16% /
	○ [root@localhost ~]# df -h |grep /dev/mapper/centos-root | awk '{print $5}
	16%
	○ [root@localhost ~]# df -h |grep /dev/mapper/centos-root |awk '{print $5}' |cut -d "%" -f 1
	16
	○ [root@localhost ~]# cd /bin
[root@localhost bin]# vim disk1.sh
	
		#!/bin/bash
		#统计根分区使用率
		rate=$(df -h |grep /dev/mapper/centos-root |awk '{print $5}' |cut -d "%" -f 1
		)
		#把根分区使用率作为变量赋予变量rate
		if [ $rate -ge 3 ]
		   then
		        echo "waring! / rate>=$rate"
		fi
	○ [root@localhost bin]# bash disk1.sh 
	waring! / rate>=16

双分支if条件语句

Ø 语法:
在这里插入图片描述

Ø  例:备份

[root@localhost bin]# yum install -y ntpdate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.bfsu.edu.cn
 * extras: mirrors.bfsu.edu.cn
 * updates: mirrors.bfsu.edu.cn
Resolving Dependencies

[root@localhost bin]# ntpdate asia.pool.ntp.org
16 Apr 10:26:25 ntpdate[15272]: step time server 133.243.238.163 offset 2816821.868925 sec
[root@localhost bin]# ntpdate asia.pool.ntp.org &>/dev/null
[root@localhost bin]# cat db.sh
	#!/bin/bash
	ntpdate asia.pool.ntp.org &>/dev/null
	#同步系统时间
	date=$(date +%y%m%d)
	#把当前时间按照“年月日”格式赋予date
	size=$(du -sh /etc)
	#统计etc文件的大小,并把大小赋予size变量
	if [ -d /tmp/dbbak ]
	   then
	        echo "Date : $date!" > /tmp/dbbak/dbinfo.txt
	        echo "Date : size : $size!" >> /tmp/dbbak/dbinfo.txt
	        cd /tmp/dbbak
	        tar -zcf etc$date.tar.gz /etc dbinfo.txt &>/dev/null
	        rm -rf /tmp/dbbak/dbinfo.txt
	   else
	        mkdir /tmp/dbbak
	        echo "Date : $date!" > /tmp/dbbak/dbinfo.txt
	        echo "Date : size : $size!" >> /tmp/dbbak/dbinfo.txt
	        cd /tmp/dbbak
	        tar -zcf etc$date.tar.gz /etc dbinfo.txt &>/dev/null
	        rm -rf /tmp/dbbak/dbinfo.txt
	Fi
[root@localhost bin]# vim db.sh

在这里插入图片描述

Ø 例题:判断Apache服务是否启动
[root@localhost bin]# cat au.sh
#!/bin/bash
port=$(nmap -sT 10.30.59.217 | grep tcp | grep http | awk '{print $2}')
#使用nmap命令扫描服务器,并截取apache服务状态,赋予变量port
if [ "$port" == "open" ]
   then
       echo "$(date) httpd is ok!"  >>/tmp/autostart-acc.log
   else
       # 启动apache
       /etc/rc.d/init.d/httpd  start &> /dev/null
       echo "$(date) restart httpd !!" >> /tmp/autostart-err.log
fi
[root@localhost bin]# bash au.sh

多分支if条件语句
Ø 语法:
在这里插入图片描述

例题:判断用户输入什么样的文件
	[root@localhost bin]# vim js.sh
	
	#!/bin/bash
	#判断用户输入的是什么文件
	read -p "please input a filename:" file
	#接受键盘的输入,并赋予变量file
	if [ -z "$file" ]
	#判断file变量是否为空
	   then
	       echo "Error,please input a filename"
	       exit 1
	elif [ ! -e "$file" ]
	#判断file的值是否存在
	    then
	       echo "Your input is not a file!"
	       exit 2
	elif [ ! -e "$file" ]
	#判断file的值是否为目录文件
	     then
	         echo "$file is a directory!"
	else
	         echo "$file is an other file!"
	fi
	
	
	[root@localhost ~]# bash js.sh
	please input a filename:au.sh
	Your input is not a file!
	[root@localhost ~]# bash js.sh
	please input a filename:/etc
	/etc is an other file!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值