simple perl script not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple perl script not working
# 1  
Old 01-11-2007
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  
Old 01-11-2007
You need a "chomp". Your $_ still has a /n which will split your shell command into two commands.
# 3  
Old 01-11-2007
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  
Old 01-11-2007
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  
Old 01-11-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple sftp script not working - Please help

I have the below sftp script to transfer a file from a linux host(source) to another linux host(target). Public key is already set up in target host and I am able to transfer file using sftp from source to target. But not sure why the below script(ftp_script) is not working. Any help in this... (3 Replies)
Discussion started by: Armaan
3 Replies

2. Shell Programming and Scripting

[Solved] Simple script not working- for loop

Hi all, Please guide me writing this script Follwing is the file which I have created, which contains the files to be copied. cat system1-dr.txt /etc/passwd /etc/shadow /etc/group /etc/vfstab /etc/profile /etc/default/init /etc/shells /etc/dfs/dfstab /etc/dfs/sharetab... (11 Replies)
Discussion started by: manalisharmabe
11 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. Shell Programming and Scripting

Hopefully a simple script, bash or perl...

I'm attempting to parse a file whose contents follow this format; 4:/eula.1028.txt: 8:/eula.1031.txt: 19:/eula.1033.txt: 23:/eula.1036.txt: 27:/eula.1040.txt: 31:/eula.1041.txt: 35:/eula.1042.txt: 39:/eula.2052.txt: 43:/eula.3082.txt: The number of lines of the file... (4 Replies)
Discussion started by: CudaPrime
4 Replies

5. Shell Programming and Scripting

simple script to alert if internet not working?

Hi, I am constantly automaticaly downloading a few things on the internet but since my internet connection is unstable, it sometimes wont work. Thing is the internet will appear to be connected, but no website can be accessed and no program can successfully connect to any location. I can fix... (4 Replies)
Discussion started by: fuzzylogic25
4 Replies

6. Homework & Coursework Questions

Help with Simple Perl/Python Script

I have the following problem, which I need done in Perl/ or Python using Unix/linux filters... 1. You have a very large file, named 'ColCheckMe', tab-delmited, that you are asked to process. You are told that each line in 'ColCheckMe' has 7 columns, and that the values... (1 Reply)
Discussion started by: Swapnilsagarwal
1 Replies

7. UNIX for Dummies Questions & Answers

Not able to run the simple perl script

Hi Experts, I have written simple perl script, which assign the value to variable and print it. Following is the script: $ cat 3.pl #!/usr/bin/env ksh #!/usr/bin/perl print "Hello World"; $iputlne = 34; print $iputlne; The error output is: $ /usr/bin/env perl 3.pl Hello World... (9 Replies)
Discussion started by: Amey Joshi
9 Replies

8. Shell Programming and Scripting

simple cgi script not working

hi all, i have installed simple cgi-script under apache/cgi-bin directory hello.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print <<END_HTML; <html> <head></head> <body>Hello, World!</body> </html> END_HTML when i hit the url... (6 Replies)
Discussion started by: raghur77
6 Replies

9. Shell Programming and Scripting

Simple BASH script not working?

So I need a script that does the following: If a certain user is logged in Run `command` Else Echo “incorrect user” This is my first stab...which doesn't work: #!/bin/bash X="user=`ls -l /dev/console | cut -d " " -f 4`" Y="foobar" echo $X echo $Y (4 Replies)
Discussion started by: doubleminus
4 Replies

10. Shell Programming and Scripting

Why this simple script, is not working ?

Hi everybody I want to create 20 file using simple script - listed bellow-. But the script doesn't work. I hope anyone guide me to correct this script ---------------- The script integer number=01 until (($number==21)) do >TELE-LOG-$number number=$number+01 echo $number done exit... (4 Replies)
Discussion started by: so_friendly
4 Replies
Login or Register to Ask a Question

Featured Tech Videos