I wrote this script for logging emails if disk-space is more than 90. Please help me get the output in separate lines. Here is my code:




for line in `df | awk '{print$6, $5, $4, $1} ' `

# get the percent and chop off the %
percent=`echo "$line" | awk -F - '{print$5}' | cut -d % -f 1`
partition=`echo "$line" | awk -F - '{print$1}' | cut -d % -f 1`

# Let's set the limit to 90% when alert should be sent

if [[ $percent -ge $limit ]]; then
    errortext="$errortext $line"

# send an email
if [ -n "$errortext" ]; then
echo "$errortext" | mail -s "NOTIFICATION: Some partitions on almost 
full"         $EMAILS

Don't try to save output in variables, and don't try to iterate over output from commands when you don't need to.


mailto=( abc1@xyz.com abc2@xyz.com )
tmpfile=$( mktemp )

df | awk '0+$5 > 90' >"$tmpfile"

if [ -s "$tmpfile" ]; then
    mail -s 'NOTIFICATION: Some partitions on almost full' "${mailto[@]}" <"$tmpfile"

rm -f "$tmpfile"

This mails the relevant lines of the df output to the addresses listed in the mailto array if there are any lines whose percentages exceeds 90%. The 0+$5 will force awk to interpret the fifth field as a number. The -s test on a file succeeds if the file is not empty. mktemp creates a temporary file and returns its name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.