[SOLVED] Re: useradd fails - script to generate users

Franz Waldmüller waldbauernbub at gmx.at
Mon Jun 15 12:03:58 UTC 2009


Markus Schönhaber schrieb:
> Franz Waldmüller:
> Tip: to see how the command to be executed really looks like, precede it
> with echo, e. g.
> echo useradd --password $PASS -m -d $HOME_BASE/$USER $USER -e 2009-12-31
> or, probably even better, use bash's
> set -x
I used echo on to display the variable but I didn't consider using it on 
the useradd command. Thumbs up for this idea!

-> I just discovered 2 blank lines at the end of the input textfile. 
After deleting them everything seams to be ok.
A detailed problem description would have been helpful (shame on me): I 
already used echo and everything seemed to work fine, but after adding 
my 3 testusers I was prompted two times for a password.

Here is the link to the source script which I modified to my needs with 
your help (this sample didn't encrypt the passwords):
http://forums.devshed.com/scripts-94/shell-script-to-create-user-in-linux-using-text-file-81336.html

Thank you Markus for your help.

Franz

Below is the script which might interest others as well:
Task: add users to a Linux system out of a text file and assign passwords:
I created the textfile with gnumeric, the passwords where created with 
pwgen -N 20 8 (copy and paste to gnumeric)

here is a sample of the text file (userename password groupname):
----
erase1 fee45eghebairiodeeni erasgrp
erase2 phuashuezuire%wiutha erasgrp
erase3 jaehuniew&ahbieghatu erasgrp
-------
Attention don't leave any blank lines at the end of the document

groupadd erasegrp

Here is the script which adds the users to the system
#!/bin/bash -x

# NEW_USERS="/path/to/text_data_file"
NEW_USERS="/path/to/text_data_file.txt"
# Path to the homedirectories of the created users
HOME_BASE="/media/data/home"

cat $NEW_USERS | \
while read USER PASSWORD GROUP
do
PASS=$(mkpasswd $PASSWORD)
# the -e command will disable the created users at the given date
useradd -p $PASS -g $GROUP -e 2009-12-31 -m -d $HOME_BASE/$USER $USER
echo $USER
done




More information about the ubuntu-users mailing list