10 Replies Latest reply: May 28, 2010 11:47 PM by Dude! RSS

    Bash - here-document at line delimited by end-of-file

    Dude!
      Hi,

      I receive the following error when running a simple shell script:

      ./test: line 14: warning: here-document at line 8 delimited by end-of-file (wanted `EOF')

      It works fine in KSH and SH, but not in Bash under Ubuntu 10.04, and I had a similar problems in the past with bash 2.05 under HP Tru64 unix.

      The work-around is to remove the spaces/ident from the lines between << EOF .... EOF, but what is the reason?
      [orcl@Pluto]$ cat test
      
      set_orafra () {
      orafra=`sqlplus -s / as sysdba << EOF
         set pagesize 0 lines 500 trimout on trimspool on
         set echo off feedback off timing off pause off heading off verify off
         whenever oserror exit failure
         whenever sqlerror exit failure
         select name from v\\$recovery_file_dest;
         EOF`
      }
      
      set_orafra
      echo $orafra
      bash -version
      GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
        • 1. Re: Bash - here-document at line delimited by end-of-file
          Dude!
          Acutally it's only necessary to remove the preceding space from the last line containing the 'end' marker.

          It seems a requirement by sqlplus, but apparently also depends on the command interpreter. By definition, the 'end' marker is supposed to be terminated by a Return, however, in this case preceded by a Return.

          http://www.oracle.com/technology/pub/articles/saternos_scripting.html
          #!/bin/bash
          
          output=`sqlplus -s "/ as sysdba" <<EOF
                 set heading off feedback off verify off
                 select distinct machine from v\\$session;
                 exit
          EOF
          `
          
          echo $output
          • 2. Re: Bash - here-document at line delimited by end-of-file
            TommyReynolds-Oracle
            With a here document, the terminating token must not have leading spaces, unless you use a form like this:
            foo=`cat <<-EOF
                I am indented.
                I am too.
                EOF`
            The - before the stop token causes all leading whitespace to be
            ignored. This is implemented by every shell I know, except may
            C-Shell but that's another story..
            • 3. Re: Bash - here-document at line delimited by end-of-file
              Dude!
              I saw the "-" in some samples and was wondering what it does - Thanks for clarifying. I was also reading that the end marker must be terminated by a LF. Maybe in some versions of bash this is confused. Since "EOF`" ending with "`" should actually fail.

              Sorry, the problem remains.
              set_orafra () {
                 orafra=`sqlplus -s / as sysdba <<-EOF
                 set heading off
                 select name from v\\$recovery_file_dest;
                 exit
                 EOF
                 `
              }
              set_orafra
              echo $orafra
              
              ./test: line 12: warning: here-document at line 7 delimited by end-of-file (wanted `EOF')
              /u01/app/oracle/flash_recovery_area
              It still doesn't find the end marker.

              Anyway, not a big deal. More of a cosmetic issue after all.


              I just discovered some more info:

              https://bugzilla.redhat.com/show_bug.cgi?id=485664

              another intersting message:

              Bash have problems with here document since a long time! http://lists.gnu.org/
              (including analysis)

              http://www.linuxquestions.org/questions/programming-9/bash-scripting-i-o-redirection-error-272594/

              Seems like an old bug in Bash is back..
              • 4. Re: Bash - here-document at line delimited by end-of-file
                TommyReynolds-Oracle
                Seems like an old bug in Bash is back..
                Fortunate that I do not use bash :)

                Having a here document within a function definition should be OK, AIUI

                Hmm.....
                set_orafra () {
                   orafra=`sqlplus -s / as sysdba <<-EOF
                   set heading off
                   select name from v\\$recovery_file_dest;
                   exit
                   EOF
                   `
                }
                set_orafra
                echo $orafra
                I see you are backquoting to escape the dollar ($), but this is a function so
                I'm not sure just how many layers of escape you need here. Why not just turn
                variable substitution off:
                set_orafra () {
                   orafra=`sqlplus -s / as sysdba <<-'EOF'
                   set heading off
                   select name from v$recovery_file_dest;
                   exit
                   EOF
                   `
                }
                set_orafra
                echo $orafra
                See, I put single quotes around the stop token: the minus trims leading blanks
                and the quotes around the token prevent variable expansion in the here document.
                • 5. Re: Bash - here-document at line delimited by end-of-file
                  Dude!
                  Nice idea, but same issue, plus sqlplus now shows ORA-00911: invalid character.

                  Perhaps sqlplus is doing something special here, depending on the command-line interpreter?
                  • 6. Re: Bash - here-document at line delimited by end-of-file
                    TommyReynolds-Oracle
                    Don't think so, but perhaps your script file is in DOS format (\n\r) instead of UNIX format (\n)?
                    $ dos2unix myscript
                    $ ./myscript
                    • 7. Re: Bash - here-document at line delimited by end-of-file
                      Dude!
                      I checked that using ":set list" in vi and it shows a $ sign at the end of each line.

                      I'm analyzing the script PID (from another seesion)

                      strace -f -v -p 25734 -o ./output.txt

                      It shows a lot of interesting things, but it did not catch EOF anywhere.


                      26747 read(3, "set_orafra () {\n orafra=`sqlpl"..., 80) = 80
                      ...
                      26748 write(2, "./test: line 12: warning: here-d"..., 90) = 90

                      Edited by: waldorfm on May 26, 2010 2:33 PM
                      • 8. Re: Bash - here-document at line delimited by end-of-file
                        Dude!
                        Some progress....

                        Using <tab> instead of <spaces> for ident AND using <<-EOF works fine.

                        The "-" removes the <tabs>, not <spaces>, but at least this works.

                        Apparently something is screwed up since the requirement is that the end-marker is terminated by a LF, not preceded by it. I think it's a Bash bug.
                        • 9. Re: Bash - here-document at line delimited by end-of-file
                          Herbert van den Bergh-Oracle
                          here-docs can be tricky. Why not do:
                          set_orafra () {
                             orafra=`echo 'set heading off
                             select name from v$recovery_file_dest;
                             exit' | sqlplus -s / as sysdba`
                          }