Bash:パーセントが90より大きいことを確認してください。

Bash:パーセントが90より大きいことを確認してください。

停止/遅延または予期しない競合を防ぐために、メモリ使用量が90を超えると、自分自身に通知を送信しようとします。

質問:syntax error: invalid arithmetic operator (error token is ".5359 < 80 ")

#!/bin/bash

INUSE=$(free | grep Mem | awk '{print $3/$2 * 100.0}')

if (( $INUSE > 90 )); then
    notify-send "Performance Warning" "Your memory usage $INUSE is getting high, if this continues your system may become unstable."
fi

ベストアンサー1

bash浮動小数点をサポートしないシェルを使用する必要がある場合は、いつでも次のものをawk比較できます。

#! /bin/sh -
memory_usage() {
  free |
    awk -v threshold="${1-90}" '
     $1 == "Mem:" {
       percent = $3 * 100 / $2
       printf "%.3g\n", percent
       exit !(percent >= threshold)
     }'
}
if inuse=$(memory_usage 90); then
   notify-send "Performance Warning" "Your memory usage $inuse is getting high, if this continues your system may become unstable."
fi

(bashに関連するものがないので、bashをshに置き換えてください。)

おすすめ記事