Back ticks and $()


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Back ticks and $()
# 1  
Old 08-12-2013
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
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
Code:
$(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
# 2  
Old 08-12-2013
Shouldn't you escape the single quotes around $VAL?

Code:
\'$VAL\'

--ahamed
# 3  
Old 08-12-2013
Quote:
Originally Posted by ahamed101
Shouldn't you escape the single quotes around $VAL?

Code:
\'$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
Code:
$(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

# 4  
Old 08-12-2013
Can you please paste your code with the changes?

--ahamed
# 5  
Old 08-12-2013
Set escape to ON and try:
Code:
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"

These 2 Users Gave Thanks to Yoda For This Post:
# 6  
Old 08-12-2013
Thanks Yoda. It worked.
Thanks Ahmad for your time and trying to resolve it
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. What is on Your Mind?

I'm back

Hi all, I used to post here years ago, and was a moderator, my old username: zazzybob. Anyway, after a few years away focusing on my career, I'm back and keener than ever to get involved in the unix.com community again. I'm looking forward to getting back into the swing of things, helping... (11 Replies)
Discussion started by: tokiwinter
11 Replies

2. Shell Programming and Scripting

Passing variable to Expression in back ticks.

Hi, In my perl script I want to check whether *.csv files exist and take the count . Below is the code: $path = “/home/usr/jan/myfiles” my $File_Count = `ls *.csv | wc -l `; # Checks in the current directory #Works fine if files exists. my $File_Count = `ls $path/*.csv | wc -l `; # I need... (2 Replies)
Discussion started by: jisha
2 Replies

3. Shell Programming and Scripting

Shell Script problem $( vs ticks [`]

ok so, I recently started using $(echo one two three) to execute commands in scripts instead of using `echo one two three`. This method works wonderfully on ubuntu. However, it doesn't seem to work on other unix systems, i.e redhat/sun sun solaris. I really hate to go back to the ticks "`"... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. IP Networking

Back-to-Back Connection using HBAs

Hi every body, Is it possible to connect two servers Back-to-Back (Point-to-Point) using HBA adapters & using Fiber. Note it is direct connection & there is no switches between the servers. I'm concern about using HBA adapters, it is possible or not. Thanks in advance. :) (3 Replies)
Discussion started by: aldowsary
3 Replies

5. AIX

back to back printing in UNIX

Hi , Can you suggest me how to back to back printing in UNIX? Is there any way? Kindly advise. Regards Vijaya Amirtha Raj (3 Replies)
Discussion started by: amirthraj_12
3 Replies

6. HP-UX

Ticks in seconds.

Hello all, Is there any thumb rule or aproximation of the equivalence in second of one tick? Thank you in advance. (1 Reply)
Discussion started by: mig28mx
1 Replies
Login or Register to Ask a Question