Learning note -5.3 Shell Programming 2

Source: Internet
Author: User
Tags stdin

5.3 Shell Programming 2


Condition Judgment:

If the user does not exist

Add user, give password and show add success

Otherwise

Displays if it already exists, does not add


How do I make conditional judgments in bash?

Condition Test Type:

Integer test

Character test

File test


Expressions for Conditional tests:

[EXPRESSOPM] must have a space before and after

[[EXPRESSOPM]]

Test expression


Integer comparison:

-eq: Test two integers for equality, 0 for example $A-eq $B

-ne: Test whether two integers are unequal; 0, equal, false

-GT: Tests whether a number is greater than another number: greater Than, true

-LT: Test whether a number is less than another number

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

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

Logical OR: | |


ID user1 &>/dev/null && echo "hello,student."

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

! ID USER6 && useradd user6

ID User6 | | Useradd User6


Variable name: can only contain letters, numbers and underscores, and cannot start with a number, should not be followed

The names of the existing environment variables in the system

It's better to see the name and righteousness

Nano second.sh

#!/bin/bash

LINES = ' Wc-l/etc/inittab '

#echo $LINES


Finlines = ' echo $LINES | Cut-d '-f1 '

#echo $FINLINES


[$FINLINES-GT] && echo "/etc/inittab is a big file."

|| echo "/etc/inittab is a small 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, it shows 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 "User1 exists."


Practice, write a script to complete the following requirements:

1 Add 3 User User1,user2,user3, but first determine if a user exists,

does not exist and then adds

2 After the add is complete, the display adds a total of several users: Of course, it cannot be included because prior

exists without adding the

3 finally shows how many users are on the current system

Nano adduser.sh

#!/bin/bash

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

--stdin user1 &>/dev/null | | echo "User1 exists."

! ID user2 &>/dev/null && useradd user2 && echo "User2" | passwd

--stdin user2 &>/dev/null | | echo "User2 exists."

! ID user3 &>/dev/null && useradd user3 && echo "User3" | passwd

--stdin user3 &>/dev/null | | echo "User3 exists."


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

echo "$USERS USERS."


If the UID is 0: Then

Show as Administrator

Otherwise

Show as normal user


NAME = User16

USERID = ' Id-u $NAME ' (~ down quote) command Reference indicates the execution result of the command

if[$USERID-eq 0];then

echo "Admin"

Else

echo "Common user."

Fi

An anti-quote reference indicates the execution result of the command

No anti-quotation marks indicate the execution status of the command (succeeded)


If ID $NAME;


Practice writing a script to complete the following requirements:

Given a user:

1 if its UID is 0, this is displayed as an administrator

2 Otherwise, it will be displayed as a normal user

Nano third.sh

#!/bin/bash

#

NAME = User1


USERID = ' Id-u $NAME '

[$USERID-eq 0] && echo "Admin" | | echo "Common user."


Conditional judgment, control structure:

Single Branch if statement

if judgment condition; then (then write next line, semicolon can be omitted)

Statement1

Statement2

...

Fi


Two-branch if statement:

if judgment condition; then

Statement1

Statement2

...

Else

Statement3

Statement4

...

Fi


Nano tt.sh

#!/bin/bash

#

NAME = User1

If ID $NAME &>/dev/null;then

echo "$NAME EXISTS."

Else

Useradd $NAME

echo $NAME | passwd--stdin $NAME &>/dev/null

echo "Add $NAME finished."

Fi



Exercise: Write a script to complete the following tasks

1 use a variable to save a user name

2 Delete the user in this variable and delete its home directory

3 Display the "User Delete complete" class information


Learning note -5.3 Shell Programming 2

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.