Hi,

why don't you use Perl's own chdir?

How do you know, that the open() was successful. You should always check for success...

Perl provides its own chdir function. There is no need to use backticks and system commands for this.

Every open() (and other calls) should be checked for success, otherwise you don't know wether it worked or not...

#!/bin/perl -w use strict; ## be strict! always! my $logdir = '/opt/app1/osa/ebcp/5_5_4/log'; my $logfile = 'AppOsaEbcp1.log'; # Changing dir to Log dir; use perl! chdir $logdir or die "chdir $logfile failed: $!\n"; # check open for success!! open my $fh, '<', $logfile or die "$logfile: open failed: $!\n"; my $major=0; my $minor=0; my $critical=0; while( <$fh> ) { chomp; if ( /MAJOR/ ) { $major++; } elsif ( /CRITICAL/ ) { $critical++; } elsif ( /MINOR/ ) { $minor++; } } close $fh or die "$logfile: close failed: $!\n"; # no need for printf if you want to print simple strings print "Count of MAJOR = $major, CRITICAL = $critical, MINOR = $minor\n +";

Update

a shorter version:

#!/bin/perl -w use strict; ## be strict! always! my $logdir = '/opt/app1/osa/ebcp/5_5_4/log'; my $logfile = 'AppOsaEbcp1.log'; # Changing dir to Log dir; use perl! chdir $logdir or die "chdir $logfile failed: $!\n"; # check open for success!! open my $fh, '<', $logfile or die "$logfile: open failed: $!\n"; my %count; while( <$fh> ) { if ( /(MAJOR|MINOR|CRITICAL)/ ) { $count{$1}++; } } close $fh or die "$logfile: close failed: $!\n"; # now with a printf() printf( "Count of MAJOR = %d, CRITICAL = %d, MINOR = $%d\n", @count{qw(MAJOR CRITICAL MINOR)} );

Both code examples are untested!

update: fixed variable mismatch ($line vs. $_)


In reply to Re: FILE reading question.(closed filehandle) issue in script by linuxer
in thread FILE reading question.(closed filehandle) issue in script by ajay.awachar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.