else unmatched

I'm getting an else unmatched error on the script below..

For info : SYDB is the database name entered as a param on the command line.

#Check the DB name
HBDB=`sql $SYDB <<_END_ | grep '^|' | grep -v dbase | sed 's/|//g' | sed 's/ //g'
set autocommit on;
\p\g
set lockmode session where readlock = nolock;
\p\g
select squeeze(trim(dbase)) as dbase
from syapplic
where applic_id = 'hb';
\p\g
_END_`

if [ -z "$HBDB" ]
then
echo "The HB Database name given is not a valid database!"
exit 1
else
echo "The Hb database name is $HBDB "
echo " "
fi

# Declare variables for output
MAINFOLDER=/server location/sig10
FILEDATE=`date +%Y%m%d`
FILETIME=`date +%H:%M:%S`
OUTPUTFILE="$MAINFOLDER/Sig_Birthdays_10_$FILEDATE.csv"

#check that the comparison tale exists withing the HBDB
TBL_CHECK=`sql $HBDB <<_END_ | grep '^|' | grep -v table_name | sed 's/|//g' | sed 's/ //g'
set autocommit on;
\p\g
set lockmode session where readlock = nolock;
\p\g
select table_name
from iitables
where table_name = 'bh_sig_10';
\p\g
_END_`

echo $TBL_CHECK

if [ "$TBL_CHECK" = "bh_sig_10" ]
then
echo "Comparison table exists"
else
echo "Comparison table does not exist - Script will create table"
sql $HBDB <<_END_ > $MAINFOLDER/Create_comparison_table.txt
set autocommit on;
\p\g
set lockmode session where readlock = nolock;
\p\g
create table bh_sig_10 (
claim_id i4,
title varchar 4,
forename varchar 32,
surname varchar 32,
birth_date date
)
with nojournaling,
noduplicates;
\p\g
_END_
fi
...

I get the else unmatched on that else in bold....any clues?

I may have missed it, but I don't see the "fi" to go with the "if-then-else" statement in question

if/then/else/fi now in bold.

Yep, missed it the first time through.

I cut and paste this into my own UNIX box, and then tried "ksh -n" - and no errors were reported. It should have found a mismatched "else" statement.

Mostly likely cause then is some whitespace that the cut/paste is ignoring. Which means to make sure there are no extraneous characters before or after the "_END_" just before the fi.

I have made that mistake before, putting a space afterwards, and then trying to find it when the IO-redirect didn't work.

Hi, I checked for that but it's still not working.

I commented out that whole if/then/else/fi, as its not really required, and then saved a test version of the script and removed the comments (I know that on the database I'll be running this on, the table does exist), It works OK with that bit commented out, I can't fathom why it's doing this, but I always get;

"sig10-test.sh[63]: syntax error at line 66 : `else' unmatched"

<attachment removed>

Thanks for any help.

The error is your indentation of "_END_".

To use:

xxxx <<_END_

the closing _END_ must be at the far left, ie, immediately following the newline.

OK - I dind't realise that the space before the _END_ would have any effect, this is the first time I've ever used << _END within an else/then, and generally indent them as it makes it clearer. Will know in future. Thanks for the help.

Another mystery solved! But that is all part of a days work at www.unix.com. :slight_smile: