Shell script programming condition test--test

Source: Internet
Author: User
Tags echo command

The test command has three types of test expressions, the first of which is the comparison of numeric values, the second is the comparison of strings, and the third is the testing of files, such as testing the existence of a file.


1. Numeric test , two operands required for comparison

-EQ: Test whether two data are equal, equal to true, and not as false

-ne: Whether the two values tested are unequal, not equal to true, and equal to false;

-LT: Two values tested, whether the left is less than the right, less than true, greater than false;

-GT: Two values to be tested, whether the left is greater than the right, greater than true, less than false;

-le: The two values to be tested, whether the left is less than or equal to the right;

Summed up is:eq equal, NE unequal, GT greater than, LT is less than, ge greater than equals, le less than equals, not non, mod modulo

~]$ Test 3-eq 4 ~]$ echo $? 1
~]$ Test 3-ne 4 ~]$ echo $? 0
~]$ Test 3-GT 4 ~]$ echo $? 1
~]$ Test 3-LT 4 ~]$ echo $? 0

We will need to make a conditional judgment when we write the script, so we need to use the test command, which in the script only needs to write the contents of the test in brackets, such as the following two methods are the same:

~]$ [2-eq 2] ~]$ echo $?0
~]$ Test 2-eq 2 ~]$ Echo $?0

Note : This type of command generally has no result, only the execution status return value, so we need to use the echo command to display the results of the execution when we want to see the comparison.


This test command can be used in conjunction with the IF command to make conditional judgments, such as using Vim to write a script:

#!/bin/bashif [1-eq 1]; Then echo "Same" else echo "not the same" fi
~]$ vim B1 ~]$ bash B1 same

Then change the number and the output will be different:

#!/bin/bashif [1-eq 2]; Then echo "Same" else echo "not the same" fi
~]$ vim B1 ~]$ bash B1 not the same

Like other test conditions can also be written in this script, not listed.




2. String Test:

Binocular operator:

==|=: The two strings tested are equal, the same is true, and the differences are false;

Like what:

~]$ [ABC = ABC] ~]$ echo $?0
~]$ [ABC = abd] ~]$ Echo $?1

! =: The two characters tested are not the same, the difference is true, the same is false;

The function of this symbol is the opposite of = =, for example

~]$ [ABC! = ABC] ~]$ echo $?1
~]$ [ABC! = ABD] ~]$ Echo $?0

: The two strings tested in the ASCLL code table corresponding to the binary data, the left is greater than the right, greater than true, less than false;

<: Tested two strings in the ASCLL code table corresponding to the binary data, the left is less than the right, less than true, greater than false;

Like what:

~]$ [[ABC < ABD]] ~]$ Echo $?0
~]$ [[ABC < ABB]] ~]$ Echo $?1

Note that the "=~" of the:<,> and head is not enforceable in only one of the brackets, but only in the case of two bracketed parcels.


=~: Tested two strings, left string, can be matched to the right pattern, can match to true, cannot match false;

~]$ [[ABC =~ ABB]] ~]$ Echo $?1
~]$ [[ABC =~ AB]] ~]$ echo $?0

The result of the top two examples is that ABB cannot match ABC, and AB can match ABC


Monocular operator:

-Z ' string ': Determines whether the specified string is a null string, NULL is true, non-null is false;

-N ' string ': Determines whether the specified string is a non-empty string, the non-null is true, and the null is false;


Note :

1. Usually, strings are quoted, single quotes, double quotation marks, all lines, according to the actual situation;

2.[[]] and [] in some cases, may have different meanings, to distinguish between use;

3. Both the expression and the operator must add a space;


3. File Status test:

1) file existence test

-A file: The test files exist as true and do not exist for false;

-e File: The test files exist as true and do not exist as false;

(These two functions are similar)


2) file type Test (test presence)

-B File: whether the files being tested are present and whether they are block devices;

-C File: Whether the tested file exists and is a character device;

-D File: Whether the tested files exist and are for directory devices;

-F File: The test file exists and is a normal file;

-h|-l file: Whether the files being tested exist and whether they are symbolic linked files;

-P File: The file being tested is present and is a socket file;

-S file: whether the tested file exists and is a socket file;


3) File access permission settings:

-R file: whether the file being tested exists, and whether the current valid user is readable;

-W File: Whether the test file exists, and whether the current valid user can write;

-X file: whether the file being tested exists, and whether the current valid user is executable;


4) file special permission identification test:

-u file: The file being tested exists and the SUID permission is set, the file exists and the set SUID permission is true; otherwise false;

-G file: The test file exists and the Sgid permission is set;

-K File: whether the files being tested exist and whether sticky permissions are set;


5) file Ownership test:

-O File: Whether the file being tested exists and whether its owner is the current active user;

-G File: Whether the file being tested exists and whether the group is currently a valid user;


6) Whether the contents of the file are empty:

-S file: The document being tested exists and the content is not empty, exists and the content is not empty is true; otherwise false;

7) time Stamp test:

-N File: The test file has been modified since the last time it was modified;


8) Binocular test:

FILE1-EF File2: Tests whether the two files point to the same inode of the same file system

9) hard link;

File1-nt file2: Two files tested, File1 is newer than file2;

File1-ot file2: Two files tested, whether file1 is older than file2;




You can add a logical operation in a test statement:

The first type of expression:

[-o/tmp/test] && [-s/tmp/test]

[-o/tmp/test] | | [-s/tmp/test]

The second type of expression:

[-o/tmp/test-a-s/tmp/test]

[-o/tmp/test-o-s/tmp/test]

[!-o/tmp/test]

The effect of these two expressions is the same



After the command executes there will be a result return value, this return value is divided into two kinds, the first is the output of the normal command, the second is the state of the command execution, sometimes there may be no first return value, but there will be a second state return value. The number of the status return value is saved in the "?" In this variable, you can pass "echo $?" command to view. Depending on the number of status return values, we can determine the status of the command execution, respectively, corresponding to the following:

0: Success

1, 2,127: Returns the value of the system's reserved state, as we try to avoid these when we write our own scripts

3-126, 128-255: User-defined execution status return value

When we find these numbers, we can go through the commands we just performed to see how each number corresponds to the description. You can also have a return value in your own script, using the "Exit" command, followed by a number, which returns the number.

Note : The exit command can only return numbers.

Shell script programming condition test--test

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.