in reply to Re: specific field selection
in thread specific field selection

Thank you both so much. I'm impressed with the helpfulness and promptness of this forum. Below is the end result. Of course the last two printf() statements are kind of silly, I was just using them to make sure the script would work. Both of the printf() calls will be changed to system() in order to send out an email notification. There is no SMART equivelant for cciss drivers in FreeBSD so this will keep an eye on things.
#!/usr/bin/perl use strict; use warnings; my @args; our $F; @args = ("cciss_vol_status /dev/ciss0 > checkstatres.txt"); system(@args); open RESULTS, "checkstatres.txt" or die "can't open datafile: $!\n"; while (<RESULTS>){ my @F=split(/:/, $_); if ( $F[2] =~ /OK./){ printf (" it's okay\n"); } else{ printf (" it's not ok\n"); } }

Replies are listed 'Best First'.
Re^3: specific field selection
by NetWallah (Canon) on Mar 06, 2008 at 16:55 UTC
    In the spirit of helping improve programming skills, please accept these minor criticisms:

    • our $F; is not being used, and is not necessary. What IS being used it my @F...
    • You are separating the declaration and assignment of @args. Proficient perl programmers would combine these as in
      my @args = ("cciss_vol_status /dev/ciss0 > checkstatres.txt");
    • You are not checking to see if your system call was successful. Typical use is :
      system ... or die ....

    Happy programming!

    You could also consider the Net::SMTP (or Mail::SendEasy) modules as an alternative to using system calls to send mail.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

      Thank you NetWallah, your input is appreciated. I've made the changes so hopefully my script now looks closer to what someone that knows what thery're doing would have done ;-) . I do have one new issue. I'm trying to get the script to email. According to what I read in "Programming Perl" and through Google it should work as is shown below. However the $hostname and $address variables in @email are not being seen. If I "hard code" the hostname and email address the script works fine. Anyone have any more suggestions?
      #!/usr/bin/perl use strict; use warnings; $address = 'user@gmail.com'; my @hostname = ("hostname -s"); my @email = ("echo $hostname array issue | mail -s 'Array problem' $ad +dress"); my @args = ("cciss_vol_status /dev/ciss0 > checkstatres.txt"); system(@args); open RESULTS, "checkstatres.txt" or die "can't open datafile: $!\n"; while (<RESULTS>){ my @F=split(/:/, $_); if ( $F[2] =~ /OK./){ ;; } else{ my $hostname = system(@hostname); system(@email); } } my @args2 = ("rm checkstatres.txt"); system(@args2);
        To get the hostname from the operating system, you need the text results(STDOUT) from the execution of the "hostname-s" command. (There are other ways to do this in perl, using modules).

        The system command does NOT return the contents of STDOUT, it merely returns the exit status value.

        To get the OUTPUT, use backticks (``), or the qx command.

        my $hostname = qx[hostname -s]; chomp ($hostname); # Gets rid of trailing CR
        You need to get the $hostname value filled-in BEFORE using it in the @email assignment.

        To make your program a little easier to read, when you are ready for it, lookup the next; statement. If you put a "next" in your "if" statement, you can avoid the "else" , and just place those statements after the closing brace of the "if".

             "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom