Back ticks and $()

In one of my previous threads, someone suggested not to use backticks. When I googled, I came to know that back ticks are deprecated instead $() should be used. But I face issue while using $().
Note: I used echo of the sql just to debug.

The following is the code

#!/bin/ksh
#set -x
ScriptDir=$(dirname $0)
. ${ScriptDir}/setEnv.sh
VAL=X

  echo "\$(sqlplus -s $user_name/$user_pwd@$database_name <<EOF
     set pagesize 0 LINESIZE 1000 feedback off verify off heading off echo off
     select count(1) from dual where dummy = '$VAL';
    EXIT;
  EOF)"

  REC_EXIST=$(sqlplus -s $user_name/$user_pwd@$database_name <<EOF
  set pagesize 0 LINESIZE 1000 feedback off verify off heading off echo off
  select count(1) from dual where dummy = '$VAL';
  EXIT;
  EOF)

  echo "CHECK RUN STATUS"
  run_status=$?
  echo "RUN STATUS    : $run_status"
  echo "REC_EXIST      : $REC_EXIST"

  if [ $REC_EXIST == 0 ] ;then
    echo "No Records    : $REC_EXIST"
  else
    echo "Records exists: $REC_EXIST"
  fi

The above code works fine as long as I use back tick. When I replace backticks with $(), I encounter the following error

$(sqlplus -s xxxxxxx/xxxxx@xxxxxxx <<EOF
       set pagesize 0 LINESIZE 1000 feedback off verify off heading off echo off
       select count(1) from dual where dummy = 'X';
       EXIT;
       EOF)


CHECK RUN STATUS
RUN STATUS    : 0
REC_EXIST     :   select count(1) from dual where dummy = "$VAL"
                                          *
ERROR at line 1:
ORA-00904: "$VAL": invalid identifier
test.sh[XX]: count(1): unknown test operator
Records exists:   select count(1) from dual where dummy = "$VAL"
                                          *
ERROR at line 1:
ORA-00904: "$VAL": invalid identifier
Records exists:   select count(1) from dual where dummy = "$VAL"
                                          *
ERROR at line 1:
ORA-00904: "$VAL": invalid identifier

By using $() I got good results for other scripts. Can anyone explain what went wrong.
Appreciate your response

Shouldn't you escape the single quotes around $VAL?

\'$VAL\'

--ahamed

Already tried it. Also tried using double quotes in place of single quote and combination of single quote, double quote, and escape character.
Just by using single quotes, I got the following error

$(sqlplus -s xxxx/xxxx@xxxx <<EOF
       set pagesize 0 LINESIZE 1000 feedback off verify off heading off echo off
       select count(1) from dual where dummy = \'X\';
       EXIT;
       EOF)


CHECK RUN STATUS
RUN STATUS    : 0
REC_EXIST     :   select count(1) from dual where dummy = \'X\'
                                          *
ERROR at line 1:
ORA-00911: invalid character
test.sh[XX]: count(1): unknown test operator
Records exists:   select count(1) from dual where dummy = \'X\'
                                          *
ERROR at line 1:
ORA-00911: invalid character
Records exists:   select count(1) from dual where dummy = \'X\'
                                          *
ERROR at line 1:
ORA-00911: invalid character

Can you please paste your code with the changes?

--ahamed

Set escape to ON and try:

Out=$(sqlplus -s xxxx/xxxx@xxxx <<EOF
set pagesize 0 LINESIZE 1000 feedback off verify off heading off echo off escape on
select count(1) from dual where dummy = \'X\';
EXIT;
EOF)

echo "$Out"
2 Likes

Thanks Yoda. It worked.
Thanks Ahmad for your time and trying to resolve it