0

I created a .sh file to monitor 2 file paths and send the disksize to me. It runs and I don't get a mail. And the filesystems are >90%

#!/bin/bash
used=$(df -Ph | grep 'location1' | awk {'print $5'}) 
used1=$(df -Ph | grep '/location2' | awk {'print $5'}) 
max=80% 
if [ ${used%?} -gt ${max%?} ]; then mail -s 'Disk space alert' abc@eee.com;bbb@eee.com << EOF
The Mount Point "location1" on $(hostname) has used $used at $(date);
if [ ${use1%?} -gt ${max%?} ]; then mail -s 'Disk space alert' abc@eee.com; bbb@eee.com << EOF
The Mount Point "location2" on $(hostname) has used $used1 at $(date);

EOF
fi

2 Answers2

0

Thanks All, I have been able to figure it out.

#!/bin/bash
used=$(df -Ph | grep 'location1' |  awk '{print $5}' | sed 's/%//g' ) 

used1=$(df -Ph | grep 'location2' |  awk '{print $5}' | sed 's/%//g' ) 

max=80% 
if [ ${used%?} -gt ${max%?} ]; then 

if [ ${use1%?} -gt ${max%?} ]; then 
mail -s 'Disk space alert' abc@eee.com bbb@eee.com << EOF

The Mount Point 'location2' on $(hostname) has used $used1 at $(date);

EOF
fi
fi
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 04 '21 at 07:31
0

You failed to include the EOF marker so the rest of the code is hidden inside the here document. (The syntax coloring in your question should help you notice.)

As an aside, you want to avoid the useless grep and fix your indentation. I'm also guessing you don't want the second if to only trigger if the first one did.

#!/bin/bash
used=$(df -Ph | awk '/location1/ { print $5}') 
used1=$(df -Ph | awk '/\/location2/ { print $5}') 
max=80%
if [ ${used%?} -gt ${max%?} ]; then
  mail -s 'Disk space alert' abc@eee.com;bbb@eee.com <<__EOF
The Mount Point "location1" on $(hostname) has used $used at $(date);
__EOF
fi
if [ ${use1%?} -gt ${max%?} ]; then
  mail -s 'Disk space alert' abc@eee.com; bbb@eee.com <<__EOF
The Mount Point "location2" on $(hostname) has used $used1 at $(date);
__EOF
fi

A somewhat more idiomatic solution with less code duplication would loop over the parameters.

#!/bin/bash
max=80
for mountpoint in location /location2; do
    used=$(df -Ph "$mountpoint" | awk 'NR>1 { print $5}') 
  if [ ${used%?} -gt $max ]; then
    mail -s 'Disk space alert' abc@eee.com;bbb@eee.com <<____EOF
The Mount Point "$mountpoint" on $(hostname) has used $used at $(date);
____EOF
  fi
done
tripleee
  • 175,061
  • 34
  • 275
  • 318