Home > Blockchain >  Shell Script Error - ./check_disk.sh: line 9: [: Use: integer expression expected
Shell Script Error - ./check_disk.sh: line 9: [: Use: integer expression expected

Time:05-24

Trying to get a simple alert when the disk is above 80

#! /usr/bin/bash -x
    alert=80
            
            df -H | awk '{print $5 " " $1}' | while read ouput;
        do
                    usage=$(echo $ouput | awk '{print $1}'| cut -d'%' -f1)
                    file_sys=$(echo $output | awk '{print $2}')
                    echo $file_sys
                    if [ $usage -ge $alert ]
                    then
                            echo "ciritcal for $file_sys"
                    fi
            done

Following is the Ouput with set -x:

      '[' Use -ge 80 ']'
    ./check_disk.sh: line 8: [: Use: integer expression expected

CodePudding user response:

Why don't you complete the task just with awk:

alert=90        # just for example
df -P | awk -v alert="$alert" 'NR>1 {
    print $1
    if ($5 > alert) printf "ciritical for %s (%s)\n", $1, $5
}'

CodePudding user response:

#!/bin/bash

alert=89

while read usage file_sys
do
        usage=${usage//%/}
        echo "# $file_sys"
        if [ $usage -ge $alert ]
        then
                echo "  - ciritcal for $file_sys"
        fi
done < <(df --output="pcent,source"|sed '1d')

Or using only awk

awk -v alert="40" 'NR!=1 && $1>alert {print "ciritical for", $2}' <(df --output="pcent,source") 
  • Related