开发者

bash quoted variable substitution in sqlplus + heredoc [closed]

开发者_Python百科 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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 `.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