Test conditions for Linux shell programming

Source: Internet
Author: User

Executes the command and uses the command state return value to determine certain states

echo $?

0 stands for Success, not 0 for unsuccessful

To test an expression

Numerical comparison

-eq equals

The return value is 1, indicating 2 is not equal to 3


-ne Not equal to

The return value is 0 description 2 is not equal to 3


You can also use brackets directly without the need to enter the test keyword

A return value of 1 means 2 equals 2


-gt left is greater than right

2 on the left is greater than 1 on the right

-lt left is less than right

Left 2 not less than right 1

The rest is Le (less than equals) and GE (greater than or equal)


string comparison

Note: When comparing strings, it is best to use double brackets, if there is a variable, it is best to enclose it in double quotation marks.

Whether > is greater than < is less than

Why is a less than B? Because the characters are in the comparison, the ASCII value is used

! = does not equal

The following comparison is rarely seen, called pattern matching.

Whether the left-hand string can be matched to the right-hand match

That's even a match. Two times, first time success, second failure

If we're going to make it this second time, you can write.

-Z "STRINGS" to determine whether the STRINGS string is empty

-N "STRINGS" to determine whether the STRINGS string is not empty

Because the variable name value is NULL, the return value of-n is 1.

Script return value

The default is the state return value of the last command in the script, and the administrator can customize the script return value

EXIT[N] n is the value returned, and the shell script exits the program when it encounters exit

File test

To see if a file exists-E

See if a file is a block device-B


To see if a file is a character device file

-C

See if a file is a normal file

-F

To see if a file is a catalog file

-D


To see if a file is a symbolic link file

-h/-l

to see if a file is a pipe file

-P

to see if a file is a socket file

-S

to see if a file exists and is readable for the current user

-R

to see if a file exists and is writable for the current user

-W

to see if a file exists and can be executed for the current user

-X

See if a file exists and has SGID

-G

See if the file has Suid-u


See if the file has Sticky-k

See if a file exists and has content-s

There are files, but there is no content in the file

To see if the file has been modified since it was last read

-N

View whether the current user is the owner of the specified file-o


View whether the current user belongs to the specified file group-G



Binocular test

-ef two files are two hard-linked files that point to the same file

Is the file on the left of-nt newer than the file on the right? Time is dominated by the most recent modification time

Is the file on the left of-ot older than the file on the right? Time is dominated by the most recent modification time

Aaaa.text than Fstab.1 New

Fstab.1 files older than Aaaa.text


Combination Test

Command1 && Command 2

If the first command is wrong, you don't run the second command.

If the first command is right, the second command continues to run

Command1 | | Command2

If the first command is wrong, the second command continues to run

If the first command is true, the second command is not run.

&& is equivalent to -a note that if it is a-a argument then the expression is either a single square bracket

|| Equivalent to-O


Non-command

!command1



Test conditions for Linux shell programming

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.