Xpassing command line argument to sybase query inside a shell script

1.8k Views Asked by At

This is my script.

#!/usr/bin/sh


isql -UXx -Pxxxxxx <<!
set nocount on
use xxxx
go
select count(*) from BSC  where bsc='$1'
go
!

exit

i am executing this script as :

temp2.sh 0000

the output is 0. but when i execute the query manually then the output is 1 which is correct. problem here is the command line argument $1 is not passed to the query.

how could i achieve this? I have tried all these possiblities:

bsc='$1'- output is 0
bsc="$1"- output is 0
bsc=`$1`- Syntax error
bsc="'$1'"- output is 0

I am using solaris unix and DB is sybase.

2

There are 2 best solutions below

1
On

The problem here, I think, is that the single quotes in your query are being interpreted by the shell, so that

select count(*) from BSC  where bsc='$1'

is being interpreted as

select count(*) from BSC  where bsc=$1

as a literal string.

I don't have anything available to me to test this with, but you might try replacing that like with

select count(*) from BSC  where bsc="'$1'"

I think (and I stress again that I don't have anything available to test with right now) that the double quotes should make the single quotes print out as characters, so it will be interpreted as

select count(*) from BSC  where bsc='0000'

given the command line input you provided.

1
On

Problem is solved:

instead of calling the script as

temp2.sh 0000 

i called it as :

temp2.sh "0000"