LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-21-2007, 03:14 AM   #1
bru
Member
 
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132

Rep: Reputation: 15
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.
 
Old 02-21-2007, 03:47 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
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
 
Old 02-21-2007, 08:22 AM   #3
bru
Member
 
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132

Original Poster
Rep: Reputation: 15
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

Last edited by bru; 02-21-2007 at 08:50 PM.
 
Old 02-21-2007, 08:46 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with perl script using a variable from a unix command. abefroman Programming 1 05-11-2006 06:11 PM
a command/script for replacing a word with another jaakkop Programming 14 12-04-2005 11:21 AM
PERL script OK at command line, not in browser alvo Programming 4 12-19-2004 09:28 AM
Copy Paste of data from word to Yahoo/rediff in opera is limited to few characters sachin_keluskar Linux - Software 0 08-19-2004 10:46 AM
newgrp command within perl/csh script bobsey Programming 1 04-13-2001 11:05 PM


All times are GMT -5. The time now is 05:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration