[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

Help with Perl and removing info from a ypcat

I want to find where each users home account is.. I am doing
a:

      #!/serve/bin/perl -w
        open(USERS, "user_list") || die "could not open file ($!)";
        while(<USERS>) {
            $person="$_";      
            $local = `ypcat passwd | grep $person`;
            print $local;
        }    

I want to remove the exta info that the ypcat gives me.  I tried doing
a $local =~ s/^$person//g; to start widdling away at the output of $person
but it doesn't work.

Any suggestions?

-Allen
0
allenm040397
Asked:
allenm040397
1 Solution
 
trip040397Commented:
The answer is not to use ypcat. Use getpwnam() which returns an array, one element of which is the user's home dir. Exactly which element depends on the password file format. Grepping the passwd file like this is dangerous because, for instance, user pete and petew would both pass the grep for user pete.

You dont say which flavour of unix you are using and it is possible that getpwnam() is not available on your system in which case use:

open(PW, "passwd");
while (<PW>) {
  if (/^$person\:/) {
    @pwent = split (/:/);
  }
}

$home_dir = $pwent[4]

... it may not be item 4, depends on your passwd file format.

steve

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now