Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Merging two condition with && operator is not working.

SOLVED
Go to Solution
Occasional Contributor

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

 

 

 

1 ACCEPTED SOLUTION
Acclaimed Contributor Acclaimed Contributor

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.)

3 REPLIES
Acclaimed Contributor Acclaimed Contributor

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

Occasional Contributor

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

 

Acclaimed Contributor Acclaimed Contributor

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.)