我想知道是否有可能获得sqlplus
以某种方式输出以发现我的数据库是否已启动。
我想在数据库上运行脚本列表,但在执行此操作之前,我想知道数据库是否已启动并正在使用我的脚本运行。
这是我尝试过的:
sqlplus /@DB1 << EOF
> select 1 from dual;
> EOF
无法连接,但sqlplus的返回码仍然显示“一切正常”!
SQL*Plus: Release 11.2.0.4.0 Production on Mon Nov 28 10:06:41 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
ERROR:
ORA-12505: TNS:listener does not currently know of SID given in connect
descriptor
Enter user-name: SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]]
where ::= [/][@]
::= [][/][@]
Enter user-name: ju@srv:/tmp/jcho $ echo $?
0
我知道我可以 grep 测试查询的结果,如下所示:
a.sh
sqlplus /@DB1 << EOF
select 'ALL_GOOD_BOY' from dual;
EOF
call:
gives 1
如果连接有效,则线路,0
否则:
$ a.sh |grep ALL_GOOD_BOY|wc -l
...这对我来说似乎有很多步骤。还有其他方法可以将 sqlplus 设置为“无法连接”给出“错误”返回代码的模式吗?
感谢@Kacper 提供的参考,我可以调整这个sqlplus /nolog
就我而言;想法是这样的:
- open sqlplus仅在不连接的情况下
- 设置特定的返回代码
SQLERROR
- 这就是发生的情况connect
fails
- 返回代码可以像往常一样在调用者脚本中收集:
a.sh
sqlplus /nolog << EOF
WHENEVER SQLERROR EXIT 50
WHENEVER OSERROR EXIT 66
connect /@${MISTERY_DB}
exit;
EOF
然后调用:
/ju $ export MISTERY_DB="eg_NON_EXISTING_DB"
/ju $ a.sh
SQL*Plus: Release 11.2.0.4.0 Production on Tue Nov 29 08:43:44 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
SQL> SQL> SQL> ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
/ju $ echo $?
50
还相关:在 shell 脚本中连接到 sqlplus 并运行 SQL 脚本
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)