Not to display SQLPLUS prompt - linux

I am trying to connect with sqlplus using a bash script. When I execute the script below SQLPLUS banner displayed.
Below the script:
$ORACLE_HOME/bin/sqlplus "/ as sysdba" <<EOF
set echo off
set heading off
spool bind.txt
select * from DBMS_LOCK_ALLOCATED where name = '$uservar';
spool off
exit
EOF
Output of the script
oracle#DMOTA01:~/script> ./before_bind.sh
SQL*Plus: Release 11.2.0.3.0 Production on Wed Nov 27 11:54:01 2019
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> SQL> 2 3 4 Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
oracle#DMOTA01:~/script>
I don't want to display "SQL*Plus: Release 11............. Real Application Testing options" lines. How i can do it?

You need to add -S to sqlplus to switch it in silence mode:
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" <<EOF
set echo off
set heading off
spool bind.txt
select * from DBMS_LOCK_ALLOCATED where name = '$uservar';
spool off
exit
EOF

Related

Not able to connect to sqlplus from linux by passing credentials as parameter using getopts

I am trying to connect sqlplus by passing credentials as a parameter using getopts method-
Code:
while getopts ":o:s:t::i::p::f::" opt; do
case "$opt" in
o) uname=$OPTARG ;;
s) sname=$OPTARG ;;
t) password=$OPTARG ;;
i) ip=$OPTARG ;;
p) port=$OPTARG;;
f) sid=$OPTARG;;
?) echo "I Don't Know What $OPTARG it is"
esac
done
sqlplus -silent $uname/$password#$ip:$port/$sid
N when I am running this script as -
../a.sh -o otv4 -s OTV4 -t Password12356/$ -i 10.10.98.6 -p 1521 -f ortf
Its giving me Output something like this-
***
SQL*Plus: Release 18.0.0.0.0 - Production Version 18.3.0.0.0
Copyright (c) 1982, 2018, Oracle. All rights reserved.
Use SQL*Plus to execute SQL, PL/SQL and SQL*Plus 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 [ [<option>] [{logon | /nolog}] [<start>] ]
<option> is: [-AC] [-C <version>] [-F] [-L] [-M "<options>"] [-NOLOGINTIME]
[-R <level>] [-S]
-AC Enable Application Continuity.
-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
reprompting on error.
-M "<options>" Sets automatic HTML or CSV markup of output. The options
***
And not able to connect Sqlplus. Can someone please help me with this?!
Thanks in advance!
I test your script and it's working fine.
I find your password contain $, and it should be escaped with a backslash.
you can use the next solution to escape the password:
#escape password, store value in password_esc
printf -v password_esc "%q" "$password"
sqlplus -silent $uname/$password_esc#$ip:$port/$sid
#solution2 (more simpler)
password2=${password#Q}
sqlplus -silent $uname/$password2#$ip:$port/$sid
A demo online to show how password is escaped.

How to reliably return results from Oracle SQL*Plus to a variable in Korn shell?

Hello Unix/Linux geniae,
When an error occurs in the query, why does the shell variable (OPEN_MODE in this case) contain not only the expected SQL error message but also a listing of all the files in the directory?
I realise it has to do with the backticks and the fact that there is an asterisk in the error message but how can I get the error message back without the directory listing?
Run from command line:
$ OPEN_MODE=`sqlplus -s '/ as sysdba' <<'EOSQL' 2>/tmp/results.synchro.$$
> set headi off newpa none feedb off
> whenever sqlerror exit failure
> select open_mode from v$database;
> EOSQL
> `
$ echo $OPEN_MODE
select open_mode from v$database db_space_calculation.ksh get_emc_schedule_status.ksh set_emc_schedule_status.ksh synchro.ksh synchro_adsp_adso.cfg synchro_arp_arpt.cfg synchro_xbrl_xbrla.cfg synchro_xbrl_xbrlt.cfg test.sh ERROR at line 1: ORA-01034: ORACLE not available Process ID: 0 Session ID: 0 Serial number: 0
And run interactively from SQL*Plus to show how the error is normally presented...
$ sqlplus '/ as sysdba'
SQL*Plus: Release 12.1.0.2.0 Production on Mon Jan 15 20:31:04 2018
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to an idle instance.
SQL> set headi off newpa none feedb off
SQL> whenever sqlerror exit failure
SQL> select open_mode from v$database;
select open_mode from v$database
*
ERROR at line 1:
ORA-01034: ORACLE not available
Process ID: 0
Session ID: 0 Serial number: 0
Disconnected
I have also tried the "piping into read" method but doesn't work either.
sqlplus -s '/ as sysdba' <<'EOSQL' 2>/tmp/results.synchro.$$ | read OPEN_MODE
set headi off newpa none feedb off
whenever sqlerror exit failure
select open_mode from v$database;
EOSQL
Has to work in ksh. No bash please.
$() produces the same results as backticks. The only time I use backticks in ksh is when calling sqlplus! There are times when $() simply does not work. But that's for another day :-)

Running Linux Functions inside Sqlplus

Can I call user-created shell functions from inside Oracle SQLPLUS using the HOST command? If not, what's the best way to approach the problem?
Essentially, I want to run a shell file:
Shell commands
sqlplus
#file.sql
HOST mylinuxfunction...
#file2.sql
HOST anotherlinuxfunction..
exit
Shell commands
Thank you!
You can surely invoke HOST commands from SQLPlus scripts, but I imagine you are really asking whether you can use the return values from your linux functions in the rest of your SQLPlus scripts. And you also might be wanting to use results from the SQL queries in your linux functions.
If you do not need to pass SQL information to your linux functions, and do not need to access the results from the linux functions in the remainder of your SQL, then what you have will almost work. This would
date
sqlplus / << xxENDxx \
#file.sql
HOST mylinuxfunction...
#file2.sql
HOST anotherlinuxfunction..
exit
xxENDxx
date
Now if you want to get information from your linux function into SQL, you will have to use external tables; a lot of setup, but look here: https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:439619916584 and search for "but here is another interesting approach, available in 10.2.0.5 and up:"
If you want to pass information from Linux commands that are done before invoking SQLPlus into your SQL commands, that would be something like this, that inserts a row into the uptimes table with output from the uptime command that is stored in the BASH variable $UPTIMES:
#!/bin/bash
if [ "$1" = "" ]
then
echo Missing User ID parm
exit 1
else
USER_ID=$1
fi
read -p "Enter Your password for Oracle instance $ORACLE_SID for user $USER_ID: " PW
UPTIMES=`uptime | awk -F, '{print $3, $4, $5}' | awk '{printf "%2.2f,%2.2f,%2.2f\n", $3, $4, $5}'`
sqlplus /nolog << xxENDxx \
connect $USER_ID/$PW
insert into uptimes (date_stamp, one_min, five_min, fifteen_min) values (sysdate, $UPTIMES);
HOST ls -o uptimes.sh
--#file2.sql
select * from uptimes;
HOST du -sh .
exit
xxENDxx
date
Invoking the above gives this:
oracle. (/home/oracle/sql)
Linux> ./uptimes.sh mark.stewart
Enter Your password for Oracle instance ecs03 for user mark.stewart: xxxx
SQL*Plus: Release 12.1.0.2.0 Production on Thu Mar 17 20:09:36 2016
Dev:#> Connected.
Dev:MARK.STEWART#ecs03> Dev:MARK.STEWART#ecs03>
1 row created.
Dev:MARK.STEWART#ecs03> -rwxr-xr-x. 1 oracle 548 Mar 17 20:09 uptimes.sh
Dev:MARK.STEWART#ecs03> Dev:MARK.STEWART#ecs03>
DATE_STAM ONE_MIN FIVE_MIN FIFTEEN_MIN
--------- ---------- ---------- -----------
17-MAR-16 0 .01 .05
17-MAR-16 0 .01 .05
17-MAR-16 0 .01 .05
Dev:MARK.STEWART#ecs03> 146M .
Dev:MARK.STEWART#ecs03> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Thu Mar 17 20:09:36 CET 2016
oracle. (/home/oracle/sql)
Linux>

issue with environment...login to sqlplus

So i am trying to figure out a simple issue which i know is related to my environment setting....as you can see the oracle database (mcq) is up and running and when i login to the server everything is all set and i can login to the database with SQLPLUS.... But my issue comes in when i switch to bash mode by typing bash(default is csh, when things work)....when i switch to bash and unset everything and set ORACLE_SID, HOME, PATH I get into SQLPLUS but as you can see i get "Connected to an idle instance." message, which means i am not connecting into the database....
the reason why i am unsetting everything and wanting this to work is because of the script i am trying to write. I do not want to relay on the things that have already been set, as on some servers we have the env set for oracle and on some we dont...so if i get this to work this will work on both env...
hostname:oramcq 51> ps -ef|grep pmon
oramcq 3810 1 0 Aug14 ? 00:08:29 ora_pmon_mcq
oramcq 121914 121660 0 13:55 pts/0 00:00:00 grep pmon
hostname:oramcq 52>
hostname:oramcq 52> env | grep ORA
dbms_type=ORA
ORACLE_SID=mcq
ORACLE_BASE=/oracle
ORACLE_HOME=/oracle/mcq/112_64
hostname:oramcq 53>
hostname:oramcq 53> env | grep PATH
PATH=/oracle/mcq/112_64/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/sap/mcq/SYS/exe/uc/linuxx86_64:/usr/sap/mcq/SYS/exe/run:/oracle/mcq/11204:.
MODULEPATH=/usr/share/Modules/modulefiles:/etc/modulefiles
RSEC_SSFS_DATAPATH=/usr/sap/mcq/SYS/global/security/rsecssfs/data
RSEC_SSFS_KEYPATH=/usr/sap/mcq/SYS/global/security/rsecssfs/key
LD_LIBRARY_PATH=/usr/sap/mcq/SYS/exe/run:/usr/sap/mcq/SYS/exe/uc/linuxx86_64:/oracle/mcq/112_64/lib
hostname:oramcq 54>
hostname:oramcq 54> sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu Oct 22 13:56:11 2015
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning and Real Application Testing options
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning and Real Application Testing options
hostname:oramcq 55>
hostname:oramcq 55>
hostname:oramcq 55> bash
bash-4.1$
bash-4.1$ ORACLE_SID=""
bash-4.1$ ORACLE_HOME=""
bash-4.1$ PATH=/usr/sbin:/sbin:/bin:/sbin:/usr/bin
bash-4.1$ export ORACLE_SID=mcq
bash-4.1$ export ORACLE_HOME=/oracle/mcq/11204
bash-4.1$ export PATH=$ORACLE_HOME/bin:$PATH
bash-4.1$ which sqlplus
/oracle/mcq/11204/bin/sqlplus
bash-4.1$
bash-4.1$ env | grep ORA
dbms_type=ORA
ORACLE_SID=mcq
ORACLE_BASE=/oracle
ORACLE_HOME=/oracle/mcq/11204
bash-4.1$
bash-4.1$ env | grep PATH
RSEC_SSFS_DATAPATH=/usr/sap/mcq/SYS/global/security/rsecssfs/data
LD_LIBRARY_PATH=/usr/sap/mcq/SYS/exe/run:/usr/sap/mcq/SYS/exe/uc/linuxx86_64:/oracle/mcq/112_64/lib
PATH=/oracle/mcq/11204/bin:/usr/sbin:/sbin:/bin:/sbin:/usr/bin
MODULEPATH=/usr/share/Modules/modulefiles:/etc/modulefiles
RSEC_SSFS_KEYPATH=/usr/sap/mcq/SYS/global/security/rsecssfs/key
bash-4.1$
bash-4.1$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu Oct 22 13:57:20 2015
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.
SQL>

running ddl file through sh

I am having issue when running a ddl script file through sh on one of the servers but executes fine on another. The script is as below
sqlplus mgr/$1#$2 #export_all_tables_mgr.ddl
if [ $? != 0 ];
then
echo 'ERROR exporting MGR data, Refer to .CSV and .CTL files for detail.' | tee -a MGR_ExtractionLog.log
fi
It uses sqlplus to run the ddl file but one the rogue server, it would just connect to sql plus and won't do anything.
oracle#dbsdev55z2 $ export_all_tables_mgr.sh password servicename
SQL*Plus: Release 10.2.0.4.0 - Production on Thu Jan 24 08:39:18 2013
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production With the Partitioning, Data Mining and Real Application Testing options
SQL>
To mention here, I am using Oracle 10g here. export_all_tables_mgr.ddl file is as below
alter session set nls_date_format = 'DDMMYYYYHH24MISS';
#drop_table_temp_extraction_counts.ddl
#create_table_temp_extraction_counts.ddl
WHENEVER SQLERROR EXIT -1
WHENEVER OSERROR EXIT -1
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL MGR_ExtractionLog.log
SELECT TO_CHAR(SYSDATE, 'DD/MM/YYYY HH24:MI:SS'), '- Start extract process' FROM dual;
SPOOL OFF
define TableName=TABLE1
#ExportTable
define TableName=TABLE2
#ExportTable
SPOOL MGR_ExtractionLog.log append
SELECT TO_CHAR(SYSDATE, 'DD/MM/YYYY HH24:MI:SS'), '- End extract process' FROM dual;
SPOOL OFF
EXIT 0
I'm guessing that this is a shell problem, try adding the following as the first line of your shell script to tell it which interpreter to use:
#!/bin/sh

Resources