0

This is my code

my @usage = `df -hT | grep -e CC -e usr`;
foreach (@usage) {
    if ($_ =~ m/([0-9]{0,3})% \/(.*)/) {
        print "$1 - $2\n";
        if ( ($2 == "CC" && $1 >= 80) || ($2 == "usr" && $1 >= 90) ) {
            print "/$2 parition is at $1% utilization\n";
        }
    }
}

It is a simple script to check if a partitions usage. The problem is in the IF condition.

if ( ($2 == "CC" && $1 >= 80) || ($2 == "usr" && $1 >= 90)

Should translate to IF (partition is CC and if use is more than 80% ) OR (IF partition is usr and its use is more than 90%) then output.

The my CC partition is at 83% and my usr is at 85%. Therefore I should only see

/CC partition is at 83%

But I get

83 - usr
/usr parition is at 85% utilization
83 - CC
/CC parition is at 83% utilization
searcot jabali
  • 438
  • 6
  • 16
  • 4
    `use warnings;` would be helpful. – mpapec Jun 27 '18 at 10:05
  • [Don't parse `df` output.](https://stackoverflow.com/q/6350466#comment7437792_6351257) It's not generally machine-readable. Use a module wrapping the statvfs system call instead, e.g. [Filesys::DfPortable](http://p3rl.org/Filesys::DfPortable). – daxim Jun 27 '18 at 13:14

1 Answers1

3

You need to use eq for comparing strings

if ( ($2 eq "CC" && $1 >= 80) || ($2 eq "usr" && $1 >= 90) ) {
Borodin
  • 126,100
  • 9
  • 70
  • 144