The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Command assistance kmuir UNIX for Dummies Questions & Answers 0 02-26-2008 07:59 PM
how to move word by word on command line pbsrinivas UNIX for Dummies Questions & Answers 1 11-23-2007 02:17 AM
Assistance with Perl and HTTP bryanthomas Shell Programming and Scripting 7 12-11-2006 05:38 PM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 03:29 AM
paste command mariner UNIX for Advanced & Expert Users 5 03-03-2005 10:42 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-21-2007
bru bru is offline
Registered User
 

Join Date: Mar 2005
Location: Fussa, Japan
Posts: 21
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-21-2007
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,420
Just corrected a few errors in syntax. Not sure what the result will be?

Code:
!#/usr/bin/perl

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

@USERS = (<USRLIST>);

	foreach $NAME (@USERS)
	  {
		exec "su - $NAME;cd;/path/to/script2";
	  }
Reply With Quote
  #3 (permalink)  
Old 02-21-2007
bru bru is offline
Registered User
 

Join Date: Mar 2005
Location: Fussa, Japan
Posts: 21
cbkihong,

Thanks for the syntax help!

--
-Adam B.
Reply With Quote
  #4 (permalink)  
Old 02-21-2007
bru bru is offline
Registered User
 

Join Date: Mar 2005
Location: Fussa, Japan
Posts: 21
cbkihong,

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

--
-Adam B.
Reply With Quote
  #5 (permalink)  
Old 02-22-2007
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,420
So what is not working?
Reply With Quote
  #6 (permalink)  
Old 02-22-2007
bru bru is offline
Registered User
 

Join Date: Mar 2005
Location: Fussa, Japan
Posts: 21
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.
Reply With Quote
  #7 (permalink)  
Old 02-22-2007
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 758
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:

Code:
#!/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");
}
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0