Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

File created but blank

by diligent (Initiate)
on Jul 24, 2008 at 03:12 UTC ( [id://699756]=perlquestion: print w/replies, xml ) Need Help??

diligent has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, first post here as I learn to use Perl more efficiently! I have a script that pings a given host, and places the results in a log file. After this is done, I want to run another command in the same script taking that log file and cutting out a part using grep & cut and putting the results in a 2nd log file.
my @days = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday +); my @shortdays = qw( Sun Mon Tue Wed Thu Fri Sat ); my @months = qw(January February March April May June July August Sept +ember October November December); my @shortmonths = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) +; my ( $sec, $min, $hr, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time); my $longyr = $year + 1900; my $fixmo = $mon + 1; $LOGFILE = ">/home/***/public_html/packet/logs/$TARGET_HOST.$mday-$ +shortmonths[$mon]-$longyr.log"; open(LOGFILE) || die "Log file will not open.\n"; print LOGFILE "--------- $mday $shortmonths[$mon], $longyr, $hr:$mi +n:$sec ---------\n\n"; print "Content-type: text/html\n\n"; print "<HTML><PRE>\n"; print "<B>Please wait for script to run, or time out</B>\n"; open (IX,"$commands{$PROGRAM} $TARGET_HOST |"); while (<IX>) { chop $_; print "$_\n"; print LOGFILE "$_\n"; } print <<__END_OF_FOOTER__; </PRE> </HTML> __END_OF_FOOTER__ close (IX); $PACKET = ">/home/***/public_html/packet/logs/$TARGET_HOST.PL.$mday +-$shortmonths[$mon]-$longyr.log"; open(PACKET) || die "Packet loss file will not open.\n"; open (PL,"grep 'packet loss' /home/***/public_html/packet/logs/$TAR +GET_HOST.$mday-$shortmonths[$mon]-$longyr.log | cut -d ' ' -f 6 | cut + -d '%' -f1 > /home/***/public_html/packet/logs/$TARGET_HOST.PL.log") +; close (PL);
From a shell, I can easily execute the 2nd command (PL) and it will return the percentage of packet loss in the new log file ($TARGET_HOST.PL.log). However, when I run the script, the new log file is created, however there is nothing in it. Any help is appreciated!

Node content restored by GrandFather

Replies are listed 'Best First'.
Re: File created but blank
by GrandFather (Saint) on Jul 24, 2008 at 04:24 UTC

    Always use strictures (use strict; use warnings; - see The strictures, according to Seuss). ;)

    Oh, and it is considered very impolite to remove the content of your node when your problem has been solved. We would much rather you add an update to the node to describe the solution.


    Perl is environmentally friendly - it saves trees
Re: File created but blank
by pc88mxer (Vicar) on Jul 24, 2008 at 03:23 UTC
    If you checked the return status of your open(PL, ...) call, I bet you would find out that it failed. Since there is no pipe symbol at the end of the file name, you are actually asking perl to read a file with a lot of shell metacharacters in it.

    An easier way is just to use system() to directly invoke the command:

    system("grep 'packet loss' ... > /home/.../$TARGET_HOST.PL.log");
    If you wanted to open a pipe from the command, you would need to terminate it with a pipe symbol, e.g.
    open(PL, "grep ... | ... |") or die "unable to open pipe from grep command: $!"; while (<PL>) { ... } close(PL);
Re: File created but blank
by jethro (Monsignor) on Jul 24, 2008 at 03:21 UTC
    Your use of open() is wrong. LOGFILE and $LOGFILE are completely different things. In your case it should have been
    open(LOGFILE,$LOGFILE)

      Yes, but if open is called with just a FILEHANDLE argument it looks in the (package, not lexical) variable of the same name (in this case $LOGFILE). Not that it's not poor style and this form really should be avoided in new code, but it will work as-is (presuming a suitable use vars qw( $LOGFILE ) / our $LOGFILE or a lack of use strict, of course).

      (And the pedantic best practices based recommendation would be open( my $logfh, ">", $LOGFILE ) or die "Can't open '$LOGFILE': $!\n";. </pedant> :)

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Re: File created but blank
by Lawliet (Curate) on Jul 24, 2008 at 03:22 UTC

    Perhaps it would be better if you posted the original problem with the solution you used to fix it rather than deleting the node. That way others can learn from your mistake.

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
Re: File created but blank
by kabeldag (Hermit) on Jul 24, 2008 at 03:21 UTC
    You are opening for writing incorrectly, looks like it.
    Update: Looks like you have removed the code that didn't work from your original post since you fixed the problem. You should probably re-post it. Unless somebody really wants to delete this thread (not what usually happens).
Re: File created but blank
by cdarke (Prior) on Jul 24, 2008 at 07:54 UTC
    Your 2nd open, when you are attempting to launch four child processes (a shell, grep, and 2 cut programs) has several problems. You probably meant to use system, open can be used to run a child process connected to a pipe, but you are using a shell process to redirect output to a file and not attempting to read the pipe in Perl.
    Second, you are not doing any error checking. But, importantly, the statement is a poor approach. Perl can doing anything that a shell, grep, and cut will do: use Perl rather than calling a bunch of other programs. It is more efficient, easier to debug, and will progress your skills.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://699756]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-16 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found