Syntax error near unexpected token `else'

Hi,

I am trying to read the session log through script. But it keeps showing me some error near. I have tried everything. Even tried converting the script using sed command to remove the hidden characters(\r).But nothing seems to be working.Below is the script :

#!/bin/bash
cd /app/Informatica/INFA95/server/infa_shared/SessLogs
grep -A 20 'SESSION LOAD SUMMARY' s_m_SCENARIO_3.log > abc.txt
rm s_m_SCENARIO_3.log
cd..
count=0
cat abc.txt | while read line
do
if[ $count -eq 1 ]then
echo " source records " >> record.txt
echo $line | grep 'Output Rows' >> record.txt
elif[ $count - eq 2 ]then
echo " target records" >> record.txt
echo $line | grep 'Output Rows' >> record.txt
else
echo "nothing"
fi
echo $line | grep -nF 'Instance Name: [SQ_'
if [ $? -eq 0 ]
then
count=1
elif
echo $line | grep 'Instance Name'
count=2
else
count=0
fi
done

Thank You in advance for you help :slight_smile:

Use code tags for code please.

You need spaces on either side of the [ and ] , you can't let them touch other characters. You can't put 'then' on the same line without a ; inbetween. You need a then for elif, too.

if [ ... ]
then
...
elif [ ... ]
then
...
else
...
fi

Thanks for replying Corona688.

I have kept proper spaces between [ and ], when i keep then in the next line it gives me the same error

"syntax error near unexpected token `then'

Lastly i have used a then after elif too, its just not placed in the next line.

---------- Post updated at 02:29 PM ---------- Previous update was at 02:25 PM ----------

Thank you soo much Corona688 it worked

---------- Post updated 05-23-13 at 05:53 AM ---------- Previous update was 05-22-13 at 02:29 PM ----------

Hi ,
My Script is working now but it is showing too many arguments :confused:

count=0
cat abc.txt | while read line
do
if [ $count -eq 1 ]
then
echo " source records " >> record.txt
echo $line | grep 'Output Rows'  >> record.txt
elif [ $count - eq 2 ]
then
echo " target records" >> record.txt
echo $line | grep 'Output Rows' >> record.txt
else
echo "nothing"
fi
echo $line | grep -nF 'Instance Name: [SQ_'
if [ $? -eq 0 ]
then
count=1
elif echo $line | grep 'Instance Name'
then
count=2
else
count=0
fi
done

Thanks,
Gaurav

Change [ $count - eq 2 ] to [ $count -eq 2 ] .

1 Like

Thanks Corona688 :b:

That was Don Cragun this time. :slight_smile:

Ohhh , Thanks Don :slight_smile: