3

I'm simply trying to get the output from a sql statement and store in bash variable. I am getting " unexpected EOF while looking for matching `)' " error. I don't see what i'm doing wrong. Why am I getting this error?

var=$($ORACLE_HOME/bin/sqlplus / as sysdba <<EOF  
select status from v\$instance;
exit;
EOF
)
4
  • This is odd: your command works for me. Oct 24, 2016 at 21:23
  • Check "bash --version" That "<< EOF" part requires Bash version -ge 4.1.
    – Michael D.
    Oct 24, 2016 at 21:28
  • @MichaelD., seems to work for me on version 3.1.17(1)-release, as well as on 4.3.
    – ilkkachu
    Oct 24, 2016 at 21:35
  • @ilkkachu ok. my source was: tldp.org/LDP/abs/html/bashver4.html (Example 37-9. Using a here document to set a variable)
    – Michael D.
    Oct 24, 2016 at 21:46

2 Answers 2

5

Is your script indented like that? the delimiter for the here-doc has to be at the beginning of the line. This works for me:

#!/bin/bash
echo $(cat <<EOF
blah
EOF
)
2
  • 1
    Good catch. Note that if you use <<-EOF instead of <<EOF, any leading tab characters (but not leading spaces!) will be ignored—for the whole "heredoc," not just the delimiter.
    – Wildcard
    Oct 24, 2016 at 22:31
  • it is not indented. Sorry for the confusion. Oct 25, 2016 at 14:09
1

Ikkachu is correct that the code, when not indented, should work fine on any reasonably modern version of Bash. However, I would add that you may find it easier to understand and debug if you use a "here string" instead of a "here doc":

var=$(sqlplus / as sysdba <<< 'select status from v$instance; exit;')

Or, use a standard pipeline:

var=$(echo 'select status from v$instance; exit;' | sqlplus / as sysdba)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .