Regarding command substitution

Oracle Linux 5.6, 64-bit
Given the following snippet

wrkvar=`sqlplus -s / as sysdba <<EOF
set echo off feedback off head off trimsp on
select count(*) from v\$parameter
where name in ('db_file_name_convert','log_file_name_convert')
and value is not null;
EOF`
echo wrkvar=$wrkvar

Produces the following results

wrkvar=select count(*) from v db_refresh db_refresh.sav doit ERROR at line 1: ORA-00942: table or view does not exist

Whereas taking the entire sqlplus block out of the command substitution (just executing it) runs as expected. Also, using the other method of command substitution works as expected:

wrkvar=$(sqlplus -s / as sysdba <<EOF
set echo off feedback off head off trimsp on
select count(*) from v\$parameter
where name in ('db_file_name_convert','log_file_name_convert')
and value is not null;
EOF)
echo wrkvar=$wrkvar

produces

wrkvar= 2

It would appear that something about the first variant doesn't like the escaping of the $ (v\$parameter). Obviously a gap in my understanding ... :frowning:

I don't know sqlplus and therefore I don't know what effect you hope to achieve. I also don't understand your need to use use backquotes. But try doubling the backslash.

bash-3.1$
bash-3.1$ abc="kkjhkjhkjhkj"
bash-3.1$ echo ` echo a\b v\$abc`
ab vkkjhkjhkjhkj
bash-3.1$ echo $( echo a\b v\$abc)
ab v$abc
bash-3.1$ echo ` echo a\b v\\$abc`
ab v$abc
bash-3.1$
bash-3.1$
1 Like

in order to avoid parameter expansion within the 'here-doc' (\$parameter) quote the "EOF".
Doing 'man bash" yields the following:

       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 sub
       stitution, and arithmetic expansion.  In the latter case, the character sequence  \<newline>
       is ignored, and \ must be used to quote the characters \, $, and `.

1 Like