send an email between specified timings

Hi

I'm using the below condition to send an email to recipients based on time, but the below condition is sending mail to abcd@gmail.com when the time is 00:00.

what is the best approach here to send mails.
I want to send mail to abcd@gmail.com if the time is in between 9:00 AM to 9:00 PM
and
if the time is between 9:00 PM to 9:00 AM, the email should be sent to efgh@gmail.com
if [[ $((10#$TIME)) -ge 0900 || $TIME -le 2100 ]]; then
                echo $i
                cat $i | /bin/mailx -s "**********alerts**********" abcd@gmail.com;
                rm $i
        elif [[ $TIME -ge 2100 || $((10#$TIME)) -le 0900 ]]; then
                echo $i
                cat $i | /bin/mailx -s "**********alerts**********" efgh@gmail.com;
                rm $i

Open in new window

LVL 1
CharanRAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TintinConnect With a Mentor Commented:

#!/bin/bash
TIME=$(date +%H%M)


if [ $TIME -ge 900 -a $TIME -le 2100 ]
then
   /bin/mailx -s "**********alerts**********" abcd@gmail.com <$i
else
   /bin/mailx -s "**********alerts**********" efgh@gmail.com <$i
fi

rm -f $i

Open in new window

0
 
CharanRAuthor Commented:
I got the below error when I gave "-a".

./alert.sh: line 11: syntax error in conditional expression
./alert.sh: line 11: syntax error near `-a'

After changing it to && , if the time is 09:55, it's giving me a different kind of error

 0955: value too great for base (error token is "0955")
0
 
TintinCommented:
If you got a syntax error, you must have made an error when re-typing the code.

What is the output of

bash -x yourscript
0
 
CharanRAuthor Commented:
I used the below to ignore base errors

TIME=`date '+%H%M'|sed 's/^0*//'`

anyways thanks Tintin
0
 
TintinCommented:
The only way I can see you would get a "value too great for base" error is if you are trying to do some arithmetic operation on the time.  Apart from that, if you are using my code, there should be no reason to strip off leading zeros.

If for some reason, you really do need to strip the leading zero off, there's no need for sed.

TIME=$(date +%k%M)

will do the job.

 
0
All Courses

From novice to tech pro — start learning today.