Error in if else statements

Hi All,

In below script I'm trying to match one column of a table value "True".If value is True then echo "Condition Satisfied" if not then "Condition not Satisfied" by using if and else statement this will be a expression condition checking the type value for 5 number of times.

#!/bin/bash
# set -xv
echo ' '
MessageDir=/hometype.out
ScriptName=Type_TEST
ScriptOutput=${MessageDir}/${ScriptName}.all.`date +"%y%m%d%H%M%S"`.msg
{
S3=TRUE
z=5
A=1
while [ $A -le $z ]
do
<DBconnection> 
"SELECT TYPE FROM TEST_TYPE" > /hometype.out
m=`sed -n '3,$s/.*| \([^ ]*\) */\1/p' /hometype.out`
if [ $m == $S3 ];
echo "$m=$S3 "
echo "Condition Satisfied"
echo "$(m) --> current value"
rm /hometype.out
exit 12

else

echo ' '
echo `date` 
echo "condition not satisfied"

sleep 120
A=`expr $A + 1`
} >> $ScriptOutput
fi
done
exit 12

but while running this script,I'm getting

test_type: line 23: syntax error near unexpected token `else'
test_type: line 23: `else'

Can any body Please tell what is the thing ..I'm missing here. Thanks in advance.

use below -- then keyword missing

if [ "$m" == "$S3" ]
then

instead

if [ $m == $S3 ]

Hi Pravin,
Thanks for your reply.
now I'm getting this Error.

test_type: line 31: syntax error near unexpected token `}'
test_type: line 31: `} > $ScriptOutput'

Reverse the order of fi and done and } . I.e. take care to close the inner loops/structures first.

It would be a lot easier to see if you indented your code.

1 Like

Little fixed and cleaned. And used more builtin commands.

#!/bin/bash
# set -xv
echo 
MessageDir=/hometype.out
ScriptName=Type_TEST
ScriptOutput=${MessageDir}/${ScriptName}.all.$(date '+%y%m%d%H%M%S').msg

> "$ScriptOutput"

S3=TRUE
z=5
A=1 
while (( A <= z ))
do
    DB_Something > /hometype.out
    m=$( sed -n '3,$s/.*| \([^ ]*\) */\1/p' /hometype.out )

    if [ "$m" = "$S3" ] 
    then
         echo "$m=$S3 "
         echo "Condition Satisfied"
         echo "$m --> current value"
         rm -f /hometype.out 2>/dev/null
         exit 12
    else
         echo 
         date
         echo "condition not satisfied"
         sleep 120
         ((A=A+1))
    fi
done >> "$ScriptOutput"
exit 12
echo > "$ScriptOutput
while [[ $A -lt $z ]]
sleep 120
A=$(($A+1))

Thats all i see atm, hope this helps

---------- Post updated at 17:04 ---------- Previous update was at 16:59 ----------

Oops not quite, there is another thing:

S3=TRUE
....
    m=$( sed -n '3,$s/.*| \([^ ]*\) */\1/p' /hometype.out )
...
    if [ "$m" = "$S3" ] 

If you want to verify the sed command was successfull, then try this:

sed -n '3,$s/.*| \([^ ]*\) */\1/p' /hometype.out 
m=$?

If you want to compare a string from within hometype.out, leave as is, but i doubt sed can limit the output todwards $m to true, as you compare.

hth