nostr:npub1cmgqvz7xr07euwkum3mjghjqcu4d3k2fcyf6g4uwwe5ggnd6fetq0wrzd2 because I feel obliged to rewrite all Bash scripts into Perl:

use v5.36;

use strict;

use warnings;

# cpan Filesys::DfPortable to install or look in your package manager

use Filesys::DfPortable;

my $mountpoint = "/";

my $GB = 1024 ** 3;

my $diskLimit = 8;

my ($diskUsage, $diskTotal, $diskFree);

# I'd use Filesys::DfPortable.

# This actually works for Win95 and above, Linux, *BSD, HP-UX,

# AIX, Solaris, macOS, Iris, Cygwin etc.

my $ref = dfportable($mountpoint, $GB);

$diskUsage = int($ref->{bused});

$diskTotal = int($ref->{blocks});

$diskFree = int($ref->{bavail});

# But if you really *require* parsing df:

my @dfOutput = `df -hk $mountpoint`;

foreach my $line (@dfOutput) {

# Extract the necessary fields using regex.

# I am essentially scanning for the 2nd and 3rd fields.

if ($line =~ /^\S+\s+(\d+)\s+(\d+)/) {

$diskUsage = $1;

$diskTotal = $2;

last;

}

}

$diskFree = $diskTotal - $diskUsage;

if ($diskFree < $diskLimit) {

print "Less than $diskLimit GB left! Only $diskFree GB left!\n";

}

else {

print "There is still $diskFree/$diskTotal GB left. No need to be alarmed as the limit is $diskLimit GB.\n";

my $curl = `curl example.com/endpoint`;

}

Reply to this note

Please Login to reply.

Discussion

No replies yet.