Shell test [command usage

Source: Internet
Author: User
Address: http://www.examw.com/linux/all/114976/index.html

Test command usage. Function: Checks files and comparison values.

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.

The basis of each condition statement is to determine what is true or false. Whether or not you understand the working principle of the script will determine whether you are writing a quality script or a script that you will be proud.

The ability of shell scripts is often underestimated, but its ability is actually limited by the ability of script writers. The more you know, the more you will be able to write a file like a magic to automate tasks and simplify your management work.

All operations performed in shell scripts (except the simplest Command Group) must check the conditions. All shell scripts, in a broad sense, can be divided into the following three categories:
If {condition exists} then...

While {condition exists} Do...

Until {condition exists} Do...

No matter what subsequent operations are, these logic-based commands determine whether a condition exists or not to determine subsequent operations. The test command is a utility that allows you to determine the existence of conditions in each case. Therefore, a thorough understanding of this command is essential for successful shell scripts.

Working Principle

The minimum definition of the test command may be to evaluate an expression. If the condition is true, a value of 0 is returned. If the expression is not true, a value greater than 0 is returned, which can also be called a false value. The easiest way to check the status of the last command is to use $? Value. For demonstration purposes, all examples in this article use this parameter.

The test command expects to find a parameter in the command line. When shell does not assign a value to the variable, the variable is treated as null. This means that test reports the error once the parameter found by the script does not exist during script processing.

When trying to protect the script, you can solve this problem by including all parameters in double quotation marks. Shell then expands the variable. If the variable has no value, a null value is passed to test. Another method is to add an additional check process in the script to determine whether the command line parameters are set. If the command line parameter is not set, the script will tell the user that the parameter is missing and then exit. We will use some examples to describe all these content in a more specific way.

Test and [command

Although each version of Linux and UNIX contains the test command, the command has a more common alias-left square brackets :[. Test and Its alias can be found in/usr/bin or/bin (depending on the operating system version and vendor.

When you use left square brackets instead of test, you must always follow a space, a condition to be evaluated, a space, and right square brackets. The right square brackets are not the alias of anything, but the end of the parameter to be evaluated. Spaces on both sides of the condition are required. This indicates that test is called to distinguish it from the character/pattern matching operation that uses square brackets frequently.

The syntax of test and [is as follows:

Test expression

[Expression]

In both cases, test evaluates an expression and returns true or false. If it is used in combination with the if, while, or until command, you can control the Program Stream extensively. However, you do not need to use the test command with any other structure; you can run it directly from the command line to check the status of almost anything.

Because they are aliases of each other, using test or [requires an expression. An expression is generally a comparison of text, numbers, or files and directory attributes, and can contain variables, constants, and operators. Operators can be string operators, integer operators, file operators, or boolean operators-we will introduce each operator in sequence in the following sections.

Test File Operator

Using these operators, you can perform different operations in the program based on the evaluation results of the file type:

-B file: if the file is a special file, it is true.

-C file: if the file is a special character file, it is true.

-D file: if the file is a directory, it is true.

-E file: true if the file exists

-F file: if the file is a normal file, it is true.

-G file: If the sgid bit of the file is set, it is true.

-G file: true if the file exists and belongs to the group

-K file: true if the adhesion bit of the file is set

-O file: true if the file exists and belongs to the user

-P file: if the file is a named pipe, it is true.

-R file: true if the file is readable

-S file: true if the file length is not zero

-S file: if the file is a special socket file, it is true.

-T fd: If FD is an open file descriptor connected to a terminal (FD is 1 by default), it is true.

-U file: If the SUID bit of the file is set, it is true.

-W file: true if the file is writable

-X file: true if the file is executable

 

 

Shell test [command usage (2)

20:23:52 | category: Stenographer | Tag: | font size, medium/small subscription

Address: http://www.examw.com/linux/all/114977/index.html

The following example shows the running status of this simple operation:

$ LS-l

Total 33

Drwxr-XR-W 2 root Root 1024 Dec 5 05:05 lst

-RW-1 Emmett users 27360 Feb 6 Evan

-Rwsrwsrwx 1 Root 152 Feb 6 hanhannah

Drwxr-XR-x 2 Emmett users 1024 Feb 6 Karen

-RW ------- 1 Emmett users 152 Feb 6 Kristin

-RW-r -- 1 Emmett users 152 Feb 6 Spencer

$

$ Test-r Evan

$ Echo $?

0

$ Test-r Walter

$ Echo $?

1

$

Because the first evaluation is true-the object exists and the readable-return value is true, or 0. Because the file for the second evaluation does not exist, the value is false and the return value is not zero. It is important to specify the value to zero or non-zero, because 1 is not always returned in the case of failure (although this is usually returned), a non-zero value may be returned.

As mentioned at the beginning, in addition to using test, you can also use square brackets [] to enclose the command to issue the same command to shell-as shown below:

$ [-W Evan]

$ Echo $?

0

$ [-X Evan]

$ Echo $?

1

$

Similarly, the first expression is true, and the second expression is false-as indicated by the return value. You can also use the following command to compare the two files:

File1-Ef file2 test to determine whether two files are connected to the same device and whether they have the same inode number

File1-nt file2 test to determine whether the first file is updated later than the second file (determined by the modification date)

File1-ot file2 test to determine if the first file is older than the second file

The following example shows how to use these operators to compare files:

$ [Evan-nt Spencer]

$ Echo $?

0

$ [Karen-ot Spencer]

$ Echo $?

1

$

Files named Evan are updated than files named Spencer and therefore evaluated as true. Similarly, a file named Karen is updated than a file named Spencer, so this evaluation is false.

String comparison operator

As shown in the title, this set of functions compares the value of a string. You can check whether they exist, are the same, or are different.

String test to determine whether the string is not empty

-N string test to determine whether the string is not null. The string must be recognized by test.

-Z string test to determine whether the string is null. The string must be recognized by test.

String1 = string2 test to determine if string1 is the same as string2

String1! = String2 test to determine if string1 is different from string2

One of the most useful tests for any variable is to judge whether its value is not empty. You can simply put it in the test command line to execute this test, as shown in the following example:

$ Test "$ variable"

We strongly recommend that you use double quotation marks to enclose variables for shell to identify variables (even if the variables are empty ). By default, the basic string evaluation and the-n test are functionally the same, as shown in the following example:

# Example1

If test-n "$1"

Then

Echo "$1"

Fi

Execute the code in the above example and give the following results based on whether $1 exists:

$ Example1 Friday

Friday

$

$ Example1

$

If you change the code to the following format, the results will be the same:

# Example2

If test "$1"

Then

Echo "$1"

Fi

As follows:

$ Example2 Friday

Friday

$

$ Example2

$

All of these indicate that-N is usually not required, which indicates the default operation.

To view the possibilities from a different perspective, you can replace-N with another option and check whether the value is null (relative to a non-empty value ). This can be implemented using the-Z option. The code is:

# Example3

If test-z "$1"

Then

Echo "no values were specified"

Fi

Run the following command:

$ Example3

No values were specified

$ Example3 Friday

$

If the program is run without a command line parameter and the expression is evaluated as true, the text in the execution block is used. If a value exists in the command line, the script exits and does not perform any operations. It is useful to place the evaluation operation at the beginning of the script, which can check the variable value in advance before any further error may occur.

 

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.