User creation script

Jacques B. jjrboucher at gmail.com
Thu Apr 26 14:42:03 UTC 2007


> not exactly, here a script I have done to had user from a list, you can
> adapt it to fit your needs. This script create users with temporary
> crypted password, add them to the mail server and crate an elementary
> web page... Choose what you want (sorry for comments in French!):
>
<snip> - --
> Fran�ois Patte
> UFR de math�matiques et informatique
> Universit� Ren� Descartes
> http://www.math-info.univ-paris5.fr/~patte

Très beau script.  Je ne l'ai pas regardé de trop près encore, mais je
vois des points que je n'ai pas couverts dans le mien. (english:  Very
nice script.  I haven't looked at it closely yet, but I see points
that I didnèt cover in mine).

In re-reading the original request, I notice that we kind of got
caught up in nice scripts, but really the OP seems to simply want to
be able to do newuser {username} {password}.

In its simplest form it would be:

/usr/sbin/useradd $1 && echo "$2" | passwd --stdin $1 && echo
Successfully added user $1 ||
echo "unable to add $1"

If you are the only user of this script and know the format then you
are ok with that.  But to make it a proper script with error checking
you'd incorporate getops to read the parameters on the command line
(and even allow you to do -u username -p password in whichever order
you'd like).

This would be much better:
------------------------------------------------------------------------------------------------------
#!/bin/bash

USERNAME=""
PASSWORD=""
MISSING_PARM=1 # exit status if parameter missing
HELP_ME=2 # exit status if person used the help switch
TOO_MANY_PARMS=3 # Too many parameters

clear

usage="Usage: $0 -u <username> -p <password>"

if [ $# -gt 4 ]
then
   echo "Too many parameters!"
   echo $usage
   exit $TOO_MANY_PARMS
fi
# checking for any command line options

while getopts "u:p:" opt; do
   case $opt in
	u ) USERNAME=$OPTARG ;;
	p ) PASSWORD=$OPTARG ;;
	h | \? | -help ) echo $usage
		exit $HELP_ME ;;
   esac
done

if [ "$USERNAME" = "" ] || [ "$PASSWORD" = "" ]
then
   echo "You must provide a username and a password."
   echo $usage
   exit $MISSING_PARM
fi

/usr/sbin/useradd $USERNAME && echo "$PASSWORD" | passwd --stdin
$USERNAME && echo Successfully added user $USERNAME ||
echo "Unable to add $USERNAME"

exit 0 # normal exit
--------------------------------------------------------------------------------------------------------

With the above script using getops, you can use either:
./scriptname -u username -p password
or
./scriptname -p password -u username

Keep in mind that scripting can be tricky.  A space here or one
missing there can potentially make a difference between a command
working or not working.  The /usr/sbin command goes on one line up to
th end of: echo "Unable to add $USERNAME".  Depending on your screen
width while reading the e-mail it may not appear as one line.

And notice the various exit codes that you can use to cause another
script to react based on the exit code from this one.

Jacques B.




More information about the users mailing list