Perl 用于比较的拆分数字和百分号

Perl 用于比较的拆分数字和百分号,perl,Perl,我编写了一个小的perl脚本来检测分区是否由于填充而出现问题 我通过bash shell调用我的脚本,如下所示: chk_part.sh /PARTITION 它工作得很好,但我对以8或9开头的计数器有问题。 e、 g.如果我的分区已满8%,我会得到“临界” 我认为问题在于我使用字符(%)和数字(8)进行比较(lt,gt,eq) 我怎样才能修好它 #!/usr/bin/perl -w use strict; use warnings; use feature qw(switch say);

我编写了一个小的perl脚本来检测分区是否由于填充而出现问题

我通过bash shell调用我的脚本,如下所示:

chk_part.sh /PARTITION
它工作得很好,但我对以8或9开头的计数器有问题。 e、 g.如果我的分区已满8%,我会得到“临界”

我认为问题在于我使用字符(
%
)和数字(8)进行比较(
lt
gt
eq

我怎样才能修好它

#!/usr/bin/perl -w
use strict;
use warnings;
use feature qw(switch say);

my $LZE = shift(@ARGV);

# die "Usage: $0 Partition\n" if @ARGV < 1;

my $used_space = `df -h $LZE |awk 'FNR == 2 {print \$5}'`;

given ($used_space) {
    chomp($used_space);
    when ($used_space lt '75%') { print "OK - $used_space of disk space used by $LZE\n"; exit(0); }
    when ($used_space eq '75%') { print "WARNING - $used_space of disk space used by $LZE\n"; exit(1);  }
    when ($used_space gt '75%') { print "CRITICAL - $used_space of disk space used by $LZE\n"; exit(2); }
    default { print "UNKNOWN - $used_space of disk space used by $LZE\n"; exit(3); }
}
#/usr/bin/perl-w
严格使用;
使用警告;
使用功能qw(开关说);
my$LZE=shift(@ARGV);
#如果@ARGV<1,则为“用法:$0分区\n”;
my$used_space=`df-h$LZE | awk'FNR==2{print\$5}`;
给定($used_space){
chomp(已使用的空间);
当($used_space lt'75%'){打印“确定-$LZE使用的磁盘空间的$used_space\n”;退出(0);}
当($used_space eq'75%'){print“警告-$LZE使用的磁盘空间的$used_space\n”;退出(1);}
当($used_space gt'75%'){print“CRITICAL-$used_space of disk space used by$LZE\n”;退出(2);}
默认值{print“UNKNOWN-$used\n$LZE\n使用的磁盘空间;退出(3);}
}

您需要删除
%
符号。这样做的一个好方法是使用正则表达式匹配和捕获组。同时,这有助于从
df
中查找格式错误的输出

my $res = `df -h $LZE |awk 'FNR == 2 {print \$5}'`;
$res =~ m/(\d+)%/;
my $used_space = $1 or die "Something went wrong with df: $!";

given ($used_space) {
    when ($used_space < 75) { ... }
}
my$res=`df-h$LZE | awk'FNR==2{print\$5}`;
$res=~m/(\d+)%/;
我的$used_space=$1或死亡“df:$!”出现问题;
给定($used_space){
当($used_space<75){…}
}
my$used_space=$1或die“df:$出现问题!”
将匹配值(
$1
是第一个捕获组)分配给
$user\u space
。该操作返回相同的值。如果它不是真值(即
undef
),则低绑定
将触发其右侧命令,程序将停止运行


有关更多信息,请参阅。

注意,
给定/当
是实验性的时,您可能需要重新考虑使用它。不管怎样,我已经做了一些细微的更改,使您的代码能够满足您的需要

它在正则表达式搜索/替换中从
$used_space
中删除
%
,从实际comaprisons中删除
%
符号,并将其添加到打印结果中

my $used_space = `df -h $LZE |awk 'FNR == 2 {print \$5}'`;

$used_space =~ s/%//;

given ($used_space) {
    chomp($used_space);
    when ($used_space < 75) { print "OK - $used_space% of disk space used by $LZE\n"; exit(0); }
    when ($used_space == 75) { print "WARNING - $used_space% of disk space used by $LZE\n"; exit(1);  }
    when ($used_space > 75) { print "CRITICAL - $used_space% of disk space used by $LZE\n"; exit(2); }
    default { print "UNKNOWN - $used_space% of disk space used by $LZE\n"; exit(3); }
}
my$used_space=`df-h$LZE | awk'FNR==2{print\$5}`;
$used_space=~s/%/;
给定($used_space){
chomp(已使用的空间);
当($used_space<75){print“OK-$used_space$LZE使用的磁盘空间的百分比”\n;退出(0)}
当($used_space==75){print“警告-$used_space占$LZE使用的磁盘空间的百分比”\n;退出(1)}
当($used_space>75){print“CRITICAL-$used_space$LZE\n使用的磁盘空间的%;退出(2)}
默认值{print“UNKNOWN-$used_space%$LZE\n使用的磁盘空间%;退出(3);}
}

这里的问题是,
eq
和类似的东西并没有按照你的设想去做。它们是字符串值比较,字母数字比较也是如此

见:

你可以侥幸逃脱,因为按字母顺序比较“两位数”的百分比会得到正确的结果

但是如果按字母顺序排序:
ab
cdefg
之前

所以基本上-不要使用那些操作符。从数字中提取一个数值(
s/%//g
可以),然后与
=
进行比较

#!/usr/bin/env perl
use strict;
use warnings;

my @values = ( '7%', '8%', '9%', '10%', '11%', '75%', '80%', '100%' );

print join "\n", sort { $a cmp $b } @values;
这将为您提供以下顺序:

10%
100%
11%
7%
75%
8%
80%
9%
我很确定这正是你不想要的。(因为“100%满”小于
75%
!)

更改值以丢失百分比,然后使用数字运算符是一种方法

s/%//g for @values;
print join "\n", sort { $a <=> $b } @values;
s/%//g表示@value;
打印联接“\n”,排序{$a$b}@values;

只想指出
awk
在这里是无用的:

my $used_space = `df -h $LZE`;
$used_space =~ m/(\d+)%/;
$used_space = $1;

given ($used_space) {
    when ($used_space <  75) { print "OK - $used_space of disk space used by $LZE\n"; exit(0); }
...
my$used_space=`df-h$LZE`;
$used_space=~m/(\d+)%/;
$used_space=$1;
给定($used_space){
当($used_space<75){print“OK-$LZE使用的磁盘空间的$used_space\n”;退出(0);}
...

或无需对3个外部二进制文件(
bash
+
df
+
awk
)进行不必要的分叉,并解析其输出-使用或,例如:

#!/usr/bin/env perl
use 5.014;
use warnings;
use Filesys::Df;
my $limit = 75;
my @txt = qw(OK WARNING CRITICAL);

for my $fs (@ARGV) {
    my $ref = df($fs);
    unless($ref) { warn "Unknown filesystem $fs"; next }
    my $lev = $ref->{per} < $limit ? 0 : $ref->{per} == $limit ? 1 : 2;
    say "$txt[$lev] - $ref->{per}% of disk space used by $fs";
}

这仍然在进行字符串比较,因此实际上还没有解决问题。已更新,但它可以与字符串比较运算符一起正常工作(至少在5.24.0上)。这与解析输出无关(您已经有了答案)但是我强烈建议将
-P
开关应用到
df
,即
df-hP
。否则,如果文件系统的名称太长,您可能会将
df
的输出分成两行。例如,只需注意一点:
df
()的输出格式错误也可以通过应用
-P
来避免(
df-hP
)。
OK - 66% of disk space used by /
Unknown filesystem /foobar at ./mydf line 10.
OK - 10% of disk space used by /tmp