简体   繁体   中英

Connecting db using shell script by passing db credentials from shell

Hi I am trying to connect my database using shell script and by passing the db credentials from shell. I am getting "ORA-12162: TNS:net service name is incorrectly specified". Please find the script below.

#!/bin/bash
DB_CREDENTIALS=$1

mkdir -p $PWD/logs

sqlplus -silent $DB_CREDENTIALS <<EOFSQL

set echo on
set timing on
set heading on
set feedback on
set linesize 5000
SET PAGESIZE 0
SET TRIMSPOOL ON


@teshscrpt.sql


EOFSQL

And the error is :

ERROR:
ORA-12162: TNS:net service name is incorrectly specified


SP2-0306: Invalid option.
Usage: CONN[ECT] [logon] [AS {SYSDBA|SYSOPER}]
where <logon>  ::= <username>[/<password>][@<connect_identifier>] | /
SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus

Can somebody help me out kindly.

Thanks, sudhir.

First, you should take the habit of quoting your variables:

#!/bin/bash
DB_CREDENTIALS="$1"                         # HERE

mkdir -p "$PWD"/logs                        # HERE

sqlplus -silent "$DB_CREDENTIALS" <<EOFSQL  # HERE

Then:

I am trying to call my script from shell like bash-4.2$ dbcheck.sh

Obviously, your script take the connection parameters as first argument. you should invoke it like that:

bash-4.2$ dbcheck.sh username/password@my.oracle.host

You need to put correct credentials. Launch it like this:

dbcheck.sh username/password@database

Have you read the correct sqlplus syntax?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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