0

i am able to send mail of simple test in Linux

echo "body" | mail -s "test" xxx@yahoo.com

but below code is not working

#!/bin/bash
VAR1=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
VAR2=$(df  /dev/sda1 | grep /dev/sda1 | awk '{ print $5}' | sed 's/%//g')
VAR3=$(df /dev/mapper/centos-var | grep /dev/mapper/centos-var | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=50
TODAY=$(date)
if [ "$VAR1" -gt "$THRESHOLD" ] ; then
  mail -s 'Disk Space Alert'  rupendra@3ess.in  << EOF
  Date of $TODAY
  Your system  partition remaining free space is critically low.
  / partition used is  $VAR1%
  / usr partition used is  $VAR2%
  / var partition used is $VAR3%
jww
  • 97,681
  • 90
  • 411
  • 885
Rupi Am
  • 29
  • 5
  • Please describe more exactly what you mean by "*is not working*". What happens instead? Any error messages? – Thomas Fritsch Apr 04 '18 at 13:09
  • Please add the full script, and describe the exact error you are experiencing, including the text of the message. Do so by adding it to your question by clicking *Edit* (and don't post it as a comment). Otherwise, there's not enough information to help troubleshoot it. – jww Apr 04 '18 at 13:33
  • Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Apr 04 '18 at 13:34
  • You don't close your if statment with "fi" and the EOF is missing. – Joao Vitorino Apr 04 '18 at 17:50
  • I am not able to receive the mail from the above code . – Rupi Am Apr 05 '18 at 04:53

1 Answers1

-1

What error did you receive?

The following one worked for me,

#!/bin/bash

set -x

VAR1=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
VAR2=$(df /dev/sda1 | grep /dev/sda1 | awk '{ print $5}' | sed 's/%//g')
VAR3=$(df /dev/mapper/centos-var | grep /dev/mapper/centos-var | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=50
TODAY=$(date)

echo "VAR1=$VAR1, THRESHOLD=$THRESHOLD"

if [ "$VAR1" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' abcd@xyz.com << EOF
Date of $TODAY
Your system partition remaining free space is critically low.
/ partition used is $VAR1%
/ usr partition used is $VAR2%
/ var partition used is $VAR3%

EOF

fi
Sachin
  • 805
  • 1
  • 7
  • 7