
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? 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
Categorized:
Shell Script Programming
(Article 4264)
Tagged: Previous: How do I figure out my Amazon sales rank? Next: Approvals needed for screenshots? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 PMYou're right! Fortunately, it's darn easy to fix. :-) Posted by: Dave Taylor at May 6, 2007 10:04 PMYou do not need the "for acct ..."-loop if you I have a lot to say, but ...
I do have a comment, now that you mention it!
|
![]()
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!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|