issues with sql inside if statement

Hi,

I have problem with the following code. My IF block is not executed. And I see "syntax error near unexpected token `)'" error for line "EOF" in the stats_function().
but when I comment the IF block I don't see this error.

Kindly help me with this issue.

clean_function()
{
count_clean=$(sqlplus -s username/password@SID<<-EOF
SET HEADING OFF
SET FEEDBACK OFF
SET PAGES 0
select count(*) from tableA;
quit
EOF
)
echo  $count_clean
}
count2_clean=$(clean_function)
echo $count2_clean


if [ $count2_clean -ne 0 ];then
  sqlplus -s $(sqlplus -s username/password@SID<<-EOF
  SET HEADING OFF
  SET FEEDBACK OFF
  SET PAGES 0
  delete from tableA;
  quit
  EOF
fi
######################## RUN THE SCRIPT TO LOAD THE DB ######################
sh dbloadscript

######################## CHECK THE STATUS OF THE DB ###########################
stats_function()
{
count_status=$(sqlplus -s username/password@SID<<-EOF
SET HEADING OFF
SET FEEDBACK OFF
SET PAGES 0
select count(*) from tableA where status=a;
EOF
)
return $count_status
}

which editor you are using. Normally editors will show what you are missing. specially with the ( and { ..:)(i am using Notepad++)

there is no closing bracket for below highlighted.. (

if [ $count2_clean -ne 0 ];then
  sqlplus -s $(sqlplus -s username/password@SID<<-EOF
  SET HEADING OFF
  SET FEEDBACK OFF
  SET PAGES 0
  delete from tableA;
  quit
  EOF
fi

Sorry that was a Typo
actually in the script it is:
sqlplus -s username/password@SID<<-EOF

Your error clear mentions that ) is missing.

yes.. that is why I am confused.... I have checked my actual code in notepad++... the braces are fine.... like I said earlier if i comment IF statement ... I don't have the error... but with the IF statement I get that error....
my IF statement looks like ... the first post had a typo

if [ $count2_clean -ne 0 ];then
 sqlplus -s username/password@SID<<-EOF
 SET HEADING OFF
 SET FEEDBACK OFF
 SET PAGES 0
 delete from tableA;
 quit
 EOF
fi

Does your closing EOF have a tab character in front of it? The dash before the first EOF means ignore leading tabs in front of the closing EOF.
On my system, it did not work if there were spaces in front of the closing EOF.

Thanks Elixir!

try using double quotes or [[

if [[ $count2_clean -ne 0 ]];then

or

if [ "$count2_clean" -ne 0 ];then

Not a necessity if you have <<-EOF instead of <<EOF . In the former case, EOF may be preceded by tabs.

1 Like

hi gary,

Sorry, I did not understand what you said.

can you kindly show it in the above code...

Please see my amended post above.

1 Like

Thanks a lot guys... the issue was due to the tab.