bash 在 shell 脚本中捕获 db2 输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7641722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:55:15  来源:igfitidea点击:

capture db2 output in shell script

bashdb2

提问by Chris

I have to convert a shell script from Oracle to db2. I found on this forum a sample of Oracle script I used ; it looks like this

我必须将 shell 脚本从 Oracle 转换为 db2。我在这个论坛上找到了我使用的 Oracle 脚本示例;它看起来像这样

#!/bin/bash
OUT=`$ORACLE_HOME/bin/sqlplus -s user/pass@instance   << EOF
select sysdate from dual;
exit success
EOF`
echo $OUT

This will output "03-OCT-11" (Oracle sysdate). My db2 script looks like this

这将输出“03-OCT-11”(Oracle 系统日期)。我的 db2 脚本如下所示

#!/bin/bash
db2bin="/users/db2inst1/sqllib/bin"
#connect
$db2bin/db2 connect to myschema;
#query
$db2bin/db2 "SELECT CURRENT_DATE FROM SYSIBM.SYSDUMMY1 WITH UR";
#debug
echo $?
#check
if [ $? = "0" ] then         echo "found-do something"
else        echo "not found-good bye"
fi
#terminate
$db2bin/db2 quit;

It works but does not retrieve the date ; only "0" or "1" (true/false). How can I retrieve the date from my Db2 query result??

它有效但不检索日期;只有“0”或“1”(真/假)。如何从我的 Db2 查询结果中检索日期??

回答by Mel Boyce

I'm not that familiar with db2, but it sounds like you need to redirect the output from the SELECT statement.

我对 db2 不太熟悉,但听起来您需要重定向 SELECT 语句的输出。

e.g., db2 SELECT CURRENT_DATE FROM SYSIBM.SYSDUMMY1 WITH UR > /tmp/output

例如, db2 SELECT CURRENT_DATE FROM SYSIBM.SYSDUMMY1 WITH UR > /tmp/output

More information here: http://www.ibm.com/developerworks/data/library/techarticle/dm-0503melnyk/

更多信息请访问:http: //www.ibm.com/developerworks/data/library/techarticle/dm-0503melnyk/

Edit: also, does the db2 select line output to stdout? I don't have a copy readily available to test with :/

编辑:还有,db2 选择行输出到标准输出吗?我没有现成的副本可用于测试:/

回答by Chris

I figured it out: the trick is to use /dev/null.

我想通了:诀窍是使用/dev/null。

#!/bin/bash
DB2INSTANCE=db2inst1
BIN="/users/db2inst1/sqllib/bin"
OUT=`${BIN}/db2 connect to myschema > /dev/null 
${BIN}/db2 -x "SELECT CURRENT_DATE FROM SYSIBM.SYSDUMMY1 WITH UR"
${BIN}/db2 quit > /dev/null
`
echo $OUT

Hope this helps.

希望这可以帮助。

回答by Fred Sobotka

Chris, your environment variables and your DB2 command path should be set by sourcing db2profile. The quit command is unnecessary when calling the db2 command with either a SQL file or a single statement specified as part of the command-line.

Chris,您的环境变量和 DB2 命令路径应该通过采购 db2profile 来设置。当使用 SQL 文件或指定为命令行一部分的单个语句调用 db2 命令时,quit 命令是不必要的。

#!/bin/bash
. ~db2inst1/sqllib/db2profile
OUT=`db2 connect to myschema >/dev/null 2>&1; db2 -x values current date`

Your database connection will remain available until the script ends, so you can run successive statements without reconnecting.

在脚本结束之前,您的数据库连接将保持可用,因此您无需重新连接即可运行连续语句。

#!/bin/bash
. ~db2inst1/sqllib/db2profile
db2 connect to myschema >/dev/null 
OUT=`db2 -x values current date`
AAA=`db2 -x " select a from sometable where b = 'c' " `