2014-12-7

Source: Internet
Author: User

Linux Logical Relationships

1. How do I make conditional judgments in bash?

There are several types of condition tests in bash:

Integer test, character test, file test


Conditional test expression:

[Expression]

[[Expression]]


2, 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

3, the time of the order of the logical relationship:

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: | |

Example 1:

Create a user User1, if present on the output, does not exist does not return

ID user1 &>/dev/null && echo "Hello.student"

Example 2:

If the user does not exist, the user is added

!mid user1 && Usreadd user1

or ID user1 | | AddUser user1

Example 3:

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

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

If the user exists, the display is present, otherwise added

! ID user1 && echo "user exist" | | Useradd user1

User does not exist, add and give password, otherwise, display exists

! ID user1 && useradd user1 &>/dev/null &&eccho "user1" | passwd--stdin user1 &>/dve/null | echo "Users exist"

Example 4:

Write a script according to the following requirements:

Add 3 table users, but you need to determine whether the user is present, add, add, show how many users have been added, excluding the previous existence, and finally show how many systems on the current system

#! /bin/bash

! ID user1 && useradd user1 &>/dve/null && echo "user1" | passwd--stdin user1 &>/dev/null

! ID user2 && useradd user2 &>/dve/null && echo "User2" | passwd--stdin user1 &>/dev/null

! ID user31 && useradd user3 &>/dve/null && echo "User3" | passwd--stdin user1 &>/dev/null


User= ' Wc-l/etc/passwd | cut-d:-f1 '

echo "$USERS USERS"


To a user, if the UID is 0, show admin otherwise for common

#!/bin/bash

Name=user

Userid= ' Id-u $NAME '

[$NAME-eq 0] && echo "Admin" | | echo "Common"


4. Condition 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


The above script for UID is rewritten accordingly:

#!/bin/bash

Name=usr1

Userid= ' Id-u $NAME '

If [$USERID-eq 0]; then

echo "Admin"

Else

echo "Common user."

Fi

Because $userid only cited 1 times, so $userid can

Name=user1

If [' id-u $NAME '-eq 0];then

echo "Admin"

Else

echo "Common user."

Fi



This article is from the "Fred" blog, make sure to keep this source http://fredse.blog.51cto.com/3097149/1587307

2014-12-7

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.