SPUG: Minimal Perl talk at UW on Thursday

John W. Krahn krahnj at telus.net
Tue Nov 14 19:08:18 PST 2006

Tim Maher wrote:
> On Tue, Nov 14, 2006 at 02:37:12PM -0800, John W. Krahn wrote:
>>>        $x=getgrgid $gid;
>>>        defined $x and $x =~ /[a-zA-Z]/ and $gid_name=$x;
>>Is there some Unix or Posix specification that requires the /[a-zA-
>>Z]/ test?
>>Or to put it another way, if the test failed would that be proof
>>that the name was invalid?
> Not testing the filename there,

I never said "filename".

> but rather whether getgrgid
> returned a numeric GID (meaning it failed to find the associated user-
> name) or a verbal GID.  Probably should be simply: $x =~ /\D/

$ perl -le'
for my $uid ( 1000 .. 1003 ) {
    $name = getpwuid $uid;
    print qq[$uid: "$name"] if defined $name
1000: "john"
1001: "june"
1002: "123"

$ perl -le'
for my $gid ( 1000 .. 1003 ) {
    $name = getgrgid $gid;
    print qq[$gid: "$name"] if defined $name
1001: "234"

If the value returned from getpwuid/getgrgid is undef then that UID/GID was
not in the file[1] but if it is defined then it is in the file[1] and it has
returned a valid name.

1)  file being either /etc/passwd or /etc/group

Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

More information about the spug-list mailing list