in reply to Exit values

Instead of exiting in the loop, set some variable, like $nonrunning_zone = 1; Then, after the loop, exit(2) if the variable is true.

Replies are listed 'Best First'.
Re^2: Exit values
by farhan (Novice) on Nov 28, 2008 at 05:40 UTC
    Thanks ysth
    your advise did the trick, now my code looks like

    #!/usr/bin/perl
    use strict;
    my $status;
    my @zones = qx {zoneadm list -ip};
    foreach (@zones) {
    my($id,$name,$state) = split(/:/,$_);
    if($state eq "running") {
    print "OK: zone $name is in running state";
    $status=0;
    }
    if($state ne "running"){
    print "CRITICAL: Zone $name is in $state "; ,br> $status = 2;
    }
    }
    if($status == 2) {
    exit 2;
    }
    if($status == 0) {
    exit 0;
    }

      By the way, putting your code in <c>...</c> tags will simplify things for both you and us.

      Applying ysth's advice, we get:

      #!/usr/bin/perl use strict; use warnings; my $non_running = 0; my @zones = qx{zoneadm list -ip}; foreach (@zones) { my ($id, $name, $state) = split(/:/, $_); if($state eq "running") { print "OK: zone $name is in running state\n"; } else { print "CRITICAL: Zone $name is in $state\n"; $non_running = 1; } } if ($non_running) { exit 2; } else { exit 0; }

      Hopefully, you've learned about else in the process.

      That's not going to work. If you get a non-running zone, it will set it to 2, but if there's a running zone after that, it will be set back to 0. Move the $status=0 to before the loop and it should work. That way, $status will be 0 if all the zones are running, but if any of them are not it will be 2.