If Judgment statement for Shell script

Source: Internet
Author: User
Tags case statement ticket

If statement

1, according to the file type to judge
-d file) to determine if the file exists and is a character device file (the character device is true)
-e file) to determine if the file exists (exists as true)
[-e/root]
echo $? The output is 0 #判断为真, which is 0
The commonly used formats are:
[-e/root] && echo yes | | Echo No

One, the single branch if conditional statement format has the following two kinds:

If [Conditional judgment type];then
Program
Fi
Or
If [conditional judgment]
Then
Program
Fi

But branching conditional statements need to be aware of several points
The if statement uses the FI end, which differs from the general language using curly braces
· [Conditional judgment] is determined using the test command, so there must be a space between the brackets and the conditional judgment
then after the following and meet the conditions after the execution of the program, can be placed after [], with ";" Segmentation. You can also write a newline, and you don't need to be ";" The

Example: Judging partition usage
#!/bin/bash
#统计根分区使用率
#Author: Xuyizhong (e-mail:[email protected])
rate=$ (df-h |grep "/dev/vda1" | awk ' {print $} ' | cut-d "%"-f1)
#把根分区使用率作为变量值赋予变量rate
If [$rate-ge 80]
Then
echo "warning! /DEV/VDA1 is FULL!!! "
Fi

Two, two-branch if condition statement

If [conditional judgment]
Then
When conditions are established, the procedure to be executed
Else
Another program that executes when the condition is not established
Fi
Example 1: Backing up the MySQL database
#!/bin/bash
#备份mysql数据库
#Author: Xuyizhong (e-mail:[email protected])
Ntpdate asia.pool.ntp.org &>/dev/null
#同步系统时间
date=$ (Date +%y%m%d)
#把当前系统时间按照 "Month and Day" format assigns variable date
size=$ (Du-sh/var/lib/mysql)
#统计mysql数据库的大小, and give the size variable
if [-d/tmp/dbbak]
#判断 the/tmp/dbbak folder exists, there is execution then, there is no execute else
Then
echo "Date: $date" >/tmp/dbbak/dbinfo.txt
echo "Data size: $size" >>/tmp/dbbak/dbinfo.txt
Cd/tmp/dbbak
TAR-ZCF mysql-lib-$date. Tar.gz/var/lib/mysql dbinfo.txt &>/dev/null
Rm-rf/tmp/dbbak/dbinfo.txt
Else
Mkdir/tmp/dbbak
echo "Date: $date" >/tmp/dbbak/dbinfo.txt
echo "Data size: $size" >>/tmp/dbbak/dbinfo.txt
Cd/tmp/dbbak
TAR-ZCF mysql-lib-$date. Tar.gz/var/lib/mysql dbinfo.txt &>/dev/null
Rm-rf/tmp/dbbak/dbinfo.txt
Fi

Example 2: Determine if Apache is booting
#!/bin/bash
#Author: Xuyizhong (e-mail:[email protected])

port=$ (nmap-st 192.168.1.156 | grep tcp |grep HTTP |awk ' print $ ')
#使用nmap命令扫描服务器, and intercepts the status of the Apache service, assigning the variable port
If ["$port" = = "Open"]
Then
echo "$ (date) httpd is ok!" >>/tmp/autostart-acc.log
Else
/ETC/RC.D/INIT.D/HTTPD Start &>/dev/null
echo "$ (date) Restart httpd!" >>/tmp/autostart-err.log
Fi

#自己将apache停止 to see if Apache will automatically restart!

Three, multi-branch if condition branch

If [conditional judgment 1]
Then
When the conditional judgment 1 is established, the execution of the program 1
elif [conditional judgment 2]
Then
When the conditional Judgment 2 is established, the execution of the program 2
... Omit more conditions ...
Else
When all conditions are not true, the final execution of this program
Fi

Example 1: Judging what the user has entered the command
#!/bin/bash
#Author: Xuyizhong (e-mail:[email protected])

Read-p "Please input a filename:" File
#接收键盘的输入, and assign the variable 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 a other file!"
Fi

Case statement

The case statement is the same as the If...elif...else statement, but unlike the if multi-branch statement, the case statement can only judge a conditional relationship, and the IF statement may judge multiple conditional relationships

Case $ variable name in
"Value 1")
If the value of the variable equals the value 1, execute the program 1
;;
"Value 2")
If the value of the variable equals the value 2, execute the program 2
;;

If the value of the variable is the value above, execute the program
;;
Esac
Example 1:
#!/bin/bash
#判断用户输入
#Author: Xuyizhong (e-mail:[email protected])
Read-p "Please choose Yes/no" T-cho
#将用户的值赋予给cho
Case $cho in
"Yes")
echo "Your Choose is yes!"
;;
"No")
echo "Your Choose is no!"
;;
)
echo "Your Choose is error!"
;;
Esac
Example 2:
#!/bin/bash
Echo ' Want to shanghai,please input ' 1 '
Echo ' Want to guangzhou,please input ' 2 '
Echo ' Want to chengdu,please input ' 3 '
Read-t 30-p "Please input your choose"-T-cho

Case ' $cho ' in
"1")
echo "Shanghai's ticket is on sale."
;;
"2")
echo "Guangzhou's ticket is on sale."
;;
"1")
echo "Chengdu's ticket is on sale."
;;
*)
echo "Error 1/2/3"
;;
Esac

For loop while loop

If Judgment statement for Shell script

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.