How to return an exit code from SQL*Plus to the Unix Shell ?[akadia]

If you want to return a code from SQL*Plus to the UNIX shell script, then the WHENEVER SQLERROR directive can help. We can return a status from 0...255 to the UNIX shell. This is usually in the $? environment variable ( for the Korn or Bash Shell ) immediately after execution of a command. Here is a KSH script that shows how this might work:

#!/bin/ksh

sqlplus -s scott/tiger </dev/null 2>&1

variable rc number
whenever sqlerror exit sql.sqlcode

begin
-- The Variable rc is the return code (from your programm logic)
-- which you want to send from SQL*Plus to the shell, when
-- the scripts aborts due the
WHENEVER SQLERROR

:rc := 1;
if (:rc <> 0)
then
raise_application_error((-20000-224) - :rc, 'Error!');
end if;
end;
/
EOF

Run this code as follows and you will see:

$ ./test.ksh
$ echo $?

1

The 1 is what you are looking for. The key is to use ...

raise_application_error( (-20000-224) - :rc, 'Error!' );

... to raise the error. We can raise errors in a given range, but the shell will only keep an unsigned byte in the status return value (values 0...255). It takes our exit file and just looks at that last byte. By using -20000-224 and subtracting from your return code, we end up exiting with the value of your return code ( given that :rc is in the range...255 ! ).


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/936/viewspace-60613/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/936/viewspace-60613/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值