10

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.

2
  • 1
    please check whether the indenting matches your original code, now after I fixed the formatting. (If matches, then it's wrong. You can not indent the here-documents' closing delimiter like that.) Commented Aug 1, 2013 at 13:12
  • What is shown in normal mode, and what's the value of count when assigning? Commented Aug 1, 2013 at 13:49

3 Answers 3

13

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

1
  • 2
    You can also use tabs with the <<-END notation. Not spaces, TABS! Commented Aug 1, 2013 at 13:55
1
OUTPUT=$( ${OracleClientHome}/sqlplus -S user/pass@database <<EOF
set pagesize 0 linesize 32767 feedback off verify off heading off echo off
select a1.id  || '|' ||
       a1.stmt_begin_date  || '|' ||
       a1.stmt_end_date  || '|' ||
       a1.status  || '|' ||
       a1.total_recs 
from acct_stmt_file a1
 where a1.actp_cd = 'HSA'
   and a1.stmt_begin_date =trunc(sysdate);
exit;
EOF
)

echo -e "\n${OUTPUT}"
1
  • 1
    Why? I think it would be better if you shared your ideas with the community, too, not only this script snippet. Commented May 26, 2015 at 6:18
-1
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

You have to use the semicolon in right place.

1
  • Also, the END at the end needs to be flush to the left, with no whitespace in front of it. Commented Jan 2, 2020 at 19:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.