home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!decwrl!mips!darwin.sura.net!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!usc!news!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.unix.questions
- Subject: Re: I want a program that prints out base gid's of any user
- Message-ID: <1992Aug25.193028.9727@netlabs.com>
- Date: 25 Aug 92 19:30:28 GMT
- References: <BtIqyp.F5A@news.cso.uiuc.edu>
- Sender: news@netlabs.com
- Organization: NetLabs, Inc.
- Lines: 55
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <BtIqyp.F5A@news.cso.uiuc.edu> bzg52408@uxa.cso.uiuc.edu (Benjamin Z. Goldsteen) writes:
- :
- : I have been trying to write a program that prints out the
- : base gid of a user (given on the command line). I have tried
- : using awk and perl, but I can not get anything decent.
- : Can show me how to do this. One thing I have tried
- : [ gid username ]
- : #! /usr/bin/awk -f
- : BEGIN { FS=":"; FILENAME="/etc/passwd" }
- : ARGV[1] == $1 {print $4}
- :
- : I am not sure if 1 if right for ARGV, but whatever...anyway, it
- : won't read from /etc/passwd - it only reads from stdin. I can;t
- : make it do otherwise.
-
- You can't make a sow's purse out of a silk ear. Or something like that... :-)
-
- : I don't really understand perl...
-
- You don't have to Understand Perl(TM). You only have to know enough
- of it to get the job done.
-
- The way to do this in Perl is to use the standard system routine for it:
-
- #!/usr/bin/perl -l
- print((getpwnam($ARGV[0]))[3], "\n");
-
- or, more readably,
-
- #!/usr/bin/perl
- ($login,$passwd,$uid,$gid) = getpwnam($ARGV[0]);
- print "$gid\n";
-
- This will even work on YP systems, er, that is, NIS system.
-
- Now, if you *wanted* to parse /etc/passwd yourself in Perl, you'd probably
- do it something like this:
-
- #!/usr/bin/perl
- open(PASSWD, "/etc/passwd") || die "Can't open /etc/passwd: $!\n";
- while (<PASSWD)) {
- ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(/:/);
- print "$gid\n" if $login eq $ARGV[0];
- }
-
- But ordinarily you should use the provided system calls. It's probably
- a mistake in the original design of Unix that system calls and shells were
- kept so far apart from each other...
-
- Have you ever read The Origin of Perl in the Breakdown of the Bicameral Unix?
-
- :-)
-
- Larry Wall
- lwall@netlabs.com
-