LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl script assistance; paste word into external command (https://www.linuxquestions.org/questions/programming-9/perl-script-assistance%3B-paste-word-into-external-command-530947/)

bru 02-21-2007 02:14 AM

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.

Code:

!#/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.

bigearsbilly 02-21-2007 02:47 AM

forgive me, but you don't need a combo of perl and awk for this.
you certainly never need awk and perl as perl is pretty much a superset of awk.

Code:

cut -f1 -d:  /etc/passwd |
while read user ;
  do echo su $user
done


bru 02-21-2007 07:22 AM

Billy,

Thanks for the input, however I'm going to keep with using awk to create the file before hand because I've already created the file.

However, your code, I have almost no idea what it is up to.

But I'm guessing...
CODE:
cut -f1 -d: /etc/passwd

is similar to:

CODE:
awk -F: '{print $1}' /etc/passwd > /tmp/usernames.tmp;
(this is not 100% correct, but quite close to what I've used)


This magically defines "user" as something, and then 1/2 magically read the file all the while in a while-loop.

CODE:
while read user ;


And this is a "better" way to do my version of exec?
CODE:
do echo su $user

If I am right then this does almost everything I'm trying to do; but it does leave out the more important part, "magically" running script2. That is unless I would be able to append a

CODE:
cd;/path/to/script2;

to the end of the do echo line.


--
-Adam

bigearsbilly 02-21-2007 07:46 AM

the echo is not important it was an illustration.

you can put anything you like in the loop obviously, even su without an echo

Code:

while read x ;do
    blah $x
    blurg $x
    blah
done



All times are GMT -5. The time now is 01:48 PM.