Shell – Assigning the output of a SQL query to variable

here-documentshell-script

I am connecting to oracle database and firing a query and assigning the output to variable
But when I echo the value of the variable it doesn't get printed correctly.

count=`sqlplus -s $configuser/$configpass@$ORACLE_SID <<END
           set pagesize 0 feedback off verify off heading off echo off;
           select max(cust_id) from cutomers
           exit;
           END`
           echo $count

The query return correct result when fired on database. But "count" variable has incorrect value.

Best Answer

The terminating here-doc word must be the only characters on the line: no indenting allowed. Also, use $() instead of backticks -- they are nestable.

count=$(sqlplus -s $configuser/$configpass@$ORACLE_SID <<END
       set pagesize 0 feedback off verify off heading off echo off;
       select max(cust_id) from cutomers
       exit;
END
)
echo $count

http://www.gnu.org/software/bash/manual/bashref.html#Here-Documents

Related Question