This article demonstrates how to use a shell script to complete adding a user, first making a decision if the user exists, prompting the user to already exist, or adding a new user.
The sample code is as follows:
1#!/bin/Bash2 3 Grep_user ()4 {5R= 'grep "^$1:"/etc/passwd|WC-l '6 Echo$R7 }8 9 Echo-N"input Username:"Ten One Read USERNAME A -ret=' Grep_user $USERNAME ' - Echo ".... return $RET .... ." the if[$RET-eq1 ] - Then - Echo "$USERNAME exist" - Else + Echo "$USERNAME not exist" - Echo "plz add a new user" + sudoAddUser $USERNAME A fi
I use the Ubuntu12.04 environment, the following steps:
1. Create a new file in the home directory.
2, change the file permissions, chmod 755 username.sh
3. Execute the script./username.sh
I want to add a user named Linux, before adding:
No Linux users.
After executing the script:
Follow the prompts to step through the steps and the end result is:
At the same time can be found under the home of Linux users, if the newly added user already exists, then the hint already exists, resulting in unable to add, the entire process is completed!
Add a user using a shell script