Linux comparison operators

Source: Internet
Author: User
Tags readable syslog

use the If statement to determine parameters in Linux shell programming
–b when file exists and is a block file returns true-D when file exists and is a character file returns True-e when pathname exists and is a directory returns true-F when a file or directory pathname specified exists and returns True when file exists and is regular -G when the file or directory specified by pathname is present and the Sgid bit is set to return True-H returns True when file exists and is a symbolic link file, this option is invalid on some old systems-K when the file or directory specified by pathname is present and the "sticky" bit is set to return True-p Return true-r when file exists and is a command pipeline when the files or directories specified by pathname are present and are readable, return true-if file exists with a size greater than 0 o'clock returns true-u when the file or directory specified by pathname is present and the SUID bit is set to return True-W Pathname the specified file or directory exists and returns True when it is executable.  A directory must be executable for its content to be accessed. -O Returns True when the file or directory specified by pathname is present and is owned by the user specified by the active user ID of the current process.

A more detailed description:
---------------------------------------------------------------------------------------------
Operator Description Example

File comparison Operators

-e filename true if filename exists [-e/var/log/syslog]

-D filename True if filename is a directory [-d/tmp/mydir]

-F filename True if filename is a regular file [-f/usr/bin/grep]

-L filename True if filename is a symbolic link [-l/usr/bin/grep]

-R filename True if filename is readable [-r/var/log/syslog]

-W filename if filename is writable, true [-w/var/mytmp.txt]

-X filename is true if filename is executable [-l/usr/bin/grep]

Filename1-nt filename2 If filename1 is newer than filename2, then true [/tmp/install/etc/services-nt/etc/services]

Filename1-ot filename2 If filename1 is older than filename2, then true [/boot/bzimage-ot Arch/i386/boot/bzimage]

The Linux Shell compares character notation:
    -eq   equals-ne    not equal to-GT    greater than-lt    less than-le equals-ge is greater than or equal to-   z    empty string =    two characters typeface etc! =    two characters unequal-n    non-empty string
String comparison operators (note the use of quotation marks, which is a good way to prevent whitespace from disturbing the code)
    -Z String                 if string length is zero, true             [-Z $myvar]-n string                 If string length is nonzero, true             [-n $myvar] string1 = string2< c5/> true if string1 is the same as string2     [$myvar = one and three] string1! = string2        True if string1 differs from string2     [$ MyVar! = one and three]
Arithmetic comparison operators
Num1-eq num2              equals         [3-eq $mynum] num1-ne num2              not equal to       [3-ne $mynum] num1-lt num2               less than        [3 -lt $mynum] Num1-le num2            less than or equal to     [3-le $mynum] num1-gt num2             greater than          [3-GT $mynum] Num1-ge num2
     greater than or equal to    [3-ge $mynum]
Sample script:
#!/bin/Bash # This script prints a message about your weightifYou give it your # weightinchKilos and hightinchcentimeters. if[ ! $# ==2]; Then    Echo "Usage: $ weight_in_kilos length_in_centimeters"ExitfiWeight=" $"Height=" $"Idealweight=$[$height- the]if[$weight-le $idealweight]; Then    Echo "You should eat a bit more fat."    Else    Echo "You should eat a bit more fruit."    fi# weight.SH  -  MaxYou should eat a bit Morefruit. # weight.SH  -  Max  -Usage:./weight.SHWeight_in_kilos length_in_centimeters position parameter $1, $2,..., $N, $ #代表了命令行的参数数量, $ A represents the name of the script, the first parameter represents the $1, the second parameter represents $2, and so on, the total number of parameters exists $ #中, the above example shows how to change the script, if the parameter is less than or 2 extra to print out a message.  Execute, and view the situation. # bash-X Tijian.SH  -  the+ weight= -+ height= the+ idealweight= -+'['  --le - ']'+Echo 'You should eat a bit more fat.'You should eat a bit Morefat. Which-X is used to check the execution of the script. Hell Various comparison operators

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.