shell backup.sh:第12行:[:用途:需要整数表达式

xienkqul  于 2023-02-24  发布在  Shell
关注(0)|答案(2)|浏览(201)

我有一个名为backup.sh的脚本:

#!/bin/bash

alert=90
backup_date=$(date +'%m/%d/%y %H:%M:%S')
df -H | awk '{print $5 " " $1}' | while read output;

do
        #echo "Disk Detail: $output"
        usage=$(echo $output | awk '{print $1}' | cut -d'%' -f1)
        file_sys=$(echo $output | awk '{print $2}')
        #echo $usage
        if [ "$usage" -ge "$alert" ]        # <--- ERROR HERE!
        then
                echo "CRITICAL FOR $file_sys"
        fi
done

当我运行它时,我得到这个错误:

backup.sh: line 12: [: Use: integer expression expected

(其中第12行是我用# <--- ERROR HERE!标记的行)。
我做错了什么?我该怎么补救?

wswtfjt7

wswtfjt71#

那么这个独特的awk呢?

max=90
df -h |
    awk -vmax=$max '{
        sub(/%/, "", $5)
        if (NR > 1 && $5 > max) {print "critical " $1 " " $5"% " $NF}
    }'

输出

critical /dev/nvme0n1p2 99% /
nfeuvbwi

nfeuvbwi2#

试试这个

#!/bin/bash

alert=90
backup_date=$(date +'%m/%d/%y %H:%M:%S')
df -H | awk '{print $5 " " $1}' | while read output;

do
        #echo "Disk Detail: $output"
        usage=$(echo $output | awk '{print $1}' | cut -d'%' -f1)
        file_sys=$(echo $output | awk '{print $2}')
        #echo $usage
        if [ $usage > $alert ]        # <--- ERROR HERE!
        then
                echo "CRITICAL FOR $file_sys"
        fi
done

相关问题