Verifying oracle connection from shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Verifying oracle connection from shell script
# 8  
Old 10-13-2010
Hi,

Check this post: https://www.unix.com/302462102-post4.html

Regards.
# 9  
Old 10-13-2010
Your problem is likely to be:

sqlplus << EOF
.
.
.
EOF -- the last one
is the trailer for a here document. It has to be in column #1 (on the left hand side
as far as you can go) in your script. You can use any word that is not a shell keyword or command for a delimiter
Us old guys use << ! .... ! for here doc delimiters

because ! is a single character that is never part of POSIX shell syntax. And we can't type.
# 10  
Old 10-13-2010
Log into an idle instance first, so you can set 'whenever sqlerror ... '

Code:
user=<username>
pass=<password>
 
sqlplus /nolog <<EOF
whenever sqlerror exit 99
connect $user/$pass
whenever sqlerror exit 98  -- change this so only the connect attempt uses '99' as the error code
-- do your normal sql stuff now
exit -- assuming a good login and no other error you log out normally
EOF
 
echo $? # will be 99 if bad login attempt, 98 if other sql error happened, or 0 if everything was fine.

# 11  
Old 10-13-2010
If possible I would use perl for this, it is perfect for this task.
Search the cpan pages for DBI and DBD::mysql, there are plenty of examples there.
# 12  
Old 10-14-2010
Hi,

I have written the below line in the .sh file

Code:
sqlplus $user_name/$password@$tns > c:/tmp/check_conn.txt

In the check_conn.txt file, I can see the redirected output as below :


Quote:
SQL*Plus: Release 9.2.0.8.0 - Production on Thu Oct 14 23:48:45 2010

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.


Connected to:
Oracle9i Release 9.2.0.8.0 - Production
JServer Release 9.2.0.8.0 - Production

SQL>

On executing the script, sqlplus gets open but the script gets hanged, it does not exit from sqlplus.

How to exit from sqlplus so that script can execute furhter ?

With Regards
# 13  
Old 10-14-2010
Try this,

Code:
#!/bin/sh

res=`sqlplus $user_name/$password@$tns <<ENDOFSQL
     set feed off
     set head off
     set pages 0
     set verify off
     select 'ok' from dual;
     exit
ENDOFSQL`

if [ "$res" == "ok"]
then
    echo "SUCCESS"
else
    echo "FAIL"
fi

# 14  
Old 10-14-2010
I would add one more check to the above.

Check below my example:
Code:
connStatus=`echo -e "WHENEVER SQLERROR EXIT 1\n SET HEAD OFF\n SET FEEDBACK OFF\n SELECT 'OK' FROM DUAL;\nEXIT" | sqlplus -S -L $user_name/$password@$tns`
sqlPlusRetCode=${?}

if [ "${connStatus}" != "OK" -o ${sqlPlusRetCode} -ne 0 ]
then
	echo "ERROR: Database connection not available."
	# exit 1
else
	echo "INFO: Success."
fi

Maybe you will not need: "echo -e", just "echo".

Regards!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script verify connection to Oracle if not successful

i have a script that connects to the oracle database and executes the query statements. it works fine and i would like to add some message to check if the connection to oracle is not successful. basically this is the code snippet: #!/bin/sh ... ... ... sqlplus -s username/password@dbName... (2 Replies)
Discussion started by: wtolentino
2 Replies

2. Shell Programming and Scripting

Help! Shell Script for Wifi Connection

Hi, I am quite new to shell scripting and was wondering if it was possible to automatically enter the username and password fields to a wifi-access-router and then ping it. I have most trouble with entering the username and password fields. Is that even possible? Also, what do I do if the... (1 Reply)
Discussion started by: Xyn
1 Replies

3. Shell Programming and Scripting

Connection with Teradata through shell Script

Hi All, As we are using SQLPLUS command to connect Oracle Database in unix: Like below syntax: sqlplus username/password@dbname << EOI SET HEADING OFF SPOOL MAX_DATE_VAL.txt select max(LAST_UPDT_DATE) from source_defect; SPOOL OFF here the result is stored in... (0 Replies)
Discussion started by: Shilpi Gupta
0 Replies

4. UNIX for Dummies Questions & Answers

How to fix connection to Oracle database through shell script?

I have a question regarding how to connect to Oracle Database through shell script. 1. If I want call a stored procedure on Linux server as this, it works. $sqlplus /nolog SQL*Plus: Release 12.1.0.2.0 Production on Fri Jun 12 14:49:49 2015 Copyright (c) 1982, 2014, Oracle. All rights... (2 Replies)
Discussion started by: duke0001
2 Replies

5. Shell Programming and Scripting

How to setup Oracle connection inside shell script?

Hi, We have Oracle Connection parameters set up in file name "TESTDB" at location /abc/etc.When I try to run my shell script it does not connect to Oracle database. Please let me know how "TESTDB" file can be called inside script. ####################### Setting the directories... (2 Replies)
Discussion started by: sandy162
2 Replies

6. Shell Programming and Scripting

Verifying if a file exist (script shell)

Hello, This is my code: nb_lignes=`wc -l $1 | cut -d " " -f1` for i in $(seq $(($nb_lignes - 1)) ) do machine=`head $1 -n $i | tail -1` machine1=`head $1 -n $nb_lignes | tail -1` ssh root@$machine -x " scp /home/file.txt root@$machine1:/home && rm -r /home/file.txt" done fi ... (2 Replies)
Discussion started by: chercheur857
2 Replies

7. Shell Programming and Scripting

How the user will provide the parameters for Oracle db connection in a shell script?

I'm new into unix. My question: is possible to write a shell script which will ask for the ORACLE_HOME, ORACLE_SID, USERNAME, PASSWORD to connect to Oracle db. In generally we have to set the ORACLE_HOME in .profile file. And after putting the 'sqlplus' command it asks for the username &... (6 Replies)
Discussion started by: priya001
6 Replies

8. Shell Programming and Scripting

oracle connection from shell script

Hi, For connecting to oracle my script is using the command sqlplus username/password@db_instance_name.For this to work i am setting ORACLE_HOME,TNS_ADMIN and ORACLE_SID in a seperate script.My question is,could we make a connection to oracle just by the command sqlplus... (4 Replies)
Discussion started by: DILEEP410
4 Replies

9. Shell Programming and Scripting

Verifying if the shell command executed or not?

Hi, I am working on a shell script that would verify if the mount command has executed or not. So , i have been doing this. mount /dev/cdrom /mnt/cdrom echo "$?" if ; then echo " Mount succesful" else echo " Mount unsuccessful" fi (3 Replies)
Discussion started by: eamani_sun
3 Replies

10. Shell Programming and Scripting

shell script- oracle connection problem

Hi all, I am having problem with a shell script. I have a couple of csv files. The shell script will do some operation on them, create a sql file which will then be called by sqlplus. The problem is to gracefully exit sqlplus at the end of every operation as I do not want to hang on to the... (4 Replies)
Discussion started by: nattynatty
4 Replies
Login or Register to Ask a Question