読みながらメールでループを送信する

読みながらメールでループを送信する

私が書いているスクリプトに問題があります。使用可能なディスク容量を監視し、特定のしきい値を超える各ファイルシステムに対して電子メールを生成しようとしています。 「mail」の代わりに「echo」と一緒にこのスクリプトを使用すると、端末で出力が正しく表示されます。メールをマージすると、重要な警告のみが送信され、電子メール本文には追加のファイルシステムが含まれます。各ファイルシステムに対して別々の電子メールを送信しようとしています。私は答えを探しているわけではありませんが、おそらく私の問題が何であるかを調べるために見てみる価値がある場所を探しています。

 #!/bin/bash
    #The following script will check filesystems (local/nfs only)
    #and notify via email current user if over certain threshold.

    #Thresholds for script to call
    CRITICAL=90
    ALERT=70

    #Gets local/nfs disk info, greps out header/tmp filesystems and awks column 1 and 5
    #while read loop is enabled to parse through each line
    df -l | grep -vE '(^Filesystem)' | awk '{ print ($5  " " $1)}' | while read output;
    do
        #updates variables to reads current step
        usage=$( echo $output | awk '{print $1}' | cut -d'%' -f1 )
        part=$( echo $output | awk '{print $2}' )

        #if percentage matches alert or critical, email current user
        if [ $usage -ge $CRITICAL ]; then
            mail -s "Critical Warning: Filesystem $part is at $usage% of capacity." $USER
        elif [ $usage -ge $ALERT ]; then
            mail -s "Warning: Filesystem $part is at $usage% of capacity." $USER
        fi
    done

ベストアンサー1

コマンドはmailそのメッセージがSTDINにあると予想するため、df ... awkパイプによって生成された残りの出力を読み取ります。

メッセージにメッセージ本文を含めたくない場合は、STDINをパイプするだけです/dev/null

mail -s "Critical Warning: Filesystem $part is at $usage% of capacity." $USER </dev/null

おすすめ記事