Shell Common Test commands

Source: Internet
Author: User

Pre-defined variables:

Predefined variables are a class of special variables that are predefined by bash programs, and users can only use predefined variables, not create new predefined variables, or assign values directly to predefined variables. The predefined variable is represented by a "$" match and another symbol combination.

$#: Indicates the number of positional parameters in the command.

$*: Represents the contents of all positional parameters.

$?: Represents the return state after the previous command was executed, with a return value of 0 indicating that the execution is correct, and any non-0 value indicates an exception occurred.

$ A: Represents the name of the currently executing script or program.

File test:

The-D test is a catalog (directory).

-e tests whether the directory or file exists (Exist).

The-f test is a file.

-R to test whether the current user has permission to read (read).

-W tests whether the current user has permission to write (write).

-X tests whether there are execute permissions (excute).

Attached:-D parameter Use example: #[-d/tmp/]

#echo $? View the return value of the previous command, 0 indicates that the TMP directory exists.

#[-d/tmp/] && echo "yes"//If the TMP directory is present, print YES on the screen.

Integer value comparison:

-eq 1th number Equals (Equal) 2nd

-ne 1th Number Not equal to (not Equal) 2nd number

-GT 1th number greater than (Greater Than) 2nd number

-LT 1th number less than (Lesser Than) 2nd number

-le 1th number less than or equal to (Lesser or Equal) 2nd number

-ge 1th number greater than or equal to (Greater or Equal) 2nd number

attached:-GT parameters Use Example: #[6-gt 5] && echo "Too many"

string comparison:

= The 1th string is the same as the 2nd string.

! = The 1th string is not the same as the 2nd string, where "! "means to take the opposite."

-Z Checks if the string is empty (Zero), and the variable that is not defined or given a null value is treated as an empty string.

Logic test:

and &&: logical And, meaning "and", only when the two conditions are true, the return value of the entire test command is 0. When testing with the test command, && can be changed to "-a".

or | | Logical OR, meaning "or", as long as there is one of the two conditions, the return value of the entire test command is 0 (the result is set). When testing with the test command, the "| |" Can be changed to "-O".

Non -! : Logical no, means "no", only if the specified condition is not true, the return value of the entire test command is 0 (the result is set).

       

Shell Common Test commands

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.