Conditional judgment in Linux Shell corner

Source: Internet
Author: User
Tags arithmetic

1. Condition Judgment:

If use:

if condition;  Then commands; fi

If else uses:

if condition;  Then commands; Else if  Then commands; Else commands; fi

Description

&&| | action; # If condition is false, the action is executed. && is the logic and operator, | | is a logical OR operator. This is a useful technique when writing bash scripts. Now let's look at the conditions and comparison operations. 

2. Arithmetic comparison

conditions are usually placed inside enclosed brackets. Be sure to note that there is a space between [or] and the operand. If you forget this space, the script will get an error. For example: [$var-eq0] or [$var-eq0] To determine the arithmetic condition of a variable or value: [$var-eq0] #当 $var equals0returns True when the [$var-ne0] #当 $var for non-0, return True other important operators as shown below.-GT: Greater than.?-LT: less than.?-GE: Greater than or equal to.?-le: Less than or equal to. You can test with multiple conditions as follows: [$var 1-ne0-A $var 2-GT2] #使用逻辑与-a[$var 1-ne0-O VAR2-GT2] #逻辑或-O

3. File System related test

we can use different criteria flags to test different file system-related properties. [ -f $file _var]: Returns True if the given variable contains a normal file path or filename. [ -x $var]: Returns True if the given variable contains a file that is executable.? [ -d $var]: Returns True if the given variable contains a directory. [ -e $var]: Returns True if the given variable contains a file that exists.? [ -c $var]: Returns True if the given variable contains a path to a character device file.? [ -b $var]: Returns True if the given variable contains a path to a block device file. [ -W$var]: Returns True if the given variable contains a file that is writable.? [ -R $var]: Returns True if the given variable contains a file that is readable. [ -L $var]: Returns True if the given variable contains a symbolic link. Here's how to use it: Fpath="/etc/passwd"if[-e $fpath]; ThenEchoFile exists;ElseEchoDoes not exist;fi

4. String comparison

======> <-n $str 1]]: Returns True if STR1 contains a non-empty string.

Conditional judgment in Linux Shell corner

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.