Linux conditional judgment-if statement-case statement-for statement

Source: Internet
Author: User
Tags case statement file permissions

01. Judging by File type =

    

Common-D,-E,-F

Two types of formats:

TEST-E/root/install.log = Common

[-e/root/install.log] = both spaces must have

Example: [-d/root] && echo "Yes" | | echo "No": Determine if the file exists and whether it is a directory file, yes, not No.

02, according to the file permissions to judge

   

Comparison between 03, two files

Comparison between 04 and two numbers + =

    

05. Single Branch If condition statement

If [conditional judgment];then = = Condition judgment Both spaces cannot be omitted

Program

Fi

Or

If [conditional judgment]

Then

Program

Fi

Example: Judging partition usage

#!/bin/bash

#统计根分区使用率

# Author:shencao (E-mail:[email protected])

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

06. Multi-Branch If statement

If [conditional judgment];then = = Condition judgment Both spaces cannot be omitted

Program

Fi

Or

If [conditional judgment]

Then

Procedure for establishing conditions

Else

Conditions do not form a program

Fi

Example: Backing up a MySQL database

#!/bin/bash

#备份 MySQL Database

# Author:shencao (E-mail:[email protected])

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

Example 2: Determine if Apache is booting

#!/bin/bash

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

#使用nmap命令扫描服务器, and intercepts the status of the Apache server, assigning the variable 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. Multi-Branch if condition statement

If [conditional statement 1]

Then

Executing the program 1

elif [Conditional Statement 2]

Then

Executing the program 2

Else

Executing the program 2

Fi

08. Case Statement

Example:

      

    

Linux conditional judgment-if statement-case statement-for statement

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.