Shell编程之流程控制语句——if语句、case语句

基本流程控制方法:

  • if语句
  • case语句
  • for循环
  • while循环

一、if语句

1、单分支if条件语句

if [ 条件判断式 ] ; then
程序
fi
或者
if [ 条件判断式 ]
then
程序
fi

单分支条件语句需要注意几个点:

  • if语句使用fi结尾,和一般语言使用大括号结尾不同;
  • [ 条件判断式 ]就是使用test命令判断,所以中括号和条件判断式之间必须有空格;
  • then后面跟符合条件之后执行的程序,可以放在[ ]之后,用“ ; ” 分割。也可以换行写入,就不需要 “ ; ” 了

示例:判断分区使用率

[root@root 桌面]# cd ~
[root@root ~]# mkdir sh
[root@root ~]# cd sh/
[root@root sh]# vim if1.sh

#具体文本如下:
#!/bin/bash
#这句话标称,我写的一下语句是Linux的shell脚本,这句话在Linux的shell脚本中是不可以省略的,必须写。
#若我们写的所有内容都是shell语言,这句话可以省略,但是如果是比较复杂的shell脚本(即里面嵌套其他语言时,这个必须写,否则会出错!)
#统计根分区使用率
# Author:shenchao

rate=$(df -h | grep "/dev/sda2" | awk '{printf $5}' | cut -d "%" -f1)
#把根分区使用率作为变量值赋予变量rate。其中/dev/sda2分区文件与自己的匹配
if [ $rate -ge 5 ]
        then
                echo "warning /dev/sda3 is full!!!" 
fi

#执行如下:
[root@root sh]# chmod 755 if1.sh
[root@root sh]# ./if1.sh
warning /dev/sda3 is full!!!
2、双分支if条件语句

if [ 条件判断式 ]
then
条件成立时,执行的程序
else
条件不成立时,执行的另一个程序
fi

  • 示例1:备份mysql数据库
#!/bin/bash
#备份mysql数据库
date=$(date +%y%m%d)
#把当前系统时间按照“年月日”格式赋予变量date
size=$(du -sh /etc)
#统计mysql数据库的大小,并把大小赋予size变量

if [ -d /tmp/dbback ]
        then
                echo "Date is : $date"  > /tmp/dbback/db.txt
                echo "size is : $size" >> /tmp/dbback/db.txt
                cd /tmp/dbback
                tar -zcf etc_$date.tar.gz /etc db.txt &>/dev/null
                rm -rf /tmp/dbback/db.txt
        else
                mkdir /tmp/dbback
                echo "Date is : $date"  > /tmp/dbback/db.txt
                echo "size is : $size" >> /tmp/dbback/db.txt
                cd /tmp/dbback
                tar -zcf etc_$date.tar.gz /etc db.txt &>/dev/null
                rm -rf /tmp/dbback/db.txt
fi

#执行这个脚本:
[root@root sh]# chmod 755 if2.sh
[root@root sh]# ./if2.sh
#第一执行if语句,第二次执行else语句
[root@root sh]# cd /tmp/
[root@root tmp]# ls
_cafenv-appconfig_  keyring-ll26zc  pulse-h68wANV588jC  virtual-root.ixLqJB
dbback              orbit-gdm       pulse-HYwFHd4subTQ  vmware-root
keyring-CfdIfM      orbit-root      stu.txt             vmware-root_1588-868458577
[root@root tmp]# cd dbback/
[root@root dbback]# ls
etc_190716.tar.gz
[root@root dbback]# tar -ztvf etc_190716.tar.gz
#查看一下这个目录下的内容
  • 示例2:判断apache是否启动

这个脚本我们只需要让系统没15 分钟执行一次,这样我们第二天只要看错误日志,就可以知道,在我们这一天的时间里面apache死机了多少次。这就是一个自动重启apache的脚本。

[root@root sh]# vi if3.sh

