Linux Learning Note--bash script programming the second, condition judgment

Source: Internet
Author: User

Bash Script Programming second, conditional judgment

Condition Judgment:

If the user does not exist

Add user, give password and display add success;

Otherwise

Show that the user is already present, not added



How do I make conditional judgments in bash?

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;

-QT: Tests whether one number is greater than the other, greater than true;

-LT: Tests if 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 second condition is true, the second condition must be judged;

logical OR: | |

If the user user2 does not exist, the user is added User2

! ID user2 && useradd user2

ID User2 | | Useradd User2



variable name:${varname}, parentheses can sometimes be omitted

1, can only contain sub-mother, number and underscore, and can not start with a number;

2, should not be in the system with the existing system variables duplicate name;

3, it is best to see the name of the meaning;


If the user User1 exists, it shows that the user already exists; otherwise, it is added;

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


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

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


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

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


conditional judgment, control structure :


Single Branch if statement


if judgment condition; Then

Statement1

Statement2

...

Fi


Dual-Branch If statement:


if judgment condition; Then

Statement1

Statement2

Else

Statement3

Statement4

Fi




























This article is from the "11913800" blog, please be sure to keep this source http://11923800.blog.51cto.com/11913800/1844019

Linux Learning Note--bash script programming the second, condition judgment

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.