The test command usage of the Linux shell is detailed

Source: Internet
Author: User

Basic format:
Test expression

Expression constructed for the test command.
Here expression is any valid expression that the test command can understand, and the simplified format will be the most common format that the reader may aiiowed see
return value:
The test command either returns 0 (TRUE) or returns 1 (false).

The type of expression that test can understand is divided into four categories:

    • Expression judgment
    • string comparison
    • Number comparison
    • File comparison

1) Judgment expression

if test (expression is true)
if test! An expression is false
Test expression 1–a expression 22 expressions are true
Test expression 1–o Expression 22 expressions have a true

2) Judging the string

Test–n string string is not zero in length
Test–z string string has a length of zero
Test string 1 = string 2 string equal
Test string 1! = String 2 string unequal

3) Judging integers

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 is less than or equal to Integer 2
Test integer 1–lt integer 2                           integer 1 is less than integer 2
Test integer 1–ne integer 2                       integer 1 is not equal to integer 2


4) Judgment document

Test File1–ef File2 Two files with the same device number and I node number
Test file1–nt File2 file 1 newer than file 2
Test File1–ot File2 file 1 older than file 2
Test–b file files exist and are block device files
Test–c file files exist and are character device files
Test–d file files exist and are directories
Test–e file files exist
test–f file files exist and are regular files
Test–g file exists and the group ID is set
Test–g file files exist and belong to valid group ID
Test–h file exists and is a symbolic link (same-l)
Test–k file files exist and sticky bits are set
Test–b file files exist and are block device files
Test–l file exists and is a symbolic link (same-h)
Test–o file files exist and belong to valid user ID
Test–p file exists and is a named pipe
Test–r file files exist and are readable
Test–s file exists and is a socket
Test–t FD file descriptor is opened in a terminal
Test–u file exists and its Set-user-id bit is set
Test–w file files exist and are writable
test–x file files exist and are executable

Test XXX can be simply written in the form of [xxx].

Note : When you use the "[" shorthand test, the space after the left bracket and the space in front of the closing parenthesis are required , and if there are no spaces, the shell cannot tell when the expression begins to end.

Other words

Test option File

It can all be changed to write:

[Option file]

For example:

Test–w File

Rewrite into

[–w File]

Example

Determines whether the first argument is an empty string, and is not empty to print
If Test-n "$"
Then
echo "$"
Fi

Test, put it in a file
#!/bin/sh

If Test-n "$"
Then
echo "$"
Fi

Perform
chmod +x test.sh
./test.sh www.linuxpig.com

The test command usage of the Linux shell is detailed

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.