1846190 Members
2601 Online
110254 Solutions
New Discussion

Re: useradd script

 
Jeff Zabel
Occasional Advisor

useradd script

I found a script on one of the forums and have modified it to use a custom skel directoy and groups. What I would like to do is insert the persons full name and location into the ect/passwd file. I'm not sure how to do this and any help would be greatly appreciated. Also if I could insert a generic encrypted password in the /etc/passwd file. Please find a below the script I have so far. Thank you in advanced.

#!/usr/bin/sh
Usage() {
echo "Usage: $1 "
exit
}
if test $# -ne 1
then
Usage $0
else
uid=`cut -d":" -f 3 /etc/passwd | sort +n | uniq | tail -1`
uid=`expr $uid + 1`
user=`echo $1 | tr -s '[A-Z]' '[a-z]'`
echo "The user $1 is given user id $uid."
echo "Ok to proceed (Y/n) ? :\c"
read choice
if test "$choice" = "Y" -o "$choice" = "y"
then
/usr/sbin/useradd -u $uid -g lawson -d /home/$user -m -k /etc/lawskel $user
case $? in
0) echo "User is successfully created.";;
9) echo "User id is not unique.";;
16) echo "Cannot add user into /etc/passwd.";;
esac

fi

fi
1 REPLY 1
Ted Ellis_2
Honored Contributor

Re: useradd script

add a little piece to your script that creates a text comment saved as a variable:

looks like you have a single argument that is run with your script at a command prompt.... then it chugs on through and creates your new user. Would it be a huge problem to add a second, third and fourth argument that is the Full name + location.... then modify the useradd as follows:

/usr/sbin/useradd -u $uid -g lawson -d /home/$user -m -c "$2 $3 located in $4" -k /etc/lawskel $user

sure there are more ways to skin this cat

Ted