#!/bin/bash
#Author:shenchao

port=$(nmap -sT 192.168.1.156 | grep tcp | grep http | awk '{print $2}')
#使用nmap命令扫描服务器,并截取Apache服务状态,赋予变量port
if [ "$port"=="open" ]
#比较port的值是否为“open”,比较两个字符串是否相等用的是“==”号
	then
		echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log
		#如果相等,说明apache服务启动,不做任何事情,只需要记录一条当前时间系统正常到日志
	else
		/etc/rc.d/init.d/httpd start &>/dev/null
		#如果不相等,启动apache,并将输出结果丢到垃圾箱
		echo "$(date) restart httpd!!" >> /tmp/autostart-err.log
		#同时记录,我们在这个时间重启apache,并将其记录在错误日志
	fi


#运行程序
[root@root sh]# chmod 755 if3.sh
[root@root sh]# ./if3.sh
[root@root sh]# ./if3.sh
[root@root sh]# cd /tmp/
[root@root tmp]# ls
... ...
[root@root sh]# cat httpd_err.log
[root@root sh]# netstat -tlun 
3、多分支if条件语句

if [ 条件判断式1 ]
then
当条件判断式1成立时,执行程序1
elif [ 条件判断式2 ]
then
当条件判断式2成立时,执行程序2
… 省略更多条件 …
else
当所有条件都不成立时,最后执行此程序
fi

示例如下:

#!/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 [ -f "$file" ]
#判断file的值是否为普通文件
        then
                echo "$file is a regulare file!"
elif [ -d "$file" ]
#判断file的值是否为目录文件
        then
                echo "$file is a directory!"
else
        echo "$file is an other file!"
fi

二、case语句

  • case语句和if…elif…else语句一样都是多分支条件语句,不过和if多分支条件语句不同的是,case语句只能判断一种条件关系,而if语句可以判断多种条件关系。
  • case语句更适合于选择列表语句;

case $变量名 in
“值1”)
如果变量的值等于值1,则执行程序1
;;
“值2”)
如果变量的值等于值2,则执行程序2
;;
…省略其他分支…
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac

其中,;;两个分号代表程序段的结束,这两个分号不能省略。

  • 示例说明1:输入一个选项,请选择yes或no
[root@root sh]# vi case.sh

#!/bin/bash

read -p "Please choose yes/no:" -t 30 cho
# -t 30 ——> 等待30 秒
case $cho in
        "yes")
                echo "Your choose is yes!"
                ;;
        "no")
                echo "Your choose is no!"
                ;;
        *)
                echo "Your choose is no!"
                ;;
esac


[root@root sh]# chmod 755 case.sh
[root@root sh]# ./case.sh
Please choose yes/no:yes
Your choose is yes!
[root@root sh]# ./case.sh
Please choose yes/no:no
Your choose is no!
  • 示例说明2:出行地选择
[root@root sh]# vi case1.sh

!/bin/bash

echo 'you want to shanghai! Please input "1"'
echo 'you want to guangzhou! Please input "2"'
echo 'you want to chengdu! Please input "3"'

read -t 30 -p "Please input your choose:" cho

case "$cho" in
        "1")
                echo "shanghai!!!!"
                ;;
        "2")
                echo "guangzhou!!!!"
                ;;
        "3")
                echo "chengdu!!!!"
                ;;
        "*")
                echo "shanghai!!!!"
                ;;
esac

#运行结果如下:
[root@root sh]# chmod 755 case1.sh
[root@root sh]# ./case1.sh
[root@root sh]# ./case1.sh
you want to shanghai! Please input "1"
you want to guangzhou! Please input "2"
you want to chengdu! Please input "3"
Please input your choose:1
shanghai!!!!
[root@root sh]# ./case1.sh
you want to shanghai! Please input "1"
you want to guangzhou! Please input "2"
you want to chengdu! Please input "3"
Please input your choose:2
guangzhou!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值