Perl script assistance; paste word into external command

I'm attempting to create a Perl script that will:

Take the contents of the usernames.tmp file
(usernames.tmp is created from an awk one-liner ran against /etc/passwd)
Take one line at a time and pass it to the su command as a users name.

This should go on until there is no more name to process

However there is one tiny problem, this is my first exposure to
Perl scripting! And I have no idea how to do this. The code below
is my full heated attempt at clobbering together code found around
the office.

!#/usr/bin/perl

open (USRLIST, "</tmp/usernames.tmp") || die ("die statement");

defined $USERS = (<USRLIST>);

	foreach $NAME (@$USERS)
	  {
		exec "su - $NAME;cd;/path/to/script2";
	  }

Any SOLID pointers, references to functions/methodologies... would
be a real treat!!

Thanks in advance!

--
-Adam B.

Just corrected a few errors in syntax. Not sure what the result will be?

!#/usr/bin/perl

open (USRLIST, "</tmp/usernames.tmp") || die ("die statement");

@USERS = (<USRLIST>);

	foreach $NAME (@USERS)
	  {
		exec "su - $NAME;cd;/path/to/script2";
	  }

cbkihong,

Thanks for the syntax help!

--
-Adam B.

cbkihong,

The syntax help helped; however I'm still stuck, got any more "Perls" of wisdom?

--
-Adam B.

So what is not working?

Sorry, it might help if I actully state that.

The line : foreach $NAME (@USERS)

Errors out with at syntax error. I'm not sure exctly what, I'll have to wait till I'm at work again to find out; when I do I'll post with the exact error.

--
-Adam B.

bru,

it is likely that you wish to use system function or backquotes rather than exec. consult the documentation of exec function to know what it does.

also, you forgot to close the filehandle.

check if this is what you want:

#!/usr/bin/perl

open (USRLIST, "< /tmp/usernames.tmp")  or  die ("Unable to read file /tmp/usernames.tmp : $?");
my @users = <USRLIST>;
close USRLIST  or  warn "Unable to close file /tmp/usernames.tmp : $?";
foreach my $user (@users) {
    system ("su - $user;cd;/path/to/script2");
}

I dont see any,

chomp($NAME);

being used here.

Any reason ?

I think that is very much necessary.

Closing all the open handles is good one!

i missed the chomp. here is the better script:

#!/usr/bin/perl
use strict;

open (USRLIST, "< /tmp/usernames.tmp")  or  die ("Unable to read file /tmp/usernames.tmp : $!");
my @users = <USRLIST>;
close USRLIST  or  warn "Unable to close file /tmp/usernames.tmp : $!";
foreach my $user (@users) {
    chomp ($user);
    system ("su - $user;cd;/path/to/script2");
}

All,

Thanks for the assistance this far.

I've ran into a snag, unless I "exit" from each user the script just sit there. If I do "exit" script2 finally runs, but not correctly (I think thats something for my end). Any ideas on how to get Perl to su to USER then cd to their $HOME then run script2 with out hiccups?

Thanks in advance!

--
-Adam B.

consider implementing sudo for this purpose