无法从Shell脚本连接到Oracle DB

时间:2018-06-19 13:22:36

标签: shell

从Shell脚本连接到Oracle DB(负载平衡环境-多主机)时,我遇到了挑战。

我直接在linux框中尝试了以下操作,并且可以正常工作。

  

/apps/oracle/product/11.2.0/client_1/bin/sqlplus   / @'(DESCRIPTION =(ADDRESS_LIST =(FAILOVER = YES))   (地址=(协议= TCP)(主机=主机名1)(端口= 6235))(地址=   (协议= TCP)(主机=主机名2)(端口= 1521)))(CONNECT_DATA =   (服务器=专用)(SERVICE_NAME = lruata1)))

当我从shell脚本尝试相同的操作(从环境脚本读取db connect url)时,它不起作用。

获取输出作为sqlplus参数

SQL * Plus:11.2.0.1.0版正式版

版权(c)1982、2009,Oracle。保留所有权利。

使用SQL Plus执行SQL,PL / SQL和SQL Plus语句。

用法1:sqlplus -H | -V

-H             Displays the SQL*Plus version and the
               usage help.
-V             Displays the SQL*Plus version.

config.sh中的内容

oracle.base=/apps/oracle/product/11.2.0/client_1
db.connect.url='(DESCRIPTION=(ADDRESS_LIST=(FAILOVER = YES) (ADDRESS = (PROTOCOL=TCP)(HOST=hostname1)(PORT = 6235)) (ADDRESS = (PROTOCOL = TCP)(HOST = hostname2)(PORT = 1521))) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = lruata1)))'
log.path=/apps/lriservers/bpmdbo_scripts/logs

在主脚本中读取键和值

function getPropertyFromFile()
{
propertyName=`echo $1 | sed -e 's/\./\\\./g'`
fileName=$2;
cat $fileName | sed -n -e "s/^[ ]*//g;/^#/d;s/^$propertyName=//p" | tail -1
}

export ORACLE_BASE=`getPropertyFromFile oracle.base $CONFIG_FILE`
export CONNECT_IDENTIFIER=`getPropertyFromFile db.connect.url $CONFIG_FILE`
export LOG_PATH=`getPropertyFromFile log.path $CONFIG_FILE`

export ORACLE_HOME=${ORACLE_BASE}

echo "Oracle Base : "$ORACLE_BASE
echo "Connect Identifier : "$CONNECT_IDENTIFIER
echo "Log Path : "$LOG_PATH

export LD_LIBRARY_PATH=${ORACLE_HOME}/lib
export SQP=${ORACLE_HOME}/bin/sqlplus

LOG_FILE=$LOG_PATH/$ENV_NAME'_'$DEPLOYMENT_VERSION.log

cd $SCRIPT_HOME
CONNECT_STRING=${DB_USERNAME}/$DB_PASSWORD@${CONNECT_IDENTIFIER}


${SQP} -S  ${CONNECT_STRING} << EOF
WHENEVER SQLERROR EXIT SQL.SQLCODE;

SET ECHO ON;
SET SERVEROUTPUT ON;

spool $LOG_FILE
@$SCRIPT_NAME
spool off;

EXIT
EOF

问候 巴拉吉(Balaji Subramaniam)

0 个答案:

没有答案