0

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:

#!/bin/bash

errortext=""

EMAILS="[email protected]"


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

# 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
limit=90

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

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

1 Answer 1

2

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

#!/bin/bash

mailto=( [email protected] [email protected] )
tmpfile=$( mktemp )

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

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

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.

1
  • 1
    @AnkkurSingh If this solves your issue, please consider accepting the answer. Commented May 11, 2018 at 12:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.