exit on error - is it possible?

Hi , i have shell scripts that run the mysql directly by echoing and redirecting the output to the mysql logins. whenever the query executes successfully then the script runs fine and nothing has to be done there.

Now, when there is an error executing the query then it will generate the error something similar to the below one

 
ERROR 1146 at line 1: Table 'XXXXXX' doesn't exist

User time 0.01, System time 0.01
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 98, Physical pagefaults 0, Swaps 0
Blocks in 0 out 1, Messages in 3 out 6, Signals 0
Voluntary context switches 1, Involuntary context switches 6
--------------
select * from XXXXX
--------------

Bye

I know i could always redirect the output to a new temp file and grep for error. but i would prefer to do it using variables. is it possible???? since there are multiple lines so i am finiding it difficult to do it with variables

Any sort of help is appreciated.

Var_Name=`${ORACLE_HOME}/bin/sqlplus -s ORA_UID/ORA_PWD <<END
SQL statements
END`

This would capture your oracke logs in a Var_Name variable and then using grep or awk search you can achieve your functionality.

manish thanks for your reply.

i think i was not clear in my previous post. All that i am trying is to avoid using a file for the output.

for instance : my query execution would be something like this.

echo "select * from XXXXXX" |mysql ****** *******

Incase of error it will print this way

ERROR 1146 at line 1: Table 'XXXXXX' doesn't exist

User time 0.01, System time 0.01
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 98, Physical pagefaults 0, Swaps 0
Blocks in 0 out 1, Messages in 3 out 6, Signals 0
Voluntary context switches 1, Involuntary context switches 6
--------------
select * from XXXXX
--------------

Bye

i was trying to capture the above mentioned output in a variable and the check if the query has run successfully or not. the first line will always give error in case of error. do you know how do i capture multiple lines in a variable?????

Redirect fd 2 to 1 and fd 1 to /dev/null

See this demo..

[/tmp]$ ls -l tmp
ls: tmp: No such file or directory
[/tmp]$ ls -l unix
ls: unix: No such file or directory
[/tmp]$ ls -l unix tmp 2>&1 1>/dev/null
ls: unix: No such file or directory
ls: tmp: No such file or directory
[/tmp]$ STR=$(ls -l unix tmp 2>&1 1>/dev/null)
[/tmp]$ echo $STR
ls: unix: No such file or directory ls: tmp: No such file or directory
[/tmp]$ [[ $STR = *No* ]] && echo "Error" || echo "No error"
Error
[/tmp]$ ls -l /tmp 2>&1 1>/dev/null
[/tmp]$ STR=$(ls -l /tmp 2>&1 1>/dev/null)
[/tmp]$ echo $STR

[/tmp]$ [[ $STR = *No* ]] && echo "Error" || echo "No error"
No error
[/tmp]$ 

vino

Hi ,
use the code which i gave you below. This will capture all the messages from oracle in the variable "Var_Name" irrespective of single line or multiline. And then you can do your processing without writting to any file.

Hope this would help you

Regards,
Manish

thanks a ton!!! this working perfectly fine - now a question. how do i print the error message that was already generated??? ( I must admit i am still naive in shell scripting ) it is skipping the first line when i tried to print it?????

A simple echo will do. See the above.

but when ever i tried to print $STR with echo it is skipping the the initial 2-3 lines before printing - do you know why or how to fix this???? i am trying like this

[[ $STR = *ERROR* ]] && exit 1 || echo $STR

try this..
export New_STR = $STR
echo $New_STR