Usage of the test command in Linux Shell

Source: Internet
Author: User

 
Basic Format:
Test expression

Expression is the expression constructed by the test command.
Expression is any valid expression that can be understood by the test command. The simplified format is the most common format that readers may refer.
Return Value:
The test command returns 0 (true) or 1 (false ).

The expression types that test can understand are divided into four types:

  • Expression judgment
  • String comparison
  • Number comparison
  • File comparison

1) judgment Expression

If test (expression true)
If test! The expression is false.
Test expression 1-A expression 2 both are true
Test expression 1-O expression 2 one of the two expressions is true

 

2) judge the string

The length of the test-N string is non-zero.
The length of the test-Z string is zero.
Test string 1 = string 2 strings are equal
Test string 1! = String 2 string

 

3) judge the integer

Test integer 1-EQ integer 2 integer equal
Test integer 1-ge integer 2 integer 1 is greater than or equal to integer 2
Test integer 1-GT integer 2 integer 1 is greater than integer 2
Test integer 1-Le integer 2 integer 1 less than or equal to integer 2
Test integer 1-lt integer 2 integer 1 less than integer 2
Test integer 1-ne integer 2 integer 1 is not equal to integer 2

 
4) file judgment

Test file1-Ef file2 two files have the same device number and I node number
Test file1-nt file2 file 1 is newer than file 2
Test file1-ot file2 file 1 is older than file 2
The test-B file exists and is a block device file.
The test-C file exists and is a character device file.
The test-D file exists and is a directory.
The test-e file exists.
The test-F file exists and is a regular file.
The test-G file exists and the group ID is set.
The test-G file exists and belongs to the valid group ID.
The test-H file exists and is a symbolic link (same as-l)
The test-K file exists and the sticky bit is set.
The test-B file exists and is a block device file.
The test-l file exists and is a symbolic link (same as-h)
The test-o file exists and is a valid user ID.
The test-P file exists and is a named pipe.
The test-r file exists and is readable.
The test-s file exists and is a socket.
The test-t fd file descriptor is opened on a terminal.
The test-u file exists and its set-user-ID bit is set.
The test-W file exists and can be written.
The test-X file exists and can be executed.

Test XXX Can be abbreviated as [XXX.

Note:: When "[" abbreviated as test is used, the Space following the left brackets andSpaces are requiredWithout spaces, shell cannot tell when the expression starts and ends.

That is to say

Test option File

It can be rewritten:

[Option file]

For example:

Test-W File
 
Rewrite

[-W file]

[Example]

// Determine whether the first parameter is a Null String. If it is not null, it is printed.
If test-n "$1"
Then
Echo "$1"
Fi

Test and put it in the file
#! /Bin/sh

If test-n "$1"
Then
Echo "$1"
Fi

Run
Chmod + X test. Sh
./Test. Sh www.linuxpig.com

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.