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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:

#!/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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.