Simple statement to add and delete users
GuideI am Su Xiaobai, a Chinese cabbage who has just entered the linux World. I have been learning about linux Through "linux should be like this" recently. I saw a question on the Internet a few days ago about how to add or delete shell script commands for common users. I didn't write it myself at the time, but I recorded the question! Later, I learned "linux should be like this" and completed what I needed. Hope you can see it. If there is anything incorrect, hope you can correct your message!Requirement: Edit shell script commands to add and delete common users.
Requirements:
1. To add or delete a common user, you must query whether the common user already exists.
2. Add (delete) to implement Circular inquiryThe Code is as follows:
#! /Bin/bashread-p "do you want to add a user? Yes (0) NO (1) "USERADD
Select Operation Type -- add or delete a user
Add User codeIf [$ USERADD-eq 0] thenC = 1 while [$ C-eq 1] doread-p ", enter the account name: "UNAME # Add username # id $ UNAME &>/dev/null # check whether a user exists # if [$? -Eq 0] thenecho "account already exists! "Else read-p" Enter the account password: "PASSWD # Add password # useradd $ UNAME &>/dev/null # create user # echo" $ PASSWD "| passwd -- stdin $ UNAME &>/dev/null # create user password #
Do not add spaces in "$ PASSWD"; otherwise, the password will contain spaces!
If [$? -Eq 0] then echo "$ UNAME created successfully! "Elseecho" $ UNAME creation failed! "Fifiread-p" do you want to continue adding? Yes (1) No (0) "C # Set the variable" C "to implement Delete Code Execution in a loop # done
Delete user codeElseDEL = 1 while [$ DEL-eq 1] doread-p "Enter the username to delete: "UNAME # username to be deleted # id $ UNAME &>/dev/nullif [$? -Eq 0] # delete a user query # thenuserdel-r $ UNAME # delete a user # echo "$ UNAME deleted successfully! "Elseecho" User $ UNAME not found! "Firead-p" do you want to continue? Yes (1) No (0) "DEL # Set the variable" DEL "to implement Delete Code Execution in a loop # donefi
Address: http://www.linuxprobe.com/user-add-del.html