Does standard input redirection within a variable work differently on RHEL 7 than older versions.

Latest response

Hi there!

I faced an interesting issue. The following code works well on RHEL 5, but drops an error on RHEL7:

case $1 in
 a)
    *some code here*
    ;;
 b)
    *some code here*
    ;;
 c)
    v_rekord=$(sqlplus -s user/password<<EOF
               *select statement here;*
               exit;
          EOF
          )
    ;;
 d)
    *some code here*
    ;;
 *)
    *some code here*
    ;;
esac

Error:
./t2.sh: line 64: unexpected EOF while looking for matching `)'
./t2.sh: line 120: syntax error: unexpected end of file

But it works well between backquotes:

case $1 in
 a)
    *some code here*
    ;;
 b)
    *some code here*
    ;;
 c)
    v_rekord=`sqlplus -s user/password<<EOF
              *select statement here;*
              exit;
     EOF
    `
    ;;
 d)
    *some code here*
    ;;
 *)
    *some code here*
    ;;
esac

Is there an explanation or official statement for this?

Responses

This works if you add a \ to the end of the multi-line command substitution, i.e.:

 c)
    v_rekord=$(sqlplus -s user/password<<EOF \
               *select statement here;* \
               exit; \
          EOF \
          )
    ;;

I guess you've found some difference between how the RHEL5 and RHEL7 bash interprets multi-line paren subs like this.

Thank you Jamie!

I found the root of the problem. Now it works with parentheses too. When the script ran exactly to this c) branch, dropped a warning message: warning: here-document at line 78 delimited by end-of-file (wanted `EOF') I thought it does not like if the "EOF" is not at the beginning of the line. I deleted the leading spaces, and it worked. Then I reverted to the parentheses formula, and it still working. It's not very nice, but at least I understand. The leading spaces were not a problem in RHEL 5.

Greets, Tamas

If you prefix your here-doc delimiter with a hyphen (-EOF), bash will automatically ignore leading tabs (not spaces) from the input lines up to and including the delimiter.

Depending on how short your sql is, you might find it more elegant to use a here-string, e.g.: sql -s user/pass <<<"blahblah;".

In man bash, search for Here Documents.

This thread is an example of why using shell-linters can be a good thing.

Also: you're speaking less a difference in the RHELs than a difference in the BASH-version you're using to interpret your code. RHEL 5's version of BASH is an older implementation than RHEL 7 or 8's. Newer versions of BASH provide additional features but, in some cases, it can be a skosh more pedantic about bad scripting-practices than older implementations.

Hi Ryan!

Yes. The root of the problem were the leading spaces before the delimiter (EOF). I checked the man page in both version, and they are identical in content. So it shouldn't have been working in earlier verson. I don't like using tabs, nor the backquota formula. I just removed the leading spaces, and it works well.

Thanks to everyone for help!