CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Opening a file line by line for use in a script

 



fuzioneer
stranger

Jul 19, 2001, 3:54 AM

Post #1 of 3 (438 views)
Opening a file line by line for use in a script Can't Post

I have a problem whereby I have a txt file which contains a list of server names which I wish to read in one at a time and carry out a windows command to see if they have a service running or not.


When I run the script it reads the script in succesfully and prints out the list of hostnames but doesn't seem to execute the sclist command against any of them other than the last one in the list.

Does anyone know why this is occuring. I have inserted the section of the script that does the input and service check below.

Please mail me back at david.burley@intel.com if you have any ideas a I am a complete perl newbie and this is driving me mad :)




$filename="d:\\security\\scripts\\Mcafee\\serverlist.txt";

open FILENAME, $filename or die "Cannot open $filename for read :$!";

while (<FILENAME>) {
system "(sclist \\\\"$_"|qgrep AlertManager)";print $_;
}
close (FILENAME);



fuzioneer
stranger

Jul 19, 2001, 5:17 AM

Post #2 of 3 (435 views)
Re: Opening a file line by line for use in a script [In reply to] Can't Post

Figured it out I needed a chomp($_); command ;)



mhx
Enthusiast / Moderator

Jul 19, 2001, 5:27 AM

Post #3 of 3 (435 views)
Re: Opening a file line by line for use in a script [In reply to] Can't Post

Hi,

first, please don't ask for replies to a private address. If you want to receive replies at your email address, you can check the appropriate checkbox when you write a post.
Your script fails because the lines you read in all have newline sequences at their end, except for the last one. Perl offers the chomp function to get rid of the newlines. Try this:

Code
#!perl -w 
$filename='d:\security\scripts\Mcafee\serverlist.txt';

open FILENAME, $filename or die "Cannot open $filename for read: $!";

while( <FILENAME> ) {
chomp;
system "sclist \\\\$_|qgrep AlertManager";
print "$_\n";
}

close FILENAME;

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives