Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell script applies "six" simulation to add users and delete users

Shulou Source: shulou.com Published: 2022-06-02 12:59:12 10月02日 Update

Practice script

#! / bin/sh

#

# determine whether the file exists

#

Path=/etc/user.conf

If [!-f "$Path"]; then

Touch $Path

Fi

Chmod 644$ Path

# determine whether there are parameters

If [2-ne $#]; then

Echo "USER: sh adduser {- add |-del | search} username"

Exit 1

Fi

Age=$1

User=$2

If [0-lt $(echo "$User" | grep-E "[^ a Mizz User 0-9 User _]" | wc-l)]; then

Echo "The age format is error"

Exit 2

Fi

# find out whether the user exists

FunCheckUser () {

Check=$ (grep-w "$User" $Path | wc-l)

If [$check-eq 0]; then

Return 0

Else

Return 1

Fi

}

# add user

FunAddUser () {

FunCheckUser

If [$?-eq 0]; then

Echo "$User" > > $Path

Else

Echo "The user is having"

Exit 3

Fi

}

FunDelUser () {

FunCheckUser

If [$?-eq 0]; then

Echo "The $User no having"

Else

Sed-ri / ^ $User$/d $Path

Echo "The $User del"

Fi

}

# search whether a user exists

FunSearch () {

FunCheckUser

If [$?-eq 0]; then

Echo "NO user match"

Else

Echo "$User is having"

Fi

}

Case $Age in

-add)

FunAddUser

-del)

FunDelUser

Search)

FunSearch

*)

Echo "USER: sh adduser {- add |-del | search} username"

Esac

Note: grep-w exact match

Tags: Users parameters scripts precision commands files methods practices prompts searches applications Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Apple Shulou Technology Huawei