Jump to content
chicosoft

Ajuda com BD + Perl + IF

Recommended Posts

chicosoft

Olá estou a escrever um script para executar uma determinada função caso detecte que existe um host em baixo registado na BD.

Para já ele só vê na BD e apresenta os hosts em baixo:

#!/usr/bin/perl
use strict;
use DBI;
use warnings;
my $username='user';
my $pass='pass';
my $db='alarms';
my $table= ("test");
my $dbh = DBI->connect( "dbi:mysql:$db", $username, $pass, { 'PrintError' => 1, 'RaiseError' => 1 } );

my $rs = $dbh->selectall_arrayref("SELECT host,alert FROM $table where alert='down'");
$dbh->disconnect();
foreach my $myRow (@$rs) {
 print "@$myRow\n";
}

Como faço para ele executar um comando se o host for igual a x ou y?

Share this post


Link to post
Share on other sites
chicosoft

Peso ter achado a solução. está correcto?

foreach my $myRow (@$rs) {
 print "@$myRow\n";

 foreach my $myElem (@$myRow) {
   if($myElem eq A){print "A";}
   elsif($myElem eq B){print "B";}

   print "\n";
   last;
 }
}

sou um pouco fraco em perl :S

Share this post


Link to post
Share on other sites
chicosoft

Não podias fazer simplemente SELECT host FROM $table WHERE alert='down' AND (host = 'A' OR host = 'B')?

como faz ele um comando assim?

Quero que, se aparecer host: A executa um comando X, se for B executa comando Y.

Edited by chicosoft

Share this post


Link to post
Share on other sites
Rui Carlos

Quero que, se aparecer host: A executa um comando X, se for B executa comando Y.

Ok, nesse caso terás mesmo de usar o ciclo. Mas em princípio só precisas de um ciclo.


foreach my $myRow (@$rs) {
 my ($host, $alert) = @$myRow;
 if($host == ...) {...}
 elsif($host == ...) {...}
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.