Sql 从BTEQ接收到除0以外的退出代码后无条件bash退出
我有一个简单的bash脚本,其中包含以下bteqSql 从BTEQ接收到除0以外的退出代码后无条件bash退出,sql,bash,unix,teradata,Sql,Bash,Unix,Teradata,我有一个简单的bash脚本,其中包含以下bteq bteq <<EOF! .set width 255 .set format off .set titledashes off .logon ${HOST}/${USER}, ${PASSWORD}; .export report file=test_file.rpt; SELECT count(*) from test_db.test_table; /*this ta
bteq <<EOF!
.set width 255
.set format off
.set titledashes off
.logon ${HOST}/${USER}, ${PASSWORD};
.export report file=test_file.rpt;
SELECT count(*) from test_db.test_table; /*this table contains 90 rows in my case*/
.IF ERRORCODE <> 0 THEN .EXIT ERRORCODE;
.IF ACTIVITYCOUNT = 0 THEN .GOTO ALLOK
.LOGOFF;
.EXIT 3;
.LABEL ALLOK;
.LOGOFF;
.EXIT 0;
EOF!
/*Now if the above bteq returns 0,(i.e. RET_FLAG = 0) the below code executes
with RC (return code) = 0 but if it returns values other than 0 below code doesn't executes and finally bash exits.*/
RET_FLAG=$?
print_msg "RET_FLAG: $RET_FLAG " /*when return code other than 0 this line is not printed*/
if [ $? -gt 0 ];
then
print_msg "Hello world get succeed" /* printing function */
exit 1
fi
bteq在if条件下,请使用RET_标志-gt 0。不是$?好的,我发布了问题,找到了答案:)
实际上,问题是从bteq到bash的退出代码。
Bash通常在收到任何子例程调用的退出代码而不是0时退出,即Bash行为。
有关Bash中退出代码的更多详细信息
现在让我们来看看上面的问题,并尝试回答它
bteq <<EOF!
.set width 255
.set format off
.set titledashes off
.logon ${HOST}/${USER}, ${PASSWORD};
.export report file=test_file.rpt;
#Assumption:- below query executes successfully
SELECT count(*) from test_db.test_table; /*this table contains 90 rows in my case*/
.IF ERRORCODE <> 0 THEN .EXIT ERRORCODE;
.IF ACTIVITYCOUNT = 0 THEN .GOTO ALLOK
#since ACTIVITYCOUNT will be other than 0(for above assumption to be true), code will execute exactly the line below
#thus bteq will exit with exit status 3
.LOGOFF;
.EXIT 3;
.LABEL ALLOK;
.LOGOFF;
.EXIT 0;
EOF!
我们如何处理这些不需要的退出,退出代码不是子程序调用或bteq的0?
索尔:-我们可以在bteq的一开始就使用set+e
,set+e
停止bash退出,即使退出代码不是0,但不推荐使用此方法。
最佳做法是使用trap
,关于如何在bash中使用trap,请参阅以下文档。回想一下,执行的每个命令都会重置$?
的值。在这种情况下,不要测试$?
测试$RET\u标志
。$?
现在保存来自print\u msg
的“0”退出代码。或者,你在bteq中的退出3
可能没有你想象的那样工作。在EOF1
之后立即添加一个echo RC=$?
。祝你好运。谢谢@Sheller,echo RC=$?如果bteq退出状态为0,则打印0;如果bteq退出状态为3,则打印3(RC=3)。我在想,可能是bash将RC(返回代码)=3解释为来自bteq的错误,因此脚本可能已退出。是的,0返回代码表示“良好”。任何其他数字都表示“坏”。祝你好运。谢谢你的建议,但即使没有条件,同样的事情也会发生。如果bteq以状态3退出,则脚本以RC=3退出;如果bteq以状态0退出,则脚本成功运行
RET_FLAG=$?
print_msg "RET_FLAG: $RET_FLAG " /*when return code other than 0 this line is not printed*/
if [ $RET_FLAG -gt 0 ]; #previously I was using if[$ -gt 0]
then
print_msg "Hello world get succeed" /* printing function */
exit 1
fi