bc コマンド後のスクリプトエラー

bc コマンド後のスクリプトエラー

次のスクリプトを実行しようとしています。

#!/bin/bash
USED=`free -m | more | grep -v total | head -1 | cut -d':' -f2 | cut -d' ' -f18`
CACHE=`free -m | more | grep -v Swap | tail -1 | cut -d':' -f2 | cut -d' ' -f9`
TOTAL=`free -m | more | grep -v total | head -1 | cut -d':' -f2 | cut -d' ' -f11`
echo "scale=2 ; ((($USED - $CACHE) /$TOTAL) *100)" | bc

ただし、常に次のエラーが発生します。

(standard_in) 1: parse error

ベストアンサー1

bcパイプを削除し| bcてスクリプトを実行すると、次のように出力されます。

scale=2 ; (((5538 - ) / 5969) * 100)

$CACHE変数が空でbc構文エラーが発生していることがわかります。

あなたは試すことができます:

CACHE=$(free -m | more | grep -v Swap | tail -1 | cut -d':' -f2 | awk '{print $1}')

ノート

  • awkこの場合よりも出力を解析する方が良いですcut
  • あなたは試す必要があります$(...)コマンドの置換に使用されます。

おすすめ記事