在shell脚本中对sybase查询进行Xpassing命令行参数

时间:2012-08-07 05:30:26

标签: shell solaris isql

这是我的剧本。

#!/usr/bin/sh


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

exit

我正在执行此脚本:

temp2.sh 0000

输出为0。 但是当我手动执行查询时,输出为1是正确的。 这里的问题是命令行参数$ 1没有传递给查询。

我怎么能实现这个目标? 我已经尝试了所有这些可能性:

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

我使用的是solaris unix,DB是sybase。

2 个答案:

答案 0 :(得分:0)

我认为,问题在于查询中的单引号是由shell解释的,所以

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

被解释为

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

作为文字字符串。

我没有任何可用的测试方法,但您可以尝试将其替换为

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

我认为(我再次强调,我现在没有任何可用的测试)双引号应该使单引号打印成字符,因此它将被解释为

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

给出您提供的命令行输入。

答案 1 :(得分:0)

问题解决了:

而不是将脚本调用为

temp2.sh 0000 
我称之为:

temp2.sh "0000"