Bash comparison Classification

Source: Internet
Author: User

Bash comparison Classification
Test method:
[Expression]
[[Expression]
Test expression

Common condition tests in bash:
1. Integer Test
2. File Test
3. String comparison test
Integer test:
-Eq: test whether two integers are equal. For example, $ A-eq $ B. If the values of A and B are equal, 0 is returned. Otherwise, A non-0 value is returned.
-Nq: test whether two integers are unequal. equal to false, unequal to true
-Gt: test whether one number is greater than the other. If the value is greater than true, otherwise the value is false.
-Lt: test whether a number is smaller than zero. If the value is smaller than true, the value is false.
-Ge: test whether a number is greater than or equal to the other number. If it is greater than or equal to true, otherwise it is false.
-Le: test whether a number is less than or equal to the other number. If it is less than or equal to true, otherwise it is false.

INT1 = 34
INT2 = 44
[$ INT1-eq $ INT2]
[[$ INT1-eq $ INT2]
Test $ INT1-eq $ INT2

If [grep "^ USERNAME \>"/etc/passwd]; then # This method is incorrect.
If grep "^ USERNAME \>"/etc/passwd; then # is written in this way.

File test:
-E FILENAME: whether the file exists
-F FILENAME: test whether the file is a common file.
-D FILENAME: test whether the specified path is a directory.
-R FILENAME: test whether the current user has the read permission on the specified file.
-W FILENAME: test whether the current user has the write permission on the specified file.
-X FILENAME: test whether the current user has the execution permission on the specified file.

String comparison test:
= OR =: equality test, equal to true, not equal to false [$ A = $ B], equal signs must have spaces at both ends
! =: Whether the test is unequal. It is not a Wei Zhen, and the test is false.
>:
<:
-Z string: test whether the specified string is null. If it is null, it is true. If it is not null, it is false.
-N string: test whether the specified string is null. If it is not null, it is true. If it is null, it is false.

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.