home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / question / 10366 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  2.2 KB

  1. Path: sparky!uunet!decwrl!mips!darwin.sura.net!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: I want a program that prints out base gid's of any user
  5. Message-ID: <1992Aug25.193028.9727@netlabs.com>
  6. Date: 25 Aug 92 19:30:28 GMT
  7. References: <BtIqyp.F5A@news.cso.uiuc.edu>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 55
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <BtIqyp.F5A@news.cso.uiuc.edu> bzg52408@uxa.cso.uiuc.edu (Benjamin Z. Goldsteen) writes:
  14. :    I have been trying to write a program that prints out the
  15. : base gid of a user (given on the command line).  I have tried
  16. : using awk and perl, but I can not get anything decent.
  17. : Can show me how to do this.  One thing I have tried
  18. : [ gid username ]
  19. : #! /usr/bin/awk -f
  20. : BEGIN { FS=":"; FILENAME="/etc/passwd" }
  21. : ARGV[1] == $1 {print $4}
  22. : I am not sure if 1 if right for ARGV, but whatever...anyway, it 
  23. : won't read from /etc/passwd - it only reads from stdin.  I can;t
  24. : make it do otherwise.
  25.  
  26. You can't make a sow's purse out of a silk ear.  Or something like that... :-)
  27.  
  28. : I don't really understand perl...
  29.  
  30. You don't have to Understand Perl(TM).  You only have to know enough
  31. of it to get the job done.
  32.  
  33. The way to do this in Perl is to use the standard system routine for it:
  34.  
  35.     #!/usr/bin/perl -l
  36.     print((getpwnam($ARGV[0]))[3], "\n");
  37.  
  38. or, more readably,
  39.  
  40.     #!/usr/bin/perl
  41.     ($login,$passwd,$uid,$gid) = getpwnam($ARGV[0]);
  42.     print "$gid\n";
  43.  
  44. This will even work on YP systems, er, that is, NIS system.
  45.  
  46. Now, if you *wanted* to parse /etc/passwd yourself in Perl, you'd probably
  47. do it something like this:
  48.  
  49.     #!/usr/bin/perl
  50.     open(PASSWD, "/etc/passwd") || die "Can't open /etc/passwd: $!\n";
  51.     while (<PASSWD)) {
  52.     ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(/:/);
  53.     print "$gid\n" if $login eq $ARGV[0];
  54.     }
  55.  
  56. But ordinarily you should use the provided system calls.  It's probably
  57. a mistake in the original design of Unix that system calls and shells were
  58. kept so far apart from each other...
  59.  
  60. Have you ever read The Origin of Perl in the Breakdown of the Bicameral Unix?
  61.  
  62. :-)
  63.  
  64. Larry Wall
  65. lwall@netlabs.com
  66.