Bash 予想される整数式の取得

Bash 予想される整数式の取得

ディスク使用量を確認するための次のスクリプトがあります

    #!/bin/bash

# set alert level 90% is default
ALERT=10

OIFS=$IFS
IFS=','

storage=$(df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }')



for output in $storage ;

do
  echo "---------------@@@@@@@@@ output started @@@@@@@@@@@@@@@@-----------"
  echo $output
  echo "---------------@@@@@@@@@ output end @@@@@@@@@@@@@@@@-----------"

  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  echo "---------------###### useo started ######-----------"
  echo $usep
  echo "---------------###### usep end ######-----------"

  if [ $usep -ge $ALERT ]; then

    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" 
  fi
done

しかし、このコードを実行すると、if条件文で整数式の予測エラーが発生し、これがこのスクリプトの出力です。

  97% /dev/sda1
1% udev
0% none
2% none
---------------@@@@@@@@@ output end @@@@@@@@@@@@@@@@-----------
---------------###### useo started ######-----------
97
1
0
2
---------------###### usep end ######-----------
./fordiskfor.sh: line 24: [: 97
1
0
2: integer expression expected

ベストアンサー1

問題はそこにあります:

if [ $usep -ge $ALERT ]; then
  ...
fi

$usep複数行の数字が含まれています。すべての項目を繰り返すには、その部分の代わりに次のものを使用します。

for $space in $usep;
do
  if [ $space -ge $ALERT ]; then
    echo "Running out of space..."
  fi
done

おすすめ記事