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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl: simple question on string append the_learner Shell Programming and Scripting 1 05-14-2007 06:09 PM
Why this simple script, is not working ? so_friendly Shell Programming and Scripting 4 07-11-2006 04:05 AM
how to change working directories in perl? megastar Shell Programming and Scripting 1 11-02-2005 03:05 PM
Simple Perl Script to Screen Display Shakey21 Shell Programming and Scripting 1 04-19-2002 12:55 PM
a simple perl gusla UNIX for Dummies Questions & Answers 3 04-17-2002 03:01 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-11-2007
hankooknara hankooknara is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 80
simple perl script not working

why won't below work?
I am trying to see

a)sipfile has username of the system.
b)it will read the sipfile and do a grep function against the /etc/passwd
c)it will save that output to /tmp/result..

but my script is just hanging...

#!/usr/bin/perl -w

open(SIPFILE, "</tmp/sipfile")
or die "Couldn't find the /tmp/sipfile: $! \n";
open(RESULT, ">>/tmp/result");
my $line;

while (<SIPFILE>) {
$line = `grep $_ /etc/passwd`;
print RESULT $line;
}

close(SIPFILE);
close(RESULT);
  #2 (permalink)  
Old 01-11-2007
Perderabo's Avatar
Perderabo Perderabo is online now Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
You need a "chomp". Your $_ still has a /n which will split your shell command into two commands.
  #3 (permalink)  
Old 01-11-2007
hankooknara hankooknara is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 80
I clearly see that /tmp/sipfile has

user1
user2
user3
user4

and I see end of line($) through vi...

but when I run the script, it shows the entire /etc/passwd file in /tmp/result.

what am I doing wrong??

#!/usr/bin/perl -w

open(SIPFILE, "</tmp/sipfile");
open(RESULT, ">>/tmp/result");

while (<SIPFILE>) {
chomp;
$line = `grep \$_ /etc/passwd`;
print RESULT $line;
}

close(SIPFILE);
close(RESULT);
  #4 (permalink)  
Old 01-11-2007
hankooknara hankooknara is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 80
I dont know why but this worked.

#!/usr/bin/perl -w

open(SIPFILE, "</tmp/sipfile");
open(RESULT, ">>/tmp/result");

while (<SIPFILE>) {
chomp;
$line = `grep $_ /etc/passwd`;
print RESULT $line;
}

close(SIPFILE);
close(RESULT);
  #5 (permalink)  
Old 01-11-2007
Perderabo's Avatar
Perderabo Perderabo is online now Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Why on earth did you add that backslash??? Now perl can't see $_ so the shell gets it. And yes it works right when you remove the backslash so perl can process it.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0