Merging two condition with && operator is not working. (199 Views)
Reply
Occasional Contributor
Alokbehria
Posts: 4
Registered: ‎04-03-2013
Message 1 of 4 (199 Views)
Accepted Solution

Merging two condition with && operator is not working.

[ Edited ]

 

Hi All,

 

I am usk ksh shell and trying to code shell script , which would send mail based on som condition. one condition would be something like, if the day is sunday and time is greater then 17:30 then  we should able to recieve mail. Also , if the day is not sunday , it continues to check database status and send across the mail , if the database is down. now, when I tried to run the script , it brough following errors on board.

 

#!/usr/bin/sh
. cpmqa
DATE=`date '+%m%d%y'`
LOG_DIR=/db/cpmqa/users/oracpmqa/monitoring/logs
LOG_FILE=${LOG_DIR}/${DATE}.log
MAIL_LIST="anme@domain.com"
typeset -i mHHMM=`date +%H%M`
typeset -i mDD=$(date +%d)
mDay=`date +%a`
for i in cpmdvfdm cpmqa cpmtest
do
RC=FAILURE
RC=`sqlplus -s <<EOF
oraadmin/cscmon@"$i"
set heading off
set feedback off
set pagesize 0
set trimspool on
select instance_name from v\\$instance;
exit
EOF`
if [[ "${mDay}" = "Wed" && ${mHHMM} -gt 1830 ] || [ "$RC" !=  "$i" ]] then
echo "Instance $i is down" | mailx -s "ALERT: Instance $i shoud be up `date`" $MAIL_LIST
else
[ "$RC" !=  "$i" ] then
echo "Instance $i is down" |  mailx -s "ALERT: Instance $i is down " $MAIL_LIST
print "`date`: the $i is down " >> $LOG_FILE
echo Failure
fi
done

cpmqa> . test5.sh
ksh: syntax error: `]' unexpected

 

 

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,897
Registered: ‎03-06-2006
Message 2 of 4 (187 Views)

Re: Merging two condition with && operator is not working.

[ Edited ]

>ksh: syntax error: `]' unexpected

 

As the message says, you have some bogus "[]" chars in your condition:

if [[ "${mDay}" = "Wed" && ${mHHMM} -gt 1830 ] || [ "$RC" != "$i" ]] then

 

Remove those "]" and "[" around your "||".

You also need a ";" between the "]]" and the "then":

if [[ "${mDay}" = "Wed" && ${mHHMM} -gt 1830 || "$RC" != "$i" ]]; then

 

Did you want this to be elif?

else

[ "$RC" != "$i" ] then

I.e.:

elif [ "$RC" != "$i" ]; then

Please use plain text.
Occasional Contributor
Alokbehria
Posts: 4
Registered: ‎04-03-2013
Message 3 of 4 (183 Views)

Re: Merging two condition with && operator is not working.

 

Great !! You really made my day. All working fine now. Well, I am not that good in shell scripting , so used bit of google and bit extracted from old script. I would also appriciate , if you could explain below lines. would like to say thanks once again.

 

 

typeset -i mHHMM=`date +%H%M`
typeset -i mDD=$(date +%d)

i know, what below lines suppose to do , but what's a point using curly braces
if [[ "${mDay}" = "Sun" && ${mHHMM} -gt 1730 || "$RC" != "$i" ]]; then

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,897
Registered: ‎03-06-2006
Message 4 of 4 (180 Views)

Re: Merging two condition with && operator is not working.

>Great !! You really made my day.

 

if you are happy with your answers, please click on the Kudos stars of each helpful post.

 

>if you could explain below lines.

 

>typeset -i mHHMM=`date +%H%M`

>typeset -i mDD=$(date +%d)

 

The above lines extract the various time/date components into integer variables.  The first one should be converted to use the $() syntax:

typeset -i mHHMM=$(date +%H%M)

 

A standard caution I learned decades ago.  In the above two date(1) commands, what happens if you run it a few microseconds before midnight, so the second one is after?  ;-)

 

>what's a point using curly braces:

if [[ "${mDay}" = "Sun" && ${mHHMM} -gt 1730 || "$RC" != "$i" ]]; then

 

Just to be pedantic but probably better to use them consistently throughout the whole line and script.

(I usually only use them where I need to do so.)

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation