Shell command test usage and examples in Linux

Source: Internet
Author: User
Tags aliases

The test command expects to find a parameter on the command line that is considered empty when the shell does not assign a value to the variable. This means that when the script is being processed, test will report the error once the script is looking for a parameter that does not exist.

When you try to protect the script, you can solve the problem by including all the arguments in double quotes. The shell then expands the variable, and if the variable has no value, it passes a null value to test. Another approach is to add an extra check in the script to determine whether the command-line arguments are set. If no command-line arguments are set, the script tells the user that the parameter is missing and then exits. We'll use some examples to illustrate all of this more specifically.

Test and [command

Although the test command is included in each version of Linux and UNIX, the command has a more commonly used alias-the left parenthesis: [. Test and its aliases can usually be found in/usr/bin or/bin (depending on the operating system version and vendor).

When you use the left parenthesis instead of test, you must always follow a space, criteria to evaluate, a space, and a closing bracket. The right bracket is not an alias for anything, but rather the end of the required evaluation parameter. The spaces on either side of the condition are required, which means that you want to call test to distinguish between character/pattern matching operations that also frequently use square brackets.

The syntax for test and [is as follows:

Test expression

[Expression]

In both cases, test evaluates an expression and then returns True or false. If it is used in conjunction with an if, while, or until command, you can have extensive control over the flow of the program. However, you do not need to use the test command with any other structure, and you can run it directly from the command line to check the state of almost anything.

Because they are aliases to each other, using test or [requires an expression. An expression is typically a comparison of text, numbers, or file and directory properties, and can contain variables, constants, and operators. The operator can be a string operator, an integer operator, a file operator, or a Boolean operator-we'll describe each operator in turn in the following sections.


Test command usage. Features: Checking files and comparison values
1) The expression of judgment
if test (expression is true)
if test! expression is False
Test expression 1–a expression 22 expressions are true
Test expression 1–o Expression 22 expressions have one true
2) Judging string
Test–n string string has a non-zero length
The length of the Test–z string string is zero
Test string 1 = string 2 string equal
Test string 1! = String 2 String unequal
3) to determine the integer
Test integer 1–eq integer 2 integer equal
Test integer 1–ge integer 2 integer 1 greater than equal to Integer 2
Test integer 1–gt integer 2 integer 1 greater than integer 2
Test integer 1–le integer 2 integer 1 less than equal to Integer 2
Test integer 1–lt integer 2 integer 1 less than integer 2
Test integer 1–ne integer 2 integer 1 not equal to Integer 2
4) Judgment document
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
Test–d file exists and is a directory
Test–e file files exist
test–f file files exist and are regular files
The Test–g file exists and the group ID is set
The Test–g file exists and belongs to a valid group ID
The Test–h file exists and is a symbolic link (with-l)
The Test–k file exists and the sticky bit is set
The Test–b file exists and is a block device file
Test–l file exists and is a symbolic link (same-h)
The Test–o file exists and belongs to a valid user ID
Test–p file exists and is a named pipe
Test–r file files exist and can be read
Test–s file exists and is a socket
The test–t FD file descriptor is opened at a terminal
The Test–u file exists and sets its Set-user-id bit
Test–w file files exist and can be written
The Test–x file exists and is executable
The basis of each conditional statement is to determine what is true and what is false. Knowing how it works will determine whether you're writing a quality-generic script or a script that you'll be proud of.
The ability of a Shell script is often underestimated, but in fact its ability to play is constrained by the ability of the scripting writer. The more you learn, the more you can compose a file like a juggler to automate tasks and simplify your management efforts.
Example 1. Comparison of two size

  code is as follows copy code
#/bin/bash
echo "Please enter two number"
read a
Read B
if test $a-eq $b
then echo "no.1 = No.2″
Elif Test $a-gt $b
then echo "No.1 > No.2″
Else echo" No.1 < No.2″
Fi
Example 2. Find the file in the/root/directory
#/bin/bash
echo "Enter a file name:"
Read a
If test -e/root/$a
then echo "The file is exist!"
Else echo "The file is not exist!"
Fi
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.