if/else statement for perl script?

Discussion in 'Perl' started by Stuart H, Nov 3, 2003.

  1. Stuart H

    Stuart H Guest

    Since bash is horrible at date statements i had to convert my bash script
    to perl....now i have a headache.. I had help to convert but now in
    modifying i have messed it up..

    i'm checking a returned value from a sql query and comparing it against
    the date.
    The two possible output types


    [bluemoon@monitor]$ /usr/local/bin/sqsh -Uuser -Ppassword -S sql1 -h -i test.sql
    [bluemoon@monitor]$
    [bluemoon@monitor]$ /usr/local/bin/sqsh -Usql1_replicator -Pkaiisdead -S mtsql1 -h -i test.sql
    0 2003110311:53:42/298


    i am usure as to how to include a if $SQLOutput= null then exit, else...



    #!/usr/bin/perl -w

    use Date::Calc qw(Delta_DHMS);
    use Date::Calc qw(Delta_Days);
    use POSIX qw(strftime);
    require Symbol;

    $SENDMAIL_EXEC = '/usr/sbin/sendmail'; # Path to sendmail(8)
    $BLUEMOON_RECV_EMAIL = '';
    my $bluemoon_sender = "bluemoon_monitor\@domain.com";

    my @today = (strftime("%Y", localtime(time)),strftime("%m", localtime(time)), strftime("%d", localtime(time)),strftime("%H", localtime(time)),strftime("%M", localtime(time)),strftime("%S", localtime(time)));
    my @timestamp;

    my $day;
    my $year;
    my $month;
    my $hour;
    my $minutes;
    my $seconds;
    my $Dd;
    my $Dh;
    my $Dm;
    my $Ds;

    my $SQLOutput;
    my $zero;

    my $bluemoon_body;

    $SQLOutput=`/usr/local/bin/sqsh -Uuser -Ppassword -S sql1 -h -i bluemoon.sql`;

    if ($SQLOutput = null then exit; else < ------- not sure how to do this in
    perl...

    /(.)(.)(........)(....)(..)(..)(..)(.)(..)(.)(..)(....)/) { $zero = $2;
    $year = $4; $month = $5; $day = $6; $hour = $7; $minutes = $9; $seconds = $11; }
    @timestamp = ($year, $month, $day, $hour, $minutes, $seconds);
    ($Dd,$Dh,$Dm,$Ds) = Delta_DHMS(@timestamp,@today);

    if (($Dd > 0 || $Dh > 0 || $Dm > 10) && $zero == 0 ) {
    $bluemoon_body = join('',"Blue Moon Timestamp Check has
    failed!\n\nThe UPLOADED Field = ",$zero,"\nThere has not been an update
    in ", $Dd," days, ", $Dh," hours and ", $Dm," minutes.\n\nPlease restart
    the interface or investigate\n\nBluemoon TimeStamp:
    ",@timestamp,"\nCurrent Timestamp: ",@today);

    my $SEND_MAIL = Symbol->gensym;
    open($SEND_MAIL, "|-") ||
    exec($SENDMAIL_EXEC, '-i', '-t', "-f$bluemoon_sender");
    print $SEND_MAIL <<"EOF";
    Date: @today
    From: $bluemoon_sender
    To: $BLUEMOON_RECV_EMAIL
    Subject: Blue Moon is Down!
    $bluemoon_body
    EOF
    close($SEND_MAIL);

    }

    Thanks for any help
     
    Stuart H, Nov 3, 2003
    #1
    1. Advertising

  2. Stuart H

    Roy Johnson Guest

    First of all, this is no longer a valid public newsgroup. You will
    reach more people at comp.lang.perl.misc

    "Stuart H" <post@to_group.com> wrote in message news:<pan.2003.11.03.17.05.59.595528@to_group.com>...
    > i am usure as to how to include a if $SQLOutput= null then exit, else...


    exit if $SQLOutput eq '';
    would do it, if SQLOutput will really be empty: no newlines,
    whitespace, etc. Otherwise, it might be better to do
    exit if $SQLOutput =~ /^\s*$/;
     
    Roy Johnson, Nov 4, 2003
    #2
    1. Advertising

  3. Roy Johnson wrote:
    > First of all, this is no longer a valid public newsgroup. You will
    > reach more people at comp.lang.perl.misc


    To which group OP posted as well yesterday. A case of multi-posting.
    You answered in clpmisc as well, btw. :)

    To Stuart: Do *not* multi-post!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 4, 2003
    #3
  4. Stuart H

    GIMME Guest

    You've got a working shell script.

    Why not just go with that ?

    Just use Perl to create the date.

    If that's what you really want ....

    For example, if the following is the code to ~/bin/todays_date.pl :

    #!/usr/local/bin/perl

    use POSIX qw(strftime);

    my @today = (strftime("%Y", localtime(time)),strftime("%m",
    localtime(time)), strftime("%d", localtime(time)),strftime("%H",
    localtime(time)),strftime("%M", localtime(time)),strftime("%S",
    localtime(time)));
    print @today[0] . '/' . @today[1] . '/' . @today[2];


    Then get the date into a shell variable using this syntax (note the
    parenthesis) :

    V=$(~/bin/todays_date.pl)


    "Stuart H" <post@to_group.com> wrote in message news:<pan.2003.11.03.17.05.59.595528@to_group.com>...
    > Since bash is horrible at date statements i had to convert my bash script
    > to perl....now i have a headache.. I had help to convert but now in
    > modifying i have messed it up..
     
    GIMME, Nov 18, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,824
  2. Replies:
    25
    Views:
    675
    MonkeeSage
    Oct 1, 2006
  3. Replies:
    21
    Views:
    1,063
    Giannis Papadopoulos
    Aug 2, 2005
  4. kj
    Replies:
    15
    Views:
    568
    Lawrence D'Oliveiro
    May 23, 2009
  5. A
    Replies:
    8
    Views:
    812
    James Kanze
    Aug 28, 2010
Loading...

Share This Page