简体   繁体   English

如何从shell / shell脚本获取sqlplus命令的输出状态?

[英]How to get output status of sqlplus command from shell/shell script?

I want to run a sqlplus command from shell script and check the status of sqlplus command from shell script (ie success or failure ie in bash it's $? ). 我想从shell脚本运行sqlplus命令并从shell脚本检查sqlplus命令的状态(即成功或失败,即在bash中它是$?)。 Can you provide me hints on how to do that? 你能告诉我如何做到的提示吗?

For example following script works, but I am interested in a shell script which will tell me whether the schema/account has "alter database link" privileges or not (ie how the shell script can interpret output of sqlplus and tell me whether it's success or failure) - 例如下面的脚本工作,但我对shell脚本感兴趣,它将告诉我架构/帐户是否具有“alter database link”特权(即shell脚本如何解释sqlplus的输出并告诉我它是否成功或失败) -

#!/bin/bash

schema=$1
password=$2
database=$3

echo "schema is $schema"
echo "password is $password"
echo "database is $database"

sqlplus -s "$schema/$password@$database" <<ENDOFSQL
SET SPACE 0
SET LINESIZE 80
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET MARKUP HTML OFF SPOOL OFF
select privilege from session_privs where PRIVILEGE = 'ALTER DATABASE LINK';
exit;
ENDOFSQL
##### END OF SCRIPT

Thanks in advance. 提前致谢。 -Shashi Divekar -Shashi Divekar

You ask for two separate things in your question. 你在问题中要求两个单独的东西。 First you ask about checking the "output status" and then you ask about "interpret[ing] output", which is different. 首先你问一下检查“输出状态”然后你问“解释输出”,这是不同的。 Here's an answer for your first question about output status, ie exit code: 这是关于输出状态的第一个问题的答案,即退出代码:

...
exit;
ENDOFSQL

if [ $? -ne 0 ]; then
    echo "whoops"
else
    echo "all good"
fi
##### END OF SCRIPT

See the docs for more info. 有关详细信息,请参阅文档

When you query the session_privs table for the privilege column, you can expect an output along the following lines: 当您在session_privs表中查询特权列时,您可以期望输出以下行:

PRIVILEGE
-----------------------
CREATE VIEW
DEBUG CONNECT SESSION
CREATE SESSION
ALTER SESSION
...

And I know that you have tried to remove everything extraneous from the query output, nevertheless you have to check it anyway. 而且我知道你已经尝试从查询输出中删除一些无关紧要的内容,但无论如何你必须检查它。 This is since the sqlplus exit status is only about the success or failure of running the query, and not the query output, so why don't you simply grep for the privilege you want to find? 这是因为sqlplus退出状态只是关于运行查询的成功或失败,而不是查询输出,所以为什么不简单地grep你要查找的权限?

...
SQLPLUS_OUTPUT=$(sqlplus -s "$schema/$password@$database" <<ENDOFSQL
select privilege from session_privs;
exit;
ENDOFSQL
) || { echo "Failed to run query"; exit 1; }

if [ -n "$(printf '%s\n' $SQLPLUS_OUTPUT | grep 'ALTER DATABASE LINK')" ]; then
    echo "Privilege located!"
else
    echo "Privilege NOT located."
fi
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM