【发布时间】:2012-08-07 10:40:58
【问题描述】:
我有函数 ShowJobHistory,它是从另一个调用的。在第一次调用这个函数时一切正常,它计算正确数量的参数,根据我的需要解析它们。但是在接下来的调用中,即使我指定了多个参数,此函数也会将它们视为一个,并且在解析它们后看起来是( jb.RJBGID=12871 12873 12868 )。我的功能有什么问题?
ShowJobHistory () {
conditions=
argCount=$#
if [[ $argCount -ne 0 ]] then
if [[ $1 == [iI] && $argCount -eq 3 ]] then
if [[ $2 -lt $3 ]] then
conditions="( jb.RJBGID between $2 and $3 )"
else
conditions="( jb.RJBGID between $3 and $2 )"
fi
else
conditions="("
for nr in $@
do
conditions="${conditions} jb.RJBGID=${nr} or "
done
conditions=${conditions%or }
conditions=$conditions")"
fi
typeset query
下面的函数调用 ShowJobHistory。
ShowJobHistoryMenu () {
typeset jobID
save=
echo "Enter jobIDs"
read jobID?"Enter jobID: "
while [[ $save != [nNyY] ]]
do
read save?"Save output to file? [y/n]"
done
if [[ save = [yY] ]] then
ShowJobHistory $jobID | tee $TRACEDIR/output.txt
else
ShowJobHistory $jobID
fi
}
【问题讨论】:
-
你的输入是什么,你得到的输出是什么,预期的输出是什么?
-
此函数的参数由用户输入,解析的参数是 sql where 语句,如果出现 sql 查询结果,我会收到 sql 错误作为意外输出。
-
( jb.RJBGID=12871 12873 12868 ) after parsing them.你是在哪个脚本代码点打印的? -
它在sql查询中显示为错误的一部分,但它会在if条件之后打印。打印
$#显示1,即使a写了几个参数,例如:12871 12873 12868 -
您可以尝试使用 $@ 编写一个循环遍历所有参数并更新
argCount(类似于您为nr所做的)检查它是否真的得到了这些参数。