Shell Process Control-IF statement

Source: Internet
Author: User

  1. Single-branch If condition statement

    If [conditional judgment]; Then
    Program
    Fi

    Example: Judging partition usage

    #!/bin/bash# Author:huangrui (email:[email protected]) rate=$(DF-H |grep "sda1"|awk '{print $}'|Cut-F1-D"%") if[$rate-GT the]; Then   Echo "warning! disk is full!!!"fi
    Code
  2. Dual-Branch IF condition statement
    If [conditional judgment]; Then
    When a condition is executed, the statement executed
    Else
    Conditional execution is not valid, the executed statement
    Fi

    Example: Backing up a site directory file

    #!/bin/bash# Author Huangrui (email:[email protected])Date=$(Date+%y%m%d) Size=$(du-SH/home/test.com)if[-d/home/test.com]; Then    Echo "date is: $date">/tmp/dbback/Db.txtEcho "size is: $size">>/tmp/dbback/db.txt CD/tmp/DbbackTar-ZCF web_$Date.Tar. gz/home/test.com db.txt&>/dev/NULL    RM-rf/tmp/dbback/Db.txtElse    mkdir/tmp/DbbackEcho "date is: $date">/tmp/dbback/Db.txtEcho "size is: $size">>/tmp/dbback/db.txt CD/tmp/DbbackTar-ZCF web_$Date.Tar. gz/home/test.com db.txt&>/dev/NULL    RM-rf/tmp/dbback/Db.txtfi
    Code

    To determine if Apache is starting, use Nmap command

    #!/bin/bash# Author Huangrui (email:[email protected]) port=$ (nmap-st172.16.193.128|grepTCP |grepHTTP |awk '{print $}') if["$port"=="Open"]; Then   Echo "$ (date) httpd is ok!">>/tmp/autostart-Acc.logElse/ETC/RC.D/INIT.D/HTTPD Restart &>/dev/NULL   Echo "$ (date) httpd is restart!!">>/tmp/autostart-Err.logfi
    Code
  3. Multi-branch IF condition statement
    If [conditional judgment 1]; Then
    When the conditional judgment 1 is established, the execution of the program 1
    elif [Conditional judgment type 2]; Then
    When the conditional Judgment 2 is established, the execution of the program 2
    ... Omit more conditions ...
    Else
    When all conditions are not set, the final execution of this program
    Fi

Shell Process Control-IF statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.