Linux conditional judgment: eq, NE, GT, lt, GE, le

Source: Internet
Author: User

Introduction to Commands:


Condition Judgment:

If the user does not exist

Add user, give password and display add success;

otherwise

Show if no already in, no add;

Variable name:

1 , can contain only letters, numbers, and underscores, and cannot begin with numbers;

2 , should not be the same as the existing environment variables in the system;

3 , it is best to see the name and know righteousness;

Bash how to achieve the conditional judgment?

Condition Test Type:

integer test

character Test

file Test

Expressions for Conditional tests:

[Expression]

[[Expression]]

Test Expression       

Integer comparison:

-eq: tests whether two integers are equal, such as $A-eq $B

-ne: test whether the two integers are unequal, unequal, true, equal, false;

-GT: tests whether one number is greater than the other, greater than, true, or false;

-lt: tests whether one number is less than the other, less than, true; otherwise, false;

-ge: greater than or equal to

-le: Less than or equal to

The logical relationship between commands:

logic and: &&

When the first condition is false, the second condition is no longer judged and the final result is already there;

When the first condition is true, the second condition must be judged;

logical OR : | |    

4 . Command instance:

If the user User6 does not exist, the user is added User6

! ID USER6 && useradd user6

ID User6 | | Useradd User6

If the number of rows in the/etc/inittab file is greater than 100, a large file is displayed;

[' Wc-l/etc/inittab | cut-d '-f1 '-gt] && echo "Large file."  

If the user exists, the user is already present, otherwise the user is added;

ID user1 && echo "user1 exists." | | Useradd user1

If the user does not exist, add; otherwise, show that it already exists;

! ID user1 && Useradd user1 | | echo "User1 exists."

If the user does not exist, add and give the password, otherwise, show that it already exists;

! ID user1 && useradd user1 && echo "user1" |passwd--stdin User1 | | echo "User1exists."


This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1863256

Linux conditional judgment: eq, NE, GT, lt, GE, le

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.