简体   繁体   中英

SQLPLUS not working if I pass variables to Shell Script

I wrote a sample shell script trying to execute a SQL file using SQLPLUS.

Here is my shell script

#!/bin/bash

username=$1
password=$2
TNS_entry=$3

schema_version=$(sqlplus -S '$username/$password@$TNS_entry' @schema.sql)
echo "Schema_number: $schema_version";

#./schema.sh username password '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST=SANDBOX.domain)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)
(SERVICE_NAME=orcl))'

After executing this I am getting below SQLPLUS error.

Schema_number: ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified


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


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

However if I run this manually I can connect to Oracle DB with out any issues, that concludes my SQLPLUS connection string has no issues. I am guessing shell is the culprit here, but couldn't figure out where it causing issue.

sqlplus username/password@'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX.domain)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)))' @schema.sql

SQL*Plus: Release 12.2.0.1.0 Production on Wed Mar 14 21:07:37 2018

Copyright (c) 1982, 2016, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

Can some one take a look at this and help me identify the issue.

Thanks and Regards

Saint

I got this working by editing as below. shell acting weird to parse quotes. Not sure this is ideal solution but this hack served the purpose.

#!/bin/bash

username=$1
password=$2
TNS_entry=$3

schema_version=$(sqlplus -S ''"$username'"/'"$password'"@'"$TNS_entry'"' @schema.sql)
echo "Schema_number: $schema_version";

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