A (surprising) missing Unix tool program

June 13, 2010

Every so often, I run across surprising omissions in the Unix toolchest of small utilities and the like. Today's is a program to print some or all of the various passwd fields for a user, by extended analogy to id (which will print user and group information in useful ways); you would use it to extract data like the shell or the home directory for a particular account.

(Perhaps I should instead call this a lack in the 'GNU Unix toolchest', since everyone else seems to have more or less frozen their idea of what 'Unix' and its toolchest is and so would probably argue that the Unix toolchest has exactly what it's supposed to have and is not missing anything. I disagree.)

The traditional answer to this is to use awk or cut on /etc/passwd to extract what you want. The problem with this is that it has been a very long time indeed since /etc/passwd was sure to have information for all logins on the system. If you are using YP/NIS, LDAP, or any number of other sources of account information, what you want is simply not there in /etc/passwd. On a modern Unix machine, you thus really do want and need something that calls getpwnam() and reports back the requested bits.

(In the process I think it should regularize the information a bit; the classic case would be to report a blank shell field as a shell of /bin/sh. This saves other programs from having to deal with both cases and getting it wrong sooner or later.)

I know that I could easily write this in Perl or Python (and with slightly more work and masochism, in C). That's not the point; I don't just want a program to do this, I want a utility that's generally present on machines (and that will not be yet another local piece of magic for my coworkers to learn). In the absence of such a tool, I just use awk and cut, since that's the 'standard' answer and it still works on our machines.

Written on 13 June 2010.
« iSCSI Enterprise Target and disk write caches (and ZFS)
A thought on feed readers versus the social web »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Sun Jun 13 00:54:24 2010
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.