I am trying to connect to an oracle database from a shell script . Script for connecting to database is given below:
#!/bin/bash
# Shell script to run sql files from command line.
# Pre-Req: sqlplus client shall be installed already.
###########################################################
# Variables Section (DB Details)
###########################################################
driverClassName=oracle.jdbc.driver.OracleDriver
url="(description=(address_list=(address=(protocol=TCP)(host=oradb.test.env.org)(port=1521)))(connect_data=(service_name=TEST_S)))"
DB_UserName="abc"
DB_Password="abc"
LogDirectory="/var/tmp/logs"
DataDirectory="/var/tmp/data"
DB_HostName="oradb.test.env.org"
DB_Port="1521"
DB_SID="KONTOR"
DIR_SqlFiles="C:\git\sql"
##########################################################
# All Script Functions Goes Here
##########################################################
db_statuscheck() {
echo "`date` :Checking DB connectivity...";
echo "`date` :Trying to connect "${DB_UserName}"/"${DB_Password}"@"${DB_SID}" ..."
echo "exit" | sqlplus -S ${DB_UserName}/${DB_Password}@${url} | grep -q "Connected to:" > /dev/null
if [ $? -eq 0 ]
then
DB_STATUS="UP"
export DB_STATUS
echo "`date` :Status: ${DB_STATUS}. Able to Connect..."
else
DB_STATUS="DOWN"
export DB_STATUS
echo "`date` :Status: DOWN . Not able to Connect."
echo "`date` :Not able to connect to database with Username: "${DB_UserName}" Password: "${DB_Password}" DB HostName: "${DB_HostName}" DB Port: "${DB_Port}" SID: "${DB_SID}"."
echo "`date` :Exiting Script Run..."
exit
fi
}
Main() {
echo "`date` :Starting Sql auto run script."
db_statuscheck
echo "`date` :Sql auto run script execution completed."
}
Main | tee autosql.log
When I tryto connect directly from terminal connection string works, but from shell script it fails. Output I am getting is:
7. apr 2016 15:18:09 :Starting Sql auto run script.
7. apr 2016 15:18:09 :Checking DB connectivity...
7. apr 2016 15:18:09 :Trying to connect abc/abc@TEST_S ...
7. apr 2016 15:18:09 :Status: DOWN . Not able to Connect.
7. apr 2016 15:18:09 :Not able to connect to database with Username: abc abc Password: kjopsprosesser_utv4 DB HostName: oradb.test.env.orgDB Port: 1521 SID: TEST_S
7. apr 2016 15:18:09 :Exiting Script Run...