Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Learning Notes-5.3 shell programming 2

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

5.3 shell programming 2

Condition judgment:

If the user does not exist

Add the user, give the password and show that the addition was successful

otherwise

Shows that if it already exists, it has not been added

How to realize conditional judgment in bash?

Conditional test type:

Integer test

Character test

File testing

Expression for conditional test:

[expressopm] there must be spaces before and after

`expressopm'

Test expression

Integer comparison:

-eq: tests whether two integers are equal; equality is true 0, such as $A-eq $B.

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

-gt: test whether one number is greater than another: greater than, true

-lt: test whether one number is less than another

-ge: greater than or equal to

-le: less than or equal to

Logical relationship between commands:

Logic and: & &

When the first condition is false, there is no need to judge the second condition, and the final result is as follows:

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

Logic or: | |

Id user1 & > / dev/null & & echo "Hello,student."

If user user6 does not exist, add user user6

! Id user6 & & useradd user6

Id user6 | | useradd user6

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

The existing environment variables in the system have the same name

It's best to see the name and know the meaning.

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, it shows that the user already exists; 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 "user1 exists."

Practice, write a script, and complete the following requirements:

1 add 3 user user1,user2,user3;, but first determine whether the user exists

Does not exist and then add

2 after the addition is completed, it shows that a total of several users have been added: of course, it cannot be included because in advance

Existing but not added

3 finally shows how many users there 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 UID is 0: then

Show as administrator

otherwise

Show as a normal user

NAME = user16

USERID = `NAME` (reverse quotation mark below ~) command reference indicates the execution result of the command

If [$USERID-eq 0]; then

Echo "Admin"

Else

Echo "common user."

Fi

The reverse quotation mark reference indicates the execution result of the command

There are no backquotes to indicate the execution status of the command (successful or not)

If id $NAME;then

Practice writing a script to complete the following requirements:

Given a user:

1 if its UID is 0, it shows that this is the administrator

2 otherwise, it will be shown as an ordinary user

Nano third.sh

#! / bin/bash

#

NAME = user1

USERID = `NAME`-u $id

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

Condition judgment, control structure:

Single branch if statement

If judgment condition; then (then writes the next line, the semicolon can be omitted)

Statement1

Statement2

...

Fi

Double-branched 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 users in this variable, and delete their home directory as well

3 display the information of the "user deletion completed" class

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report