例如:
#!/usr/bin/perl
...
my $host = $db_conf->{host};
my @cmd = ('date',$host);
system(@cmd);
所以我期待的结果是:
Fri Aug 11 15:41:28 CST 2017 db-test-1
使用db-test-1作为主机名。
这是我的整个代码:
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use DBI;
use Encode;
use IO::File;
use JSON;
use utf8;
BEGIN {
binmode STDERR, ':utf8';
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
}
INIT {
my @databases = </kkcorp/kksecret/db-*/db.json>;
foreach my $filename (@databases) {
my $fh = IO::File->new($filename, 'r') or croak $!;
my @buf = <$fh>;
my $str = join '', @buf;
$fh->close;
my $db_conf = decode_json $str;
my $entry = int(rand(scalar(@{$db_conf})));
$db_conf = $db_conf->[$entry]->{writer}->{params};
my $host = $db_conf->{host};
my $dbname = $db_conf->{dbname};
my $password = $db_conf->{password};
my $username = $db_conf->{username};
my $wsrep_check = `mysql -h $host -u $username -p$password < /authdir/auto_inc_script.sql`;
$hosti
if (index($wsrep_check, 'Value: Synced') != -1) {
my @cmd = ('date ${host}');
system(@cmd);
my @wsrep_check_lines = split /\n/, $wsrep_check;
my @table_name = grep { /Synced/ } @wsrep_check_lines;
for my $line (@table_name){
say STDERR $line;
}
say '';
}
}
}
__END__
所以我想在“if”语句开头之后的两个语句中实现。代码运行正常,但忽略了$ {host}变量而没有显示任何输出。
以下是我的结果:
Fri Aug 11 17:13:19 CST 2017
ok Value: Synced
Fri Aug 11 17:13:20 CST 2017
ok Value: Synced
答案 0 :(得分:0)
system
非常强大,但也非常危险。如果你用rm
提供灾难,很容易发生灾难,如果你在运行rm
之前绝对清楚明确了system
的参数,那就更好了。
因此我总是遵循这种做法:
system
部分以下是命令行中的示例:
perl -e ' $var = "Hello"; $cmd = qq(echo $var); print "The command: $cmd \n";'
接下来,添加系统:
perl -e ' $var = "Hello"; $cmd = qq(echo $var); print "the command: $cmd \n"; system($cmd);'