I am trying to write a shell script which sends the output of df -h in mail with the help of HTML code embedded in the script. The issue is this is printing all six columns. I want only specific sequence (6,2,3,4,5). How do I achieve it? Tried lots of things putting array also ) but still not working.

#! /bin/bash


ALERT_DIR_COUNT=`df -Ph | grep -v "Use%" | sed 's/%//g' | awk '$5 > 70 
{print $1,$2,$3,$4,$5"%",$6;}' | column -t | wc -l`

printf "To: xyz@gmail.com\n"
printf "Subject:$HOST\n :$ALERT_DIR_COUNT\n mounts reached threshold & Logs 
Cleared : $Numberofdays days\n"
printf "Content-Type: text/html\n"

printf "<html>\n
df -Ph| awk '{ print $6,$2,$3,$4,$5 }''
print "<table  border=\"8\" cellpadding=\"3\"  style=\"border-collapse: 
printf "<tr>"
printf "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
printf "</tr>"
printf "<tr>"
printf "<th bgcolor=gray>MOUNT</th>"
printf "<th bgcolor=gray>SIZE</th>"
printf "<th bgcolor=gray>USED</th>"
printf "<th bgcolor=gray>AVAILABLE</th>"
printf "<th bgcolor=gray>USE%</th>"

printf "</tr>"}
for( i = 1; i <= NF; i++ ) {
  printf "%s", "<td bgcolor=azure"
  if (i==5&&$i+0>70) printf " bgcolor=azure"
  print ">" $i "</td>"
print "</tr>"
END { print "</table>" }'
  • 2
    (1) Please don’t say “not working.” What is happening? (2) IIRC, email doesn’t allow multi-line Subject lines. (And it’s probably a good idea to have a space after the colon (:).) (3) The BEGIN block (if any) must be the first thing in an awk program. (4) Please don’t say 'foo''bar' when you mean 'foobar' unless you have a good reason for doing so (and I can’t think of any). (5) You have an unmatched ( in your script (before printf "To: xyz@gmail.com\n"). … (Cont’d) – G-Man Says 'Reinstate Monica' Jun 13 '17 at 22:48
  • 2
    (Cont’d) …  (6) For every line of input, your script first writes fields 6, 2, 3, 4 and 5 (in that order), unadorned, and then prints all the fields (in 1→NF order), wrapped in HTML tags.  That’s probably not what you mean.  (7) Your script prints bgcolor=azure unconditionally (for every cell/value), and then, conditionally, again.  (8) Your script is very complicated.  Simplify it, get something working, and then build on it. – G-Man Says 'Reinstate Monica' Jun 13 '17 at 22:48
  • 1
    aks: Welcome to U&L on SE ! Please do acknowledge at least the answer you got. If it effectively answers your query, do check the green mark to the left of it (below the point accumulator) so others may later benefit from it. PS: G-Man's comments are well worth acknowledging as well but you are sole judge here. – Cbhihe Jun 14 '17 at 16:44

To simplify the body, you could use something like this:

df -Ph | awk -f stat.awk

I've extracted the awk parts into an awk script, but the script could easily be inlined.

Where stat.awk is:

    print "<html><body><table  border=\"8\" cellpadding=\"3\"  style=\"border-collapse: collapse\">"

    print "<tr>"
    print "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
    print "</tr>"
    print "<tr>"
    print "<th bgcolor=gray>MOUNT</th>"
    print "<th bgcolor=gray>SIZE</th>"
    print "<th bgcolor=gray>USED</th>"
    print "<th bgcolor=gray>AVAILABLE</th>"
    print "<th bgcolor=gray>USE%</th>"
    print "</tr>"

NR > 1 {
    if ($5+0 > 70) {
        bgcolor=" bgcolor=azure"
    print "<tr><td>"$6"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td"bgcolor">"$5"</td></tr>"

    print "</table></body></html>"

For me, this produces:

<html><body><table  border="8" cellpadding="3"  style="border-collapse: collapse">
<th bgcolor=turquoise colspan=6>BEFORE_USAGE</th>
<th bgcolor=gray>MOUNT</th>
<th bgcolor=gray>SIZE</th>
<th bgcolor=gray>USED</th>
<th bgcolor=gray>AVAILABLE</th>
<th bgcolor=gray>USE%</th>
<tr><td>/</td><td>465Gi</td><td>402Gi</td><td>62Gi</td><td bgcolor=azure>87%</td></tr>
<tr><td>/dev</td><td>339Ki</td><td>339Ki</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/MobileBackups</td><td>465Gi</td><td>465Gi</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>

Please note, that this solution breaks when your volume names contain spaces.

| improve this answer | |

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.