Operating System - HP-UX
1833491 Members
2789 Online
110052 Solutions
New Discussion

Re: useradd several users in a script ?

 
SOLVED
Go to solution
Sammy_2
Super Advisor

useradd several users in a script ?

Hi,
Have a text file (see example in the bottom of the message).

How do I script it so I don't have to do it manually ?
====================
while `cat userlist`
do
useradd -u $3 -g users -c "$1" -d /home/$2 -m $2
==================
done
( I believe $1 will trip me here so I can't use space as a delimeter, because of first name and last name).
----------------
# cat userlist
Joe Smow jose214 345
Mike Jones mike123 346
San Kumar san233 347
...
...
good judgement comes from experience and experience comes from bad judgement.
4 REPLIES 4
Sammy_2
Super Advisor

Re: useradd several users in a script ?

FYI,
Some of these names have middle initial. And there is more than 3 spaces between the full name(joe Smow) , username id(joe214), and uid(345).
=================
# cat userlist
Joe Smow joe214 345
Mike Jones mike123 346
San Kumar san233 347
Saml L. Jacks samu234 348
Jo S mike joe232 349
good judgement comes from experience and experience comes from bad judgement.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: useradd several users in a script ?

Well, you are in the ballpark. However, you shot yourself in the foot by putting the full name as the first field(s). If I can trust you to ALWAYS use EXACTLY two name fields then this will work:

cat userlist | while read NAME1 NAME2 XLOGIN XUID
do
useradd -u ${XUID} -g users -c "${NAME1} ${NAME2}" -d /home/${XLOGIN} -m ${XLOGIN}
done

The better answer would be to put the comment field (Full Names) LAST in each line because read assigns everything leftover to the last variable listed:


cat userlist | while read XLOGIN XUID NAME
do
useradd -u ${XUID} -g users -c "${NAME}" -d /home/${XLOGIN} -m ${XLOGIN}
done

Rather than rearranging the order of field in your userlist, you could use awk to do the same thing.
If it ain't broke, I can fix that.
Cheryl Griffin
Honored Contributor

Re: useradd several users in a script ?

Sammy_2
Super Advisor

Re: useradd several users in a script ?

Clay,
I like your method of using awk to put the comment line in the end. That works great. Good point!! and Thanks a bunch.I used that and it works.
Cheryl,
I got some good hints (especially Sridhar suggestions) from the link you provided but Clay method gave me exactly what I wanted.
Appreciate it Clay, cheryl.
good judgement comes from experience and experience comes from bad judgement.