Shell Scripting-Example _ batch add user script

Source: Internet
Author: User
Tags stdin

User management is one of the tasks of Linux system maintenance, in which simple actions such as adding and deleting users are designed.

Requirements: Add a lot of users at once. In a text file.

Format: In the unit of behavior, each line is a user information. The user name and password are separated by a specific delimiter, but are spaces, commas, tab keys, and so on. Here is a space to distinguish

eg

[email protected] ~]$ cat addusers.txt username001 password001username002 password002username003 password003username004 password004usernmae005 paswword005

Reads the user information in the file by row. You can use for, while, until loops, and so on. Here, for the first time, use a for loop.

[[Email protected] ~ ]$ vim useradd_for01.sh[[email protected] ~ ]$ Cat useradd_for01.sh #!/bin/Bashcount=0 forLINESinch' Cat addusers.txt ' Doecho $LINES let COUNT+=1Done  Echoecho"$ looped $COUNT times" [email protected] ~]$Sh./useradd_for01.sh username001password001username002password002username003password003username004password004usernmae005paswword005. /useradd_for01.sh loopedTenTimes

The above is supposed to be circulating 5 times, why the cycle of 10 times? , the original addusers.txt is separated by a space, while the for loop reads the file, any whitespace character can be read as a delimiter. The following is an example of a while loop:

[[Email protected] ~ ]$ vim useradd_while01.sh[[email protected] ~ ]$ Cat useradd_while01.sh #!/bin/Bashcount=0 whileRead LINES Doecho $LINES let COUNT+=1 Done<Addusers.txtechoecho"$ looped $COUNT times" [email protected] ~]$Sh./useradd_while01.sh username001 password001username002 password002username003 password003username004 password004usernmae005 paswword005./useradd_while01.sh looped5Times

From the results of the script run, the while row-by-line reading does not matter, because while uses a newline character as a token. (Read command)

Processing: The user name and password are separated from each line, the first part of each line is the user name, and the part after the space is the password. Values require a simple split by using the Cut command.

[[Email protected] ~ ]$ vim useradd_while02.sh [[email protected] ~ ]$ Cat useradd_while02.sh #!/bin/Bash while Read LINES DoUSERNAME=`Echo$LINES |Cut-f1-d' '' Userpass=`Echo$LINES |Cut-f2-d' '' echo -N"USERNAME: $USERNAME PASSWORD: $USERPASS" echo  Done<addusers.txt[[email protected] ~]$Sh./useradd_while02.sh username:username001 PASSWORD:password001USERNAME:username002 password:password002username : username003 PASSWORD:password003USERNAME:username004 PASSWORD:password004USERNAME:usernmae005 PASSWORD: paswword005

When you run the passwd command, you need to enter the password manually, which can be observed through the man passwd--stdin this option to use the pipe "|" for the password. Transfer the password.

--stdin
is used to indicate that passwd should read The from standard input, which can be A pipe.

The script is modified as follows: (requires root permission)

 [email protected] rhat]# cat useradd_while03.sh #!/bin/Bash whileRead LINES DoUSERNAME= ' echo $LINES | Cut-f1-d' '' Userpass= ' echo $LINES | Cut-f2-d' '' Useradd $USERNAME echo $USERPASS| passwd--stdin $USERNAME  Done<addusers.txt[[email protected] rhat]# sh./useradd_while03.sh changing password forUser Username001.passwd:all authentication tokens updated successfully.changing password forUser Username002.passwd:all authentication tokens updated successfully. changing password forUser Username003.passwd:all authentication tokens updated successfully. changing password forUser Username004.passwd:all authentication tokens updated successfully. changing password forUser Usernmae005.passwd:all authentication tokens updated successfully.

The above script "can work", but not very perfect, then run one more, you will find that the new users are not. But they re-revised the password. This is dangerous . You should increase the presence of a user's judgment. In addition, all non-shell built-in commands recommend the use of full paths to avoid command not found due to problems with environment variables. Finally, the script body should use as few variables as possible, so you need to define the variables at the beginning of the script.

[  Email protected] rhat]# vim useradd_while04.sh [[email protected] rhat]# cat useradd_while04.sh #!/bin/bashuser_info =/home/rhat/addusers.txtuseradd=/usr/sbin/useraddpasswd=/usr/bin/passwdcut= /bin/cut  whileRead LINES DoUSERNAME= ' echo $LINES | $CUT-f1-d' '' Userpass= ' echo $LINES | $CUT-f2-d' '' $USERADD $USERNAMEif[$?-ne0];then Echo"$USERNAME exists,skip Set Password"    ElseEcho $USERPASS| $PASSWD--stdin $USERNAME Fidone<$USER _info[[email protected] rhat]# sh./useradd_while04.sh Useradd:user'username001'already existsusername001 Exists,skipSetPassworduseradd:user'username002'already existsusername002 Exists,skipSetPassworduseradd:user'username003'already existsusername003 Exists,skipSetPassworduseradd:user'username004'already existsusername004 Exists,skipSetPassworduseradd:user'usernmae005'already existsusernmae005 Exists,skipSetPassword

Back to Top

Shell Scripting-Example _ batch add user script

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.