• Linux 条件判断


    01、按照文件类型进行判断   =>

        

        常用-d、-e、-f

        两种格式:

          test -e /root/install.log  =>   常用

          [ -e /root/install.log ]  =>   两边空格必须有

          例如:[ -d /root ] && echo "yes" || echo "no":判断该文件是否存在,并且是否为目录文件,是yes,不是no。

    02、按照文件权限进行判断   =>

       

     03、两个文件之间比较

          

    04、两个数之间的比较  =>

        

     05、单分支 if 条件语句

        if [ 条件判断式 ];then   =>  条件判断两边空格不能省略

          程序

        fi

        或者

        if [ 条件判断式 ]

          then

            程序

        fi

          例子:判断分区使用率

          #!/bin/bash 

          #统计根分区使用率

          # Author :shencao(E-mail:15258090@qq.com)       

          rect=$(df -h | grep /dev/sda5 | awk '{print $5}' | cut -d "%" -f1)
          if [ $rect -ge 10 ]
            then
              echo "warning! /dev/sda5 is full!!"
          fi

     06、多分支if语句

         

    if [ 条件判断式 ];then   =>  条件判断两边空格不能省略

          程序

        fi

        或者

        if [ 条件判断式 ]

          then

            条件成立程序

          else

            条件不成立程序

        fi

         例子:备份 mysql 数据库

          #!/bin/bash 

          #备份 mysql 数据库

          # Author :shencao(E-mail:15258090@qq.com)

          date=$(date +%y%m%d)
          size=$(du -sh /etc)

          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

         例子2:判断apache是否启动

          #!/bin/bash      

          port=$(nmap -sT 192.168.3.207 | grep tcp | grep http | awk '{print $2}')

          #使用nmap命令扫描服务器,并截取 apache 服务器的状态,赋予变量 port

          if [ "$port" == "open" ]
            then
              echo "$(date) http is ok" >> /tmp/httpd_acc.log
            else
              /etc/rc.d/init.d/httpd restart &>/dev/null
              echo "$(date) http is reboot!!" >> /tmp/httpd_err.log

          fi

     07、多分支if条件语句

        if [ 条件语句1 ]

           then

            执行程序1

        elif [ 条件语句2 ] 

          then

            执行程序2

         else

          执行程序2 

         fi

     08、case语句

         

        例子:

          

        

  • 相关阅读:
    idea修改代码没法实时编译终极解决方案
    linux 安装 hadoop
    linux克隆虚拟机后需要修改的点
    多线程第一篇
    windows搭建ftp环境
    第8章PostGIS参考
    postgis 简单应用
    linux 安装postgresql
    复杂度分析(下)
    复杂度分析(上)
  • 原文地址:https://www.cnblogs.com/patriot/p/7905277.html
Copyright © 2020-2023  润新知