bash quoted variable substitution in sqlplus + heredoc [closed]
must be an easy one. [edit] i am writing a BASH script, using HERE document to redirect some statements to SQL*Plus command-prompt.
sqlplus -s user/pw@db > $TMPFILE <<EOF
SET ECHO OFF;
SET FEEDBACK OFF;
SET HEADING OFF;
select x
from a_table
where a_field like '%$1%';
EXIT;
EOF
of course this doesn't work as there is no substitution within single quotes. so how i go about doing this? i cannot do something like "'%$1%'"
because double-quotes still go as input to SQL*Plus and cause error.
thank you.
It should work without modification
My simple tests works:
cat << HERE
$BASHPID
'$BASHPID'
HERE
returns
4608
'4608'
This is because
If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence \newline is ignored, and ‘\’ must be used to quote the characters ‘\’, ‘$’, and ‘`’.
Have you tried it? It works for me. Check the man page which confirms that all lines of the here-document are subjected to parameter expansion.
The format of here-documents is:
<<[-]word here-document delimiter
No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence \ is ignored, and \ must be used to quote the characters \, $, and `.
精彩评论