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

Select range of users from /etc/password in a script?

I am working on a script where I would like to grab the usernames out of the passwd file for a defined range of UIDs. For example I would like to pull the usernames for all users in /etc/passwd that are in the range of 5000-5999. Can you help?


Dave's Answer:

While this task might initially sound daunting, it's actually quite easy to accomplish by using either Perl or awk. My preference is awk, simply because I know that it's included in every single version of Unix or Linux, however old or obsolete, so let's look at how that'd help us here.

First off, I'm going to assume that the only thing you need is the username itself, not any of the other fields in the password file. I think that's safe!

A typical entry in the /etc/passwd file looks like this:

root:*:0:0:System Administrator:/var/root:/bin/sh

In order, the fields, separated by colons, are: account name, encrypted password (a "*" shows that the password's actually stored elsewhere, as it should be), user ID, group ID, so-called "gecos field" which is the full name of the user, home directory and default login shell. Here you can see that root has a UID of zero and that it uses /bin/sh as the login shell and starts out at /var/root.

There are various tricks we could do in a shell script using cut and similar utilities, but let's make it easy and let awk do all the work.

To start, awk lets you specify the field separator with the -F flag, so awk -F: tells the program that it should consider a colon as the field separator, easily enough! Then you specify the mini-program you want awk to run. To pull out just the third field, for example, add '{ print $3 }'. Combine it all and you have a simple way of pulling out all the UIDs in the password file:

awk -F: '{ print $3 }' /etc/passwd

That's not really useful yet, though, so let's take the next step and give awk a conditional statement to evaluate, line by line, before it determines whether to run the code block subsequent. Let's start by just printing the account names of accounts with a UID > 99:

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

That's easy enough, isn't it?

All that's left is to make the conditional test check for an upper and lower bound simultaneously:

awk -F: '$3 > 99 && $3 < 120 { print $1 }' /etc/passwd

That's the solution. That'll give you a list of userIDs associated with accounts between 99 and 120. I'll let you take the final step of changing these values to 5000 and 5999, respectively.

Good luck with your script!



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

In your first awk example you have a square bracket instead of the curly bracket. It should be:

awk -F: '{print $3}' /etc/passwd

Posted by: Michael Clark at August 15, 2005 3:38 AM

Oops! Right you are, Mike. All fixed.

Posted by: Dave Taylor at August 15, 2005 4:18 AM

How do
list username and group

example.
passwd =
user:x:Uid:106::/home:/bin/ksh
group =
group::106
list =
user group

for many users.

Posted by: Camp at November 11, 2008 3:02 PM

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

Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ 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
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.