SQLPLUS not working if I pass variables to Shell Script - bash

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";

Related

SQL*Plus issues with connection to DB from Command Prompt

I am on SQL*Plus: Release 21.0.0.0.0, on a Windows machine.
In the command prompt, when I do
C:\users\TOMMY> sqlplus /nolog
I see this
SQL>
Here I connect to the Database using a connection string(EZ Connect format)
With Credentials,
username -> dddd
pwd -> wefm#vrev
host -> local
port -> 15210
ServiceName -> abcd.xysp.gthe.com
SQL> connect dddd/"wefm#vrev"#local:15210/abcd.xysp.gthe.com
And I see Output(after a few seconds),
Connected.
My issue begins when I decide to connect from Windows directly with sqlplus. Mentioned below
C:\users\TOMMY> sqlplus -S
dddd/"wefm#vrev"#local:15210/abcd.xysp.gthe.com
Results in an error, I am baffled. What am I missing
ERROR: ORA-12154: TNS:could not resolve the connect identifier
specified
I have also tried putting the connection string in single quotes (').
When I do that I get this whole thing printed out, which I assume is indicative of me passing the wrong args.
SQL*Plus: Release 21.0.0.0.0 - Production Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle. All rights reserved.
Use SQLPlus to execute SQL, PL/SQL and SQLPlus statements.
Usage 1: sqlplus -H | -V
-H Displays the SQL*Plus version and the
usage help.
-V Displays the SQL*Plus version.
Usage 2: sqlplus [ [] [{logon | /nolog}] [] ]
is: [-C ] [-F] [-L] [-M ""]
[-NOLOGINTIME]
[-R ] [-S]
-C <version> Sets the compatibility of affected commands to the
version specified by <version>. The version has
the form "x.y[.z]". For example, -C 10.2.0
-F This option improves performance in general. It changes
the default values settings.
See SQL*Plus User's Guide for the detailed settings.
-L Attempts to log on just once, instead of
prompting on error.
-M "<options>" Sets automatic HTML or CSV markup of output. The options
have the form:
{HTML html_options|CSV csv_options}
See SQL*Plus User's Guide for detailed HTML and CSV options.
-NOLOGINTIME Don't display Last Successful Login Time.
-R <level> Sets restricted mode to disable SQL*Plus commands
that interact with the file system. The level can
be 1, 2 or 3. The most restrictive is -R 3 which
disables all user commands interacting with the
file system.
-S Sets silent mode which suppresses the display of
the SQL*Plus banner, prompts, and echoing of
commands.
is: {[/][#<connect_identifier>] | / }
[AS {SYSDBA | SYSOPER | SYSASM | SYSBACKUP | SYSDG
| SYSKM | SYSRAC}] [EDITION=value]
Specifies the database account username, password and connect
identifier for the database connection. Without a connect
identifier, SQL*Plus connects to the default database.
The AS SYSDBA, AS SYSOPER, AS SYSASM, AS SYSBACKUP, AS SYSDG,
AS SYSKM and AS SYSRAC options are database administration privileges.
<connect_identifier> can be in the form of Net Service Name
or Easy Connect.
#[<net_service_name> | [[//]Host[:Port]/<service_name>] |
[[[protocol:]//]host1{,host12}[:port1]{,host2:port2}[/service_name]
[:server][/instance name][?[parameter name=value]
{&parameter name=value}]]]
<net_service_name> is a simple name for a service that resolves
to a connect descriptor.
Example: Connect to database using Net Service Name and the
database net service name is ORCL.
sqlplus username/mypassword#ORCL
Host specifies the host name or IP address of the database
server computer.
Port specifies the listening port on the database server.
<service name> specifies the service name of the database you
want to access.
Example: Connect to database using Easy Connect and the
Service name is ORCL.
sqlplus username/mypassword#Host/ORCL
The /NOLOG option starts SQL*Plus without connecting to a
database.
The EDITION specifies the value for Session Edition.
is: #|[.] [ ...]
Runs the specified SQL*Plus script from a web server (URL) or the
local file system (filename.ext) with specified parameters that
will be assigned to substitution variables in the script.
When SQLPlus starts, and after CONNECT commands, the site profile
(e.g. $ORACLE_HOME/sqlplus/admin/glogin.sql) and the user profile
(e.g. login.sql in the working directory) are run. The files may
contain SQLPlus commands.
Refer to the SQL*Plus User's Guide and Reference for more information.
I would like a solution that only involves using a connection string. I don't want to be using tns_ora file
Also without any manual intervention. I don't want to separately enter the password after I put in 'username#connect-identifier'.
Please ignore security related issues of passing in the password visibly in the cmd prompt. That is fine with me.
Solution powershell script test-connect2.ps1:
$username = "demin"
$password = '"test#test"'
$connect_string ="10.241.33.71:1521/DEV"
$log_file = "log.txt"
$list_scripts = "connect $username/$password#$connect_string"
$list_scripts = $list_scripts + "
select 1,sysdate from dual;
#file1.sql
#file2.sql"
$list_scripts | sqlplus -s /nolog >>$log_file
Run script:
C:\instantclient_21_3>powershell .\test-connect2.ps1
output log.txt
1 SYSDATE
---------- ---------
1 27-MAY-22
2 SYSDATE
---------- ---------
2 27-MAY-22
3 SYSDATE
---------- ---------
3 27-MAY-22
SQL*Plus: Release 11.2.0.4.0 Production on Fri May 27 06:34:41 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
SQL> alter user DEMIN identified by "test#test";
User altered.
SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
C:\>sqlplus demin/"test#test"#dev19
SQL*Plus: Release 11.2.0.4.0 Production on Fri May 27 06:35:14 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
ERROR:ORA-12154: TNS:could not resolve the connect identifier specified
Enter user-name:
C:\>sqlplus demin/\"test#test\"#dev19
SQL*Plus: Release 11.2.0.4.0 Production on Fri May 27 06:35:23 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
SQL> show user
USER is "DEMIN"
SQL>
C:\app\product\18.0.0\dbhomeXE\bin>sqlplus demin/\"test#test\"#dev19
SQL*Plus: Release 18.0.0.0.0 - Production on Fri May 27 10:28:18 2022
Version 18.4.0.0.0
Copyright (c) 1982, 2018, Oracle. All rights reserved.
Last Successful login time: Fri May 27 2022 06:35:29 +03:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.12.0.0.0
SQL>
C:\instantclient_19_15>sqlplus demin/\"test#test\"#dev19
SQL*Plus: Release 19.0.0.0.0 - Production on Fri May 27 11:23:35 2022
Version 19.15.0.0.0
Copyright (c) 1982, 2021, Oracle. All rights reserved.
Last Successful login time: Fri May 27 2022 10:31:49 +03:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.12.0.0.0
SQL>
C:\instantclient_21_3>sqlplus demin/\"test#test\"#dev19
SQL*Plus: Release 21.0.0.0.0 - Production on Fri May 27 11:18:08 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle. All rights reserved.
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified

Can't restart oracle database inside docker

So I'm trying to configure debezium on a Oracle database inside this docker image, according to the documentation described here.
At the "Preparing the Database" the first config I have to do is:
ORACLE_SID=ORACLCDB dbz_oracle sqlplus /nolog
CONNECT sys/top_secret AS SYSDBA
alter system set db_recovery_file_dest_size = 10G;
alter system set db_recovery_file_dest = '/opt/oracle/oradata/recovery_area' scope=spfile;
shutdown immediate
startup mount
alter database archivelog;
alter database open;
-- Should now "Database log mode: Archive Mode"
archive log list
exit;
The problem is that when I run shutdown immediate, the database dies and never returns. When I try to run:
docker exec -ti oracle sqlplus sys/top_secret#localhost:1521/ORCLCDB as SYSDBA
I get the error:
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Aug 10 07:44:23 2021
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
ERROR:
ORA-12514: TNS:listener does not currently know of service requested in connect
descriptor
I tried running this script inside the container, but still nothing. Anyone knows how could I start the database again?
There are 2 solutions for this
create a static listener configuration that has the database defined in the listener. Doing so enables startup through sqlnet like you tried.
run the script on the server using an account that is member of the local dba group. For startup use something like: ORAENV_ASK=NO; ORACLE_SID=ORCLCDB; source oraenv; “echo startup”| sqlplus / as sysdba.
This asumes that ORCLCDB is listed in /etc/oratab

Unable to login to Oracle Database from OSX

I've downloaded and installed Oracle Instant Client on my Mac Sierra.
I have also created the tnsnames.ora files and exported its path.
However, when I try to login to the remote Oracle server, I get the following error:
Mridulas-MacBook-Pro:~ mridulaprabhu$ sqlplus demo$security/password#ERPONWEB
SQL*Plus: Release 11.2.0.4.0 Production on Fri Aug 18 16:37:03 2017
Copyright (c) 1982, 2013, Oracle. All rights reserved.
ERROR:
ORA-01017: invalid username/password; logon denied
When my colleague connects to the same server using the same credentials on windows, he can login.
Someone told me it could be because of the characterset on my MAC. So I have set NLS_LANG to AMERICAN_AMERICA.US7ASCII but it still doesn't work.
How can I fix this?
OS X is a Unix variant, and you're running this from a shell. So the $ in your username is being interpreted as an environment variable.
If you do:
echo demo$security/password#ERPONWEB
you'll see that it only shows demo/password#ERPONWEB - unless you happen to have an envorinment variable called security, in which case that will be substituted.
You can escape the dollar symbol to stop that interpretation:
sqlplus demo\$security/password#ERPONWEB
or enclose the entire argument in single (not double) quotes:
sqlplus 'demo$security/password#ERPONWEB'

Sqlplus default login and password?

I just installed SQLPLUS via this Ubuntu documentation. What are the default login and password credentials? Following the documentation, I already tried sqlplus username/password#//dbhost:1521/SID I have investigated similar questions below, but none of the solutions have been successful for me.
https://askubuntu.com/questions/159939/how-to-install-sqlplus#
Oracle TNS: net service name is incorrecly specified
How come sqlplus not connecting?
I have tried testuser/password credentials
sqlplus64 username/password#//dbhost:1521/SID
SQL*Plus: Release 12.1.0.2.0 Production on Wed Oct 14 23:08:13 2015
Copyright (c) 1982, 2014, Oracle. All rights reserved.
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
Enter user-name: testuser
Enter password:
ERROR:
ORA-12162: TNS:net service name is incorrectly specified
Enter user-name:
How do I login, I'm sorry the comments don't make sense to me, could someone please write out an example, should I be 1sqlpus 64 username/password#//dbhost:1521/` ?

unable to import dump using "imp"

I am trying to import into oracle using imp command and the output of the command is as below.
invincible:/home/invincible# imp
Import: Release 10.2.0.1.0 - Production on Thu Aug 12 22:19:00 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Username: n_data
Password:
IMP-00058: ORACLE error 1034 encountered
ORA-01034: ORACLE not available
ORA-27123: unable to attach to shared memory segment
Linux Error: 13: Permission denied
IMP-00005: all allowable logon attempts failed
IMP-00000: Import terminated unsuccessfully
invincible:/home/invincible#
user name and password are correct(I am able to connect using sqldeveloper). I have granted dba access to n_data. All the environment variables are set (I ran oracle_env.sh before running env ). So what might be the problem? I am running oracle on debian.
Check this blog post: ORA-27123: unable to attach to shared memory segment.
It describes same error as you have and problem was caused with incorrectly set permissions on oracle executable.
This is excerpt from the above blog post:
Here the oracle file permission has
-rwxrwxr-x i.e. 775, but this file must have the permission -rwsr-s- -x
i.e. 6751
Change the permissions for oracle
file.
$ cd $ORACLE_HOME/bin
$ chmod 6751 oracle
$ ls -l oracle
-rwsr-s--x 1 oracle dba 119582976 Feb 3 2008 oracle
After changing the permissions on
oracle executable file, all the users
are now able to connect to the
database without any errors.
Read blog post for detailed information.
The error suggests it is trying to connect to an instance on the same host that isn't there. Generally I'd say the database isn't actually up and running, but if you can connect with SQL Developer, it suggests it is. But check you are on the correct machine.
Also try
echo :${ORACLE_SID}:
There may be some stray character in the SID. If you are using XE, you want to see :XE: (in upper case, not mixed or lower)
Have you tried
imp n_data/password#XE

Resources