scope of variable in pipe

后端 未结 6 1482
余生分开走
余生分开走 2020-12-18 10:51

The following shell scrip will check the disk space and change the variable diskfull to 1 if the usage is more than 10% The last echo always shows

6条回答
  •  独厮守ぢ
    2020-12-18 11:13

    you can do it this way with gawk(no need to use grep). for alerts you can send email to root.

    threshold=10
    df -HP | awk -v t="$threshold" -v msg="" 'NR>1 && $5+0 > t{ 
        msg=msg $1" is "$5"\n"
    }END{print msg}' | mail root 
    

    or check whether there is "msg" or not first

    threshold=10
    result=$(df -HP | awk -v t="$threshold" -v msg="" 'NR>1 && $5+0 > t{ 
        msg=msg $1" is "$5"\n"
    }END{print msg}')
    if [ -n "$result" ];then
      echo "Overload"
      echo "$result" | mail root
    
    fi
    

提交回复
热议问题