Stop script execution, when we encounter error


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
Stop script execution, when we encounter error

Team,
Im new to shell scripting please help in below issue:
Considering scenario i have 1.sql,2.sql,3.sql and 4.sql . If my second script fails my loop should come out and should not execute 3.sql and 4.sql.

Here is my code.
Code:
#! /bin/bash
echo "exit" | user/password | grep Connected > /dev/null
if [ $? -eq 0 ]
then
   echo "Passed"
   while read file
   do
   echo "Executing file $file..."
   output=`user/password << EOF >> test.log
   @/home/$file`
   done < /home/master.sql
else
   echo "Failed"
   
fi


Last edited by joeyg; 1 Week Ago at 07:46 AM.. Reason: Highlight the range of code, and then place CodeTags. This manintains the code formatting.
# 2  
Old 1 Week Ago
From the thread title: "Stop script execution, when we encounter error", reading what you have posted, I am totally lost...
Quote:
Considering scenario i have 1.sql,2.sql,3.sql and 4.sql
OK where do i see them?
Quote:
If my second script fails my loop should come out and should not execute 3.sql and 4.sql.
So what is the script you posted?
Now even this script puzzles me as I cant see how it works...
Do you mind explaining what this first line is supposed to do?
Code:
echo "exit" | user/password | grep Connected > /dev/null

Then
lets say the following works:
Code:
if [ $? -eq 0 ]
then
   echo "Passed"
   while read file

Where is file set? ...
# 3  
Old 1 Week Ago
Quote:
Originally Posted by vbe
From the thread title: "Stop script execution, when we encounter error", reading what you have posted, I am totally lost...
OK where do i see them?

So what is the script you posted?
Now even this script puzzles me as I cant see how it works...
Do you mind explaining what this first line is supposed to do?
Code:
echo "exit" | user/password | grep Connected > /dev/null

Then
lets say the following works:
Code:
if [ $? -eq 0 ]
then
   echo "Passed"
   while read file

Where is file set? ...
Hi vbe,
Code:
   while read file
   do
   echo "Executing file $file..."
   output=`user/password << EOF >> test.log
   @/home/$file`
   done < /home/master.sql

Each time through this loop, the variable named file is set by the read command to contain the contents of a line read from the file /home/master.sql. (But, that would only be true if the here-document inside that while read loop was terminated. Oh, well.)

Having said that, I agree with you that I have no idea why finding the string Connected in the output from the utility named user/passwd when it reads a line from standard input that is exit would provide any reason for reading names of files from a file named /home/master.sql.

And, since the here-document terminator (i.e., EOF) does not appear anywhere in the code provided in post #1, this script can't possibly be working. I'm afraid preethi87 has failed to tell us several things that are important before we will be able to help debug the problem that was specified.
This User Gave Thanks to Don Cragun For This Post:
rbatte1 (5 Days Ago)
# 4  
Old 1 Week Ago
Sorry team , let me provide more info..
1.Firstly, it checks for the sql db connection. If connection is success it will execute script/file , if not it prints connection failed and wont execute any scripts.
2.Secondly all my files content resides in sqlfiles, whereas just filename is given in master.sql
3.Finally, if any of the script failed it should come out from the loop.
Eg: if 2.sql is failed with some error
Code:
SP2-0734: unknown command beginning "elect * fr..." - rest of line ignored.

remaining file/script should not get executed and come out of the loop.

Moderator's Comments:
Mod Comment Please do wrap your samples in CODE TAGS as per forum rules, see how I wrapped your given error, use [CODE]your samples/errors/codes/etc[/CODE] as an example.

Last edited by Don Cragun; 1 Week Ago at 07:44 AM.. Reason: Fix mismatched CODE/ICODE sample tags.
# 5  
Old 5 Days Ago
In your SQL, do you have WHENEVER ERROR EXIT .something early on? The syntax allows you to set an exit code for your script to pick up, something like this perhaps:-
Code:
:
:
deck=sql1.sql                           # The deck of SQL commands you want to run
   sqlplus / <<-EOSQL                   # Logs on with external authentication.  For remote connection use sqlplus <<-EOSQL and include the credentials in the here document
   WHENEVER SQLERROR EXIT 1;            # Set the equivalent of a shell trap
   @$deck ;                             # Run what you want to do.
   exit ;                               # Terminate sensibly
EOSQL                                   # End the here document.
RC=$?

if [ $RC -ne 0 ]
then
   echo "Take some action because ${deck} failed with return code ${RC}"
fi

It's not tested at all because I no longer have an Oracle database to connect to. I might even have the here document wrong, but you get the idea.

I inherently shy away from putting credentials on the same line as the sqlplus because that would make them visible to a ps command whilst your code is running. There are plenty of examples on the board on how to do it properly.




I hope that this helps,
Robin
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Error while script execution - 0403-029 there is not enough memory available now Adyan Faruqi AIX 6 08-11-2015 03:40 PM
To stop execution in cron azherkn3 Shell Programming and Scripting 2 01-30-2014 10:43 AM
Unmirrorvg problem encounter ckwan AIX 6 11-06-2013 09:21 PM
Syntax error in sh script execution Amit Kulkarni Shell Programming and Scripting 3 10-08-2011 02:56 PM
Execution of shell script returns error 127 RuchirP Shell Programming and Scripting 8 08-18-2011 08:33 AM
Stop execution of script if some condition fails vivek1489 Shell Programming and Scripting 1 05-02-2011 08:10 AM
Execution error with awk script ENG_MOHD Shell Programming and Scripting 2 04-14-2011 09:14 AM
how to stop execution of a script after one hour mady135 Shell Programming and Scripting 5 11-19-2010 08:26 AM
Stop load script if error found... msrahman Shell Programming and Scripting 5 11-04-2010 11:17 AM
How to stop a script running in remote server from local script mannepalli Shell Programming and Scripting 1 03-04-2009 07:18 PM
SolarisCron job perl script execution error sonu2die4 Shell Programming and Scripting 4 02-06-2009 04:22 PM
To Stop at error Prashantckc UNIX for Dummies Questions & Answers 1 11-24-2008 02:57 PM
stop execution of script with in the script dsdev_123 AIX 3 03-20-2008 11:57 AM
how to stop execution in for loop useless79 Programming 1 09-06-2007 10:54 PM
error during the execution of script surjyap Shell Programming and Scripting 1 08-24-2007 08:10 PM