Conditional judgment in a Linux shell

Source: Internet
Author: User
Tags logical operators

1. Exit status

In a Linux system, every time a command executes, the system returns an exit state, which is stored in the variable, which is an integer value that we can determine whether the command is running correctly.

Typically, the exit status value is 0, indicating that execution was successful and not 0 when execution failed.

The meaning of the POSIX-defined exit status and exit status:

0 successful operation

1-255 failed to run, script command, System command error, or parameter pass error

126 The command was found but could not be executed

127 the command to run was not found

128 command ended by System force

2. Test commands

With the test command, expression is an expression:

Test expression

To improve readability, you can use a different format:

[Expression]

It is important to note that there is a space between the curly brace and the expression and cannot be omitted. This approach, combined with statements such as if, case, and while, can be used as a judgment condition in a shell script.

3. Integer comparison Operators

Comparing two numbers in the shell is not like using operators such as ">" in C + +, but rather using formatting similar to parameter options.

-eq if equal is true

-ge true if it is greater than or equal to

-GT is true if it is greater than

-le true if it is less than or equal to

-lt true if it is less than

-ne if not equal to True

The parameters can be interpreted in such a way as e (equal), g (Greater), T (than), L (less), n (not), so as to facilitate memory.

4. String-related operators

-n string string not NULL is True

-Z string string is empty then true

string1 = string2 string equal is true (or = = can also)

String1! = string2 string is not equal to true

One thing to note here is that when you use the-n operator for judgment, you need to be careful to add double quotes around the variable.

For example if [-N $string] should be written if [-N ' $string "], otherwise the expression will always return true, because when the string variable is empty, it is equivalent to if [-n].

5. File operators

-D file Tests if file is a directory

-E file tests if file exists

-F file to test whether files are normal

-R file to test whether files are process-readable

-S file to test if file length is not 0

-W file to test if file is a process writable file

-X file Tests if file is a process executable

-L file Tests if file is symbolic of a link

6. Logical operators

! Expression non-

Expression1–a Expression2 and

Expression1–o expression2 or

Multi-nesting:

For example if [$a = = 1] && [$b = = 1-o $b = = 3]

Conditional judgment in a Linux shell

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.