LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 02: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, 02:47 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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, 07: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 07:50 PM.
 
Old 02-21-2007, 07:46 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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



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 05:11 PM
a command/script for replacing a word with another jaakkop Programming 14 12-04-2005 10:21 AM
PERL script OK at command line, not in browser alvo Programming 4 12-19-2004 08: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 09:46 AM
newgrp command within perl/csh script bobsey Programming 1 04-13-2001 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 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
Open Source Consulting | Domain Registration