Amal
Amal

Reputation: 91

Linux script shows unnecessary files

I have written following script and it shows some unnecessary files when i'm running it. I just want to execute only the command and receive the alerts only. the script as follows

 df -h | nawk '/backup/ {print $5 " "  $6}' | while read line;
  do
        usep=$(echo  $line | nawk  '{printf "%d", $1}' )    
        partition=$(echo $line | nawk '{print $2}')
        if (( $usep >= 90)); then
                 echo "$partition ($usep%)" | mailx -s "172.27.68.101" [email protected];
                echo  "$partition ($usep%)" | mailx -s "172.27.68.101" [email protected];
        echo  "$partition ($usep%)" | mailx -s "172.27.68.101" [email protected];
        fi
  done

Follwing image shows the output problem

enter image description here

How can i add multiple recipient to this script without opening such directories?

Upvotes: 0

Views: 174

Answers (2)

donaldgavis
donaldgavis

Reputation: 565

Firstly command df -h|grep backup|sed 's/\%//g'|awk '$5 >= 90 {print $5"% "$6}' for having FS(Partition) more than 90% usage. the rest of command to alert trought mail. Then :

df -h|grep backup|sed 's/\%//g'|awk '$5 >= 90 {print $5"% "$6}'|while read USAGE PARTITION do echo "$PARTITION ($USAGE)"|mailx -s "172.27.68.101" "[email protected],[email protected]" done

Upvotes: 0

sys0dm1n
sys0dm1n

Reputation: 879

To paste a multi-line bash code into terminal, add parenthesis around the lines otherwise each line gets run as a separate command as soon as it gets pasted:

(df -h | nawk '/backup/ {print $5 " "  $6}' | while read line; do
    usep=$(echo  "$line" | nawk  '{printf "%d", $1}')
    partition=$(echo $line | nawk '{print $2}')
    if(("$usep" >= 90)) ; then echo "$partition ($usep%)" | mailx -s "172.27.68.101" [email protected];
        echo  "$partition ($usep%)" | mailx -s "172.27.68.101" [email protected];
        echo  "$partition ($usep%)" | mailx -s "172.27.68.101" [email protected];
    fi
done)

Upvotes: 3

Related Questions