Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

Bug in shell script using awk to read /etc/passwd?

In your book Wicked Cool Shell Scripts, on p. 115, Script #40 Reporting Disk Hogs, on the 8th line the second part of the line reads:

    awk -F: '$2 > 99 {print $1} ')

I am very new to shell scripting, so I am probably just not understanding how this line should be interpreted. In my Linux class, I have been learning that in the /etc/passwed file the 1st field is username, the 2nd field is password (x), and the third field is the UID, so wouldn't the $2 above really be $3 so that the script would be checking to see if the UID field, which is field 3 is greater that 99, not the password field,which is field 2? Please let me know, since this is confusing to me. Thanks for your help.


Dave's Answer:

You're right. But you haven't noticed that this is part of a pipe in that particular script:

for name in $(cut -d: -f1,3 /etc/passwd | awk -F: '$2 > 99 { print $1 }')

The cut just extracts the name and uid from the password file, at which point the second field *is* the UID, and this way we can use awk to only print the username of accounts with uid's greater than 99.

Remember, here's a typical few lines from the Linux / Unix /etc/passwd file:

coloradoport:*:1041:1041:coloradoportraits:/home/coloradoport:/sbin/nologin
shining-light:*:1042:1042:shining-light-books:/home/shining-light:/sbin/nologin
startup101:*:1043:1043:startup101:/home/startup101:/sbin/nologin

Of course, looking at it now, I don't know why I didn't just use:

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

But that's another story. :-)



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

Subscribe!

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

Comments

alert('XSS BUG Ditemukan..')

Posted by: coba at November 1, 2007 5:08 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.









Search
Find just the answers you seek from among our 1700+ 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 - 2008 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]