1848527 Members
2086 Online
104032 Solutions
New Discussion

Re: Hep with user script

 
SOLVED
Go to solution
Mark Trux
Regular Advisor

Hep with user script

I have users to move and all I was sent was a passwd file.
I have removed the : from the passwd file and I am trying to write a script to do a useradd.
My OS is HP-UX 11.23. I have appx 50 users.
Here is my script. I am not much good at this.
Any Help?
Thanks
#!/usr/bin/sh
#read all the data in the file /tmp/user
while read data
do
#split the fields up
#first field
login=`echo $login | cut -d' ' -f1`
#second field onwards
passwd=`echo $passwd | cut -d' ' -f2-`
#3rd
uid=`echo $uid | cut -d' ' -f3-`
#4th
gid=`echo $gid | cut -d' ' -f4-`
#5th
comment=`echo $comment | cut -d' ' -f5-`
#6th
home=`echo $home | cut -d' ' -f6-`
#7th
shell=`echo $shell | cut -d' ' -f7-`

useradd -u "$uid" -g "$gid" -d "$home" -s "$shell" -m -c "$comment" "$login"
done < /tmp/user
9 REPLIES 9
Dennis Handly
Acclaimed Contributor

Re: Hep with user script

You shouldn't remove the colons. They help split it up easily using awk:
awk -F: '{print "useradd -u", $3, "-g", $4, "-d", $6, "-s", $7, "-m -c \"" $5 "\"", $1}' /etc/passwd > add_users

If you are happy with the contents of add_users, you can use the shell to execute it:
# sh add_users
Kenan Erdey
Honored Contributor

Re: Hep with user script

Hi,

i didn't understand why you just don't edit /etc/passwd and add lines etc.?

Computers have lots of memory but no imagination
Doug O'Leary
Honored Contributor

Re: Hep with user script

Hey;

Editing the password file on most current UNIX operating systems is generally frowned upon. Still possible, but not really a good idea.

The biggest reason, currently, is that most UNIX OSes have a shadow password file. If you're using HPUX TCB, then the files are stashed under /tcb/files/auth/${initial}/${user}. If you're using the shadow patch or solaris, the password files are under /etc/shadow.

useradd knows about the location of the encrypted passwords and updates the files appropriately. If you edit the passwd file directly, you have to ensure the shadow file gets updated. If you don't, or if you make a mistake, there's a chance no one will be able to log into the box - including root.

There's still times when it makes sense to edit the password file directly - but those times are fairly rare.

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Mark Trux
Regular Advisor

Re: Hep with user script

Thanks Dennis,
I have already removed the colons.
I will test what you sent me using the resulting file.
Each item is seperated by a space now

Kenan,
I dont just add them for a couple of reasons.
1. This will come up again.
2. I want to make sure the home directories are created and the permissions are set (-m)

Thanks to both of you for your input.
I wil post results.
Dennis Handly
Acclaimed Contributor

Re: Hep with user script

>I have already removed the colons.

My awk program requires the colons, "-F:".
Doug O'Leary
Honored Contributor
Solution

Re: Hep with user script

As to the original script question. First, ensure /tmp/user doesn't have any system accounts in it (root, bin, sys, etc). Also, verify that all fields are present:

awk -F: '{print NF}' /tmp/user

should show 7 for all lines of the file.

awk -F: '{print $1, $2, $3, $4, $5, $6, $7}' /tmp/user | \
while read user pwd uid gid gecos home shell
do
useradd -u ${uid} -g ${gid} -c "${gecos}' -d ${home} -m -s ${shell} ${user}
done

Node the quotes around ${gecos}. That field will normally have spaces in it - hence the quotes.

Also note that this command does not supply default passwords for the new accounts.

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Doug O'Leary
Honored Contributor

Re: Hep with user script

Hey;

You probably want to put the quotes back in. As I mentioned earlier, the gecos field will normally have spaces in it. Since you're using spaces as your field separater, there's no way to identify where the gecos field starts or ends.

It's much easier to use some type of field separator, :/|,!, soemthing, when mucking about with password file entries.

Doug O'Leary

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
James R. Ferguson
Acclaimed Contributor

Re: Hep with user script

Hi Mark:

> I have already removed the colons.

Then how do you plan to handle the GECOS field which *can* have embedded spaces?

Removing the colon, which is the principal field delimiter complicates the processing you would need to do.

If you don't like the 'awk' solution, you can do everything with a shell script, like:

# cat dousers
#!/usr/bin/sh
OLDIFS=${IFS}
IFS=":"
echo "#!/usr/bin/sh" > userstoadd
while read NAME PASS UID GID GECOS HOMEDIR THESHELL
do
echo useradd -u ${UID} -g ${GID} -c "\""${GECOS}"\"" -d ${HOMEDIR} -m -s ${THESHELL} ${NAME}
done < /etc/passwd > userstoadd
IFS=${OLDIFS}
exit 0

...When you are satisfied with the output file (named 'userstoadd'), make it executable and run it.

Regards!

...JRF...
Mark Trux
Regular Advisor

Re: Hep with user script

Big Thanks
James, Doug and Dennis.
I did use the awk option.
I went back thru the file and did a global search and replace :.
There were some isssues but I corrected them manually.
Now I know NOT to remove the : and/or keep an original.

Great