Introduction to if else and logical expressions greater than, less than, or equal to in linux shell

Source: Internet
Author: User

For example, to compare strings, determine whether a file exists, and whether the file is readable, "[]" is usually used to represent a conditional test.

Note: spaces are important. Make sure that the square brackets have spaces. I used to waste a lot of valuable time due to lack of spaces or incorrect positions.

If...; then
....
Elif...; then
....
Else
....
Fi
[-F "somefile"]: determines whether it is a file.
[-X "/bin/ls"]: determines whether/bin/ls exists and has the executable permission.
[-N "$ var"]: determines whether the $ var variable has a value.
["$ A" = "$ B"]: determines whether $ a and $ B are equal.
-R file user readable to true
-W file users can write to true
-X file user executable is true
-F file: the regular file is true.
-D file: the directory is true.
-C file: the special character file is true.
-B file: The Block special file is true.
-S file is true if the file size is not 0
-T file: this parameter is true when the specified device is a terminal in the file descriptor (1 by default ).

Shell scripts with conditional selection are generally competent for simple shell scripts for tasks without variables. However, when executing some decision-making tasks, it is necessary to include the if/then condition judgment. Shell script programming supports such operations, including comparison operations and determining whether a file exists.
The basic if condition Command Options include-eq-to compare whether two parameters are equal (for example, if [2-eq 5]).
-Ne-compare whether two parameters are not equal
-Lt-parameter 1: whether it is smaller than parameter 2
-Le-parameter 1: whether it is less than or equal to parameter 2
-Gt-whether parameter 1 is greater than parameter 2
-Whether ge-parameter 1 is greater than or equal to parameter 2
-F-check whether a file exists (for example, if [-f "filename"])
-D-check whether the directory exists
Almost all judgments can be implemented using these comparison operators. In the script, the common-f Command Option checks whether a file exists before executing it.

Here are two examples for your understanding.

1. Determine whether a file exists

Copy codeThe Code is as follows :#! /Bin/sh
# Determining whether a file exists
# Link: www.jb51.net
# Date: 2013/2/27

YACCESS = 'date-d yesterday + % Y % m % d'
FILE = "access _ $ YACCESS. log. tgz"
Cd/data/nginx/logs
If [-f "$ FILE"]; then
Echo "OK"
Else
Echo "error $ FILE"> error. log
Mail-s "$ FILE backup fail" test123@jb51.net <error. log
Fi

Copy codeThe Code is as follows :#! /Bin/sh
# Clear related files and record logs by time period
# Link: www.jb51.net
# Date: 2013/2/27
#
DIR =/data/img_cache
DAY = 'date + "% Y-% m-% d % H: % M "'
NUM = 'ls $ DIR | wc-l'
DIRNAME = 'ls $ DIR | grep leveldb | head-n 1 | awk '{print $ NF }''
If [[$ NUM-gt 3]; then
Rm-rf $ DIR/$ DIRNAME
Echo "--------- $ DAY ---- ($ DIR) ---------------------">/tmp/img_cache.log
Echo "$ DIRNAME Deleted successful">/tmp/img_cache.log
Fi
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.