Perl script assistance; paste word into external command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script assistance; paste word into external command
# 1  
Old 02-21-2007
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.
# 2  
Old 02-21-2007
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";
	  }

# 3  
Old 02-21-2007
cbkihong,

Thanks for the syntax help!

--
-Adam B.
# 4  
Old 02-22-2007
cbkihong,

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

--
-Adam B.
# 5  
Old 02-22-2007
So what is not working?
# 6  
Old 02-22-2007
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.
# 7  
Old 02-22-2007
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");
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assistance on complicated perl script

As a followup to my previous thread, I'm trying to make a complicated perl script that involves storing information from a text file into a hash, and giving the user the ability to change the information present/write the information currently inside the hash to a new file. This is the code I've... (8 Replies)
Discussion started by: Eric1
8 Replies

2. Shell Programming and Scripting

Paste command not working in shell script

Hai , When i use paste command in command prompt its giving expected output but not in the script. Below is the example. $cat file 1 2 3 $cat file1 4 5 6 $paste -d ':' file file1 1:4 2:5 3:6 but when i used the same command in script its giving the output as below : 1 2 3 (3 Replies)
Discussion started by: Subbu123
3 Replies

3. UNIX for Advanced & Expert Users

Perl command to replace word in file...

Hi, I want to search for a specific word in file and replace whole line with new text. e.g. 1) I have file with below lines APP=ABCD 12/12/2012 DB=DDB 01/01/2013 I need perl command which will check for APP=$VAL and replace whole line with APP=$NEWVAL $NEWDT Simlarly need a... (2 Replies)
Discussion started by: mgpatil31
2 Replies

4. Shell Programming and Scripting

Use of the PASTE command in a script file

Hi, When I use the paste command in the terminal window it works fine, but when i try to use it in a bash script file i get errors. I'm not sure how to use the paste command in a bash script file. my paste command looks like this paste <( code1 ) <(code2) thanks (7 Replies)
Discussion started by: eboye
7 Replies

5. Shell Programming and Scripting

Problem in passing date to external function from perl script.

my $sysdate = strftime('%Y-%m-%d', localtime ); biDeriveByDate('Table_Str',$sysdate,\@lIndx,\@lResVals) In a perl script, when I'm trying to pass $sysdate to some external function it's not working since $sysdate is passed as a string mentioned above but my function is expecting a date value... (1 Reply)
Discussion started by: Devesh5683
1 Replies

6. Shell Programming and Scripting

Perl Parse word from command output

Hello, I used the following script to conect to cisco router: #!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Opsware::NAS::Connect; my($host, $port, $user, $pass) = ('localhost','$tc_proxy_telnet_port$','$tc_user_username$','$tc_user_password$'); my $device =... (5 Replies)
Discussion started by: ahmed_zaher
5 Replies

7. Shell Programming and Scripting

Grep Alerting - command or script assistance

Hello! I need some help with grep from various logs we use for monitoring transactions. The logs contain the following information (which is consistent in all of the files): 12:28:33.157 EWY D 1 (tcpip.c:282): tcpip.c: Unable to connect to x.x.x.x on port xxxx. (79) Connection refused ... (1 Reply)
Discussion started by: sbchecko
1 Replies

8. UNIX for Dummies Questions & Answers

Paste a word in the third field position of a file

Hi All, I have a file like this, 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 The above line is a comma separted data file. I want to modify the third field to The final data file should be like 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 ... (1 Reply)
Discussion started by: girish.raos
1 Replies

9. Shell Programming and Scripting

Assistance needed with perl script

Ok, theres a log file containing the below. Lets call the logfile log_fantastic: 2009/03/16 21:42:45 USER: tonnabo - MAC: 0014BF2D385A - STATUS_ID: 30 - STATE: ERROR 2009/03/16 21:42:45 USER: tonnabo - MAC: 001310AC120D - STATUS_ID: 15 - STATE: OK 2009/03/16 21:42:45 USER: tonnabo - MAC:... (5 Replies)
Discussion started by: SkySmart
5 Replies

10. Shell Programming and Scripting

sed search and replace word assistance...

Hi, I am trying to write a shell script designed to take input line by line by line from a file with a word on each line for editing with sed. Example file: 1.ejverything 2.bllown 3.maikling 4.manegement 5.existjing 6.systems My design currently takes input from the user, and... (2 Replies)
Discussion started by: mkfitzwilliams
2 Replies
Login or Register to Ask a Question