shell脚本中的db2查询不能在shell中以空结果运行 [英] db2 query in shell script doesn't run with empty results in shell
问题描述
我在shell中有以下脚本。 test.sh
I have below script in shell. test.sh
#! /bin/bash
connect_stat=$(db2 -x "connect to $DB_NAME USER $DB_USER using $DB_PASSWORD" )
db2 "SET SCHEMA=SCHEMA1"
while read line;
do
a=$(db2 -x "SELECT C.id FROM table C WHERE C.col1 IN ('$line)') with ur")
echo $a
done<inputs.txt
运行 sh test.sh时得到空结果
I get empty results when I run "sh test.sh"
当我通过腻子在db2中运行与上面相同的查询时,会得到结果。
Where as when I run the same above query in db2 through putty, I get results.
我在dbcon文件下面。将触发腻子中的dbcon文件,结果可用
I have below dbcon file. Will trigger the dbcon file in putty and results are available
/admin/.profile
/admin/.profile
db2 connect to DB_NAME USER DB_USER using DB_PASSWORD
db2 set SCHEMA=SCHEMA1
db2
db2 => SELECT C.id FROM table C WHERE C.col1 IN ('xyz-asd-asd') with ur
ID
----------------------------------------------------------------
123
1 record(s) selected.
请帮助我在shell脚本中做错的事情。
Please help me what I am doing wrong in shell script.
预先感谢。
推荐答案
$(db2 -x connect $ DB_NAME ...)
在子shell中执行,当子shell退出时,它将终止连接,所以到 db2 SELECT C.PARTNUMBER ...
连接不存在。
$(db2 -x "connect to $DB_NAME...)
executes in a subshell, which terminates the connection when the subshell exits, so by the time you get to db2 "SELECT C.PARTNUMBER..."
the connection does not exist.
这篇关于shell脚本中的db2查询不能在shell中以空结果运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!