Shell test, shelltest

Source: Internet
Author: User

Shell test, shelltest

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.


Who can explain a shell command, if test! -H // Home

This is a judgment expression.
Use man test to view the help of the test command:
-H FILE
FILE exists and is a symbolic link (same as-L)
The file exists and is a character Link (the same as the-L option ).

! EXPRESSION
EXPRESSION is false
The expression is false, that is, "Non" Operation ("reverse" operation ).

Therefore, the meaning of this judgment statement is:
If the file ../Home does not exist, or the file exists but is not a character link.

If it is a character link, use ls-l.../../Home to see a link symbol-> link to another file. For example:
$ Ls-l/bin/sh
Lrwxrwxrwx 1 root 4 Mar 16 2009/bin/sh-> bash

Shell programming if, test command, where is my understanding wrong?

If test-z $1
Then echo 'Please input a file name'; exit # Add an exit ticket
Else
...
Otherwise, if test-x $1 will return an error

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.