Introduction to the use of test commands for Linux systems

Source: Internet
Author: User
Tags error code

Linux systems sometimes need to detect whether a certain condition is set up, then use the Test command, Linux under the use of test has a lot of, the following small set together to learn how to use the Linux System Test command.

1. Judge the result of a command using test, which returns 0, or an integer. Returns 0 to True, returning an integer representing the error code

2. Get the return result of the previous command using $?

3. For example

There is a/home/www folder on my server, so ls/home/www The return result of this command is 0;

That is, echo $? Returns a value of 0

There is no such folder as/HOME/KKK on my server, so ls/home/kkk The return result of this command is the error code after the execution of the command;

The return result of Echo $ is 2. This 2 indicates that the file or folder does not exist.

4. Commonly used file-testing characters

-E Indicates whether this file name exists

-F Indicates whether this file name is a file

-D Indicates if this file name is a folder

-R Indicates whether this file name is readable

-W Indicates whether this file can be written

-X Indicates whether this file is executable

-S Indicates whether this file asks the socket

4.1/home/www as a folder on my server

So the result of Test-e/home/www is 0, that is true, this file name exists

So the result of Test-f/home/www is not 0, that is, false, this file name is not a file

So the result of Test-d/home/www is 0, that is true, this file name is a folder

4.2 On my server/home/kkk this file name does not exist

So the result of Test-e/home/kkk is not 0, that is, false, this file name does not exist

So the result of test-d/home/kkk is not 0, that is, false, this file name does not ask a folder.

The above is the Linux logical command of the test command to use the method introduced, if you want to determine whether a condition is set up, you will use the command, you have learned?

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.