ITPub博客

首页 > Linux操作系统 > Linux操作系统 > How to return an exit code from SQL*Plus to the Unix Shell ?[akadia]

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

原创 Linux操作系统 作者:jlandzpa 时间:2019-05-09 11:45:05 0 删除 编辑

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 <<-EOF 1>/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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2001-10-12

  • 博文量
    240
  • 访问量
    173365