
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? 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
Categorized:
Shell Script Programming
(Article 4124)
Tagged: Previous: Legal disclaimers in email signatures? Why? Next: What's the Digital Millennium Copyright Act? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 AMOops! Right you are, Mike. All fixed. Posted by: Dave Taylor at August 15, 2005 4:18 AMHow do example. for many users. Posted by: Camp at November 11, 2008 3:02 PMI 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
|