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!!
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.
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");
}
#!/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");
}
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?