http://qs321.pair.com?node_id=1214848


in reply to Re: use getpwent to find users and delete them
in thread use getpwent to find users and delete them

Hello sadly it does not work. executing this:
#!/usr/bin/perl use warnings; use strict; my $account = "tata"; my ( $name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell +, $expire ) = getpwent($account); if ( defined $uid ) { print "User $account uid=$uid. OK to delete user!\n"; } else { print "User $account does not exist...\n"; }
gives me errors:

syntax error at ./users_detect.pl line 9, near "($account" Execution of ./users_detect.pl aborted due to compilation errors.

if I do not put the getpwent($account) it compiles but always returns uid=0 whatever the account

Replies are listed 'Best First'.
Re^3: use getpwent to find users and delete them
by Corion (Patriarch) on May 18, 2018 at 13:56 UTC

    I think getpwent expects no parameter. It's unfortunate that it (or Perl) doesn't say so...

    Changing your code to use getpwnam instead works for me:

    my ( $uid ) = getpwnam($account);
Re^3: use getpwent to find users and delete them
by roboticus (Chancellor) on May 18, 2018 at 14:55 UTC

    garcimo:

    Sorry about that, I thought that I tested that, but it's obvious that I didn't. (Generally, I try to test my assumptions before converting a preview into an actual post, but it appears I made a mistake this time!)

    Good thing that Corion++ noticed the error and showed you the appropriate fix.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.