- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Hep with user script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:07 AM
06-30-2008 04:07 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:20 AM
06-30-2008 04:20 AM
Re: Hep with user script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:20 AM
06-30-2008 04:20 AM
Re: Hep with user script
i didn't understand why you just don't edit /etc/passwd and add lines etc.?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:25 AM
06-30-2008 04:25 AM
Re: Hep with user script
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:29 AM
06-30-2008 04:29 AM
Re: Hep with user script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:31 AM
06-30-2008 04:31 AM
Re: Hep with user script
My awk program requires the colons, "-F:".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:33 AM
06-30-2008 04:33 AM
Solutionawk -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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:44 AM
06-30-2008 04:44 AM
Re: Hep with user script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:54 AM
06-30-2008 04:54 AM
Re: Hep with user script
> 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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2008 04:58 AM
06-30-2008 04:58 AM
Re: Hep with user script
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