Problem in connecting to db in a loop

Im trying to run a query in multiple db thro sqlplus in a loop written n shell script. whenevr there is a problem in connecting to db, the shell script abruptly exits. My requirement is , when db connection fails, the script should print the msg and contnue to connect to the next db mentioned in the file county_list Below is my code. Pls assist in getting it fixed.

cat county_list | while read details
do
county_code=`echo $details | cut -d '|' -f1`
county_name=`echo $details | cut -d '|' -f2`
usr_nm=`echo $details | cut -d '|' -f3`
passwd=`echo $details | cut -d '|' -f4`
domain=`echo $details | cut -d '|' -f5`

echo "Connecting to ${domain}"

$ORACLE_HOME/bin/sqlplus ${usr_nm}/${passwd}@${domain} @sqlquery.sql >> ${conn_dir}/${county_name}_conn.txt

if [ "$?" = "1" ]
then
echo "Connection problem"
continue
fi

echo "LOOP over for ${county_name}"
done

sqlquery.sql :

SET ECHO OFF
SET VERIFY OFF
SET FEEDBACK OFF
SET MARKUP HTML OFF SPOOL OFF
SET PAGESIZE 50000
SET LINESIZE 5000
SET HEADING ON
SET TRIMSPOOL ON
SET TRIMOUT ON
COLUMN clogname NEW_VALUE logname NOPRINT
SET COLSEP ,
SET SERVEROUTPUT ON
SPOOL /tmp/uat_temp.txt

DECLARE
   v1_sql_err_code   CHAR (10) := NULL;
   v1_success        BOOLEAN := TRUE;
 v1_count NUMBER(7,2);
BEGIN
   DBMS_OUTPUT.enable (NULL);
   v1_success := TRUE;

   SELECT COUNT(*) INTO v1_count
     FROM se_usr a
    
DBMS_OUTPUT.put_line (v1_count);
EXCEPTION
   WHEN OTHERS
   THEN
      v1_success := FALSE;
      v1_sql_err_code := SUBSTR (SQLERRM, 1, 10);
      DBMS_OUTPUT.put_line (
         'EXCEPTION CAUGHT. SQL Error Code: ' || v1_sql_err_code);
END;
/
SPOOL OFF

OUTPUT ( for incorrect login detail in the first db connection. )

/home/dev/uat_report>./UAT_usr_login_report.sh
Connecting to ALA01r
Connection problem
/home/dev/uat_report>
oifs="$IFS"
while IFS="|" read county_code county_name usr_nm passwd domain xstr
do
        IFS="$oifs"
        echo "Connecting to ${domain}"

        $ORACLE_HOME/bin/sqlplus ${usr_nm}/${passwd}@${domain} @sqlquery.sql >> ${conn_dir}/${county_name}_conn.txt 2>error.txt
        stat=$?
        [ "$stat" = 0 ] && echo "OK" && break  # or continue ?

        echo "LOOP over for ${county_name}"
        cat error.txt
done < country_list

@kshji oifs=$IFS and IFS=$oifs are not needed here and can be left out, since in IFS="|" read IFS is set local to the read command and does not affect the global IFS value.

Thank you both of you for your inputs.

sorry to mention that the solution you gave is not working for me.

I tried using continue command in two ways, one as mentioned by you, and the other by searching for any ERROR in the sqlplus output file.

Both continue statements doesn seem to work.

I have included some echo statements for your reference.


while IFS="|" read county_code county_name usr_nm passwd domain
do
 echo "Connecting to county $county_code"
$ORACLE_HOME/bin/sqlplus ${usr_nm}/${passwd}@${domain} @sqlquery.sql >> ${conn_dir}/${county_name}_conn.txt 2>error.txt

stat=$?
echo "PRINTING STATUS $stat"
[ "$stat" != 0 ] && continue

echo "SEARCHING FOR ERROR IN LOG FILE"
cnt=`grep "ERROR" ${conn_dir}/${county_name}_conn.txt|wc -l`
if [ $cnt -ne 0 ]
then
     echo "pattern match found - next continue command will be executed"
    continue
else
     echo "no match with the pattern"
fi

echo "OUT OF IF LOOP"
echo ""
count=`sed -n "4 p" /tmp/uat_temp.txt | sed 's/ //g'`

echo "${county_code},${county_name},${count}" >> $rep_dir/UAT_Report_${CUR_DATE}.CSV

rm -f /tmp/uat_temp.txt

done < county_list

echo "OUT OF WHILE LOOP"

OUTPUT:

/home/devdbg2/uat_report>./UAT_usr_login_report.sh
Connecting to county 1
PRINTING STATUS 0
SEARCHING FOR ERROR IN LOG FILE
no match with the pattern
OUT OF IF LOOP

Connecting to county 7
PRINTING STATUS 0
SEARCHING FOR ERROR IN LOG FILE
pattern match found - next continue command will be executed
OUT OF WHILE LOOP

sqlplus connection output file:

/home/devdbg2/uat_report/DB_conn>cat ContraCosta_conn.txt

SQL*Plus: Release 11.2.0.3.0 Production on Mon Apr 11 02:22:41 2016

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

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


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

county_list file :


/home/devdbg2/uat_report>cat county_list
1|Alameda|abc|def|ALA01r
7|ContraCosta|abc|xyz|CC07r
10|Fresno|abc|def|FR10r

Here in county_list, passwrd for ContraCosta is incorrect. in that case, my script should print an error message and continue to run the query for the next county ie Fresno.

But, my script exits whenever there is an ORA error printing the lines followed by the sqlplus commands and exit at the current iteration if the while loop.

Kindly advise where I am going wrong.

Thanks in advance.!

Is your county file a correct *nix text file, i.e. terminated with a <new line> char?

when i give a newline at the end of county_list file, the output is as follows:

/home/devdbg2/uat_report>./UAT_usr_login_report.sh
Connecting to county 1
PRINTING STATUS 0
SEARCHING FOR ERROR IN LOG FILE
no match with the pattern
OUT OF IF LOOP

Connecting to county 7
PRINTING STATUS 1
OUT OF WHILE LOOP

exit code is captured as 1 but the while loop exits which should not happen

Run the script with the -vx options set AND post the resulting log.

Here it is:

done <county_list
+ 0< county_list
+ read county_code county_name usr_nm passwd domain
+ IFS=|
+ echo Connecting to county 1
Connecting to county 1
+ /app/oracle/product/11.2.0.3/client/bin/sqlplus abc/def@ALA01 @sqlquery.sql
+ 1>> /home/devdbg2/uat_report/DB_conn/Alameda_conn.txt 2> error.txt
+ stat=0
+ echo PRINTING STATUS 0
PRINTING STATUS 0
+ [ 0 != 0 ]
+ echo SEARCHING FOR ERROR IN LOG FILE
SEARCHING FOR ERROR IN LOG FILE
+ + grep ERROR /home/devdbg2/uat_report/DB_conn/Alameda_conn.txt
+ wc -l
cnt=0
+ [ 0 -ne 0 ]
+ echo no match with the pattern
no match with the pattern
+ echo OUT OF IF LOOP
OUT OF IF LOOP
+ echo

+ + sed -n 4 p /tmp/uat_temp.txt
+ sed s/ //g
count=3
+ echo 1,Alameda,3
+ 1>> /home/devdbg2/uat_report/Report/UAT_Report_04112016.CSV
+ rm -f /tmp/uat_temp.txt
+ read county_code county_name usr_nm passwd domain
+ IFS=|
+ echo Connecting to county 7
Connecting to county 7
+ /app/oracle/product/11.2.0.3/client/bin/sqlplus abc/xyz@CC07 @sqlquery.sql
+ 1>> /home/devdbg2/uat_report/DB_conn/ContraCosta_conn.txt 2> error.txt
+ stat=1
+ echo PRINTING STATUS 1
PRINTING STATUS 1
+ [ 1 != 0 ]
+ continue
+ read county_code county_name usr_nm passwd domain
+ IFS=|

echo "OUT OF WHILE LOOP"
+ echo OUT OF WHILE LOOP
OUT OF WHILE LOOP

OK, post a hex dump ( od -Ax -tx1z file ) of your county file.

the command doesn work

/home/devdbg2/uat_report>od -Ax -tx1z county_list
od: Invalid argument for -t option
/home/devdbg2/uat_report>

You are not telling me that you can't read the od man page to correct an obviously version dependant command option incompatibility?