Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

Shell script to scrape /etc/passwd data?

I am working on a script where I would like to grab the usernames out of the passwd file and add a emaildomain then ftp it. For example I would like to pull the usernames for all users in /etc/passwd and add @domain.com the ftp it to ftp.domain.com . Can you help?


Dave's Answer:

This is a surprisingly easy scripting task, actually, but there are some nuances worth mentioning: if you just blindly go through the /etc/passwd file, where all accounts are saved, you'll end up with lots of addresses like "postmaster" and "admin" and "ftp" and "uucp" and so on. Probably not what you want.

So the first step isn't just to pull out the account names, but to have some sort of rule-based extraction where you, for example, don't grab anything with a UserID of less than 100. This can be most easily done with the awk command, a highly scriptable and flexible pattern matching utility. Here's how I'd do this first step:

awk -F: '{ if ($3 > 99) { print $1 }}' /etc/passwd

This will pull out all the account names where the ID is greater than 99, which should accomplish the first step of your task. The "-F:" tells awk to use colons as the field separators, then "$1" is the first field (account name) and "$3" is the account ID.

Now you have a stream of account names, guaranteed not to have spaces, so this can easily be produced in a subshell and then fed into a looping structure, thusly:

for acct in $(awk -F: '{ if ($3 > 88) { print $1 }}' /etc/passwd)
do
  echo "$acct@hostname.com"
done

This four line Bourne shell script will do exactly what you seek. Change "hostname.com" to be the desired domain name, and run this script on your Linux or Unix box, and the output will be a stream of email addresses as per your specification.

I hope this helps you out! Also, I have a lot more about shell script programming in my acclaimed book Wicked Cool Shell Scripts, if this whets your appetite!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Dave-

I think you have a typo in your full script.

Instead of:

awk -F: '{ if ($3 > 99) { print $1 }}' /etc/passwd

you have

awk -F: '{ if ($3 > 88) { print $1 }}' /etc/passwd

That changes the meaning a bit, so I am pretty sure it is a mistake.

Posted by: Jim at May 4, 2007 11:15 PM

You're right! Fortunately, it's darn easy to fix. :-)

Posted by: Dave Taylor at May 6, 2007 10:04 PM

You do not need the "for acct ..."-loop if you
extend the awk-command with "@hostname.com" to:
awk -F: '{ if ($3 > 99) { print $1 "@hostname.com" }}' /etc/passwd

Posted by: harald eck at December 31, 2007 8:30 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 2000+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.