Description of the If else and greater than, less than, equal to logical expressions in the Linux shell

Source: Internet
Author: User
Tags readable

For example, comparing strings, judging whether the file exists and whether it is readable, etc., usually uses "[]" to represent the condition test.

Note: The space here is important. The space to ensure the square brackets. I have wasted a lot of precious time because the space is missing or the position is wrong.

If ....; Then
....
Elif ...; Then
....
Else
....
Fi
[-F "somefile"]: Determine if it is a file
[-X "/bin/ls"]: Determine if/bin/ls exists and has executable permissions
[-N ' $var]: Determine if the $var variable has a value
["$a" = "$b"]: Determine if $ A and $b are equal
-r file user readable as True
-W file user can write as true
-X file user can execute as true
-F file is true for normal files
-e file exists as true
-d file files are directory-True
-C File file is true for character special files
-B file files are true for block special files
-S file files non-0 o'clock True
-T file is true when the specified device is terminal (default = 1)


-Z file to determine whether the variable value is empty
If [!-s filename]
Ps:-s is judged non-empty! -S is judged to be empty

Shell scripts with conditional selection are generally competent for simple shell scripts that do not contain variables. However, when you perform some decision-making tasks, you need to include the if/then criteria to judge. Shell scripting supports such operations, including comparison operations, determining whether a file exists, and so on.
The basic if Condition command options are:-eq-compare two parameters for equality (for example, if [2–eq 5])
-ne-comparison of two parameters is not equal
-lt-parameter 1 is less than parameter 2
-le-parameter 1 is less than or equal to parameter 2
-gt-parameter 1 is greater than parameter 2
-ge-parameter 1 is greater than or equal to parameter 2
-D Check if directory exists
Almost all judgments can be implemented with these comparison operators. The common-f command option in a script checks to see if it exists before executing a file.

Let's take two examples to make it easier for everyone to understand.

1. Determine if the file exists


Copy the code code as follows:
#!/bin/sh
# Determine if the 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" [email protected]
Fi


Copy the code code as follows:
#!/bin/sh
# Clear related files and log 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


The logical relationship between commands
Logic and:&&
When the first condition is false, the second condition is no longer judged and the final result is already there;
When the first condition is true, the second condition must be judged;
Logical OR: | |
When the first condition is false, the second condition must be judged;
When the first condition is true, the second condition is no longer judged and the final result is already

Description of the If else and greater than, less than, equal to logical expressions in the Linux shell

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.