exit on error - is it possible?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exit on error - is it possible?
# 1  
Old 01-03-2006
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

Code:
 
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.
# 2  
Old 01-03-2006
try out this

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.
# 3  
Old 01-05-2006
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.
Code:
echo "select * from XXXXXX" |mysql ****** *******

Incase of error it will print this way


Code:
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?????
# 4  
Old 01-05-2006
Redirect fd 2 to 1 and fd 1 to /dev/null

See this demo..
Code:
[/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

Last edited by vino; 01-05-2006 at 06:20 AM..
# 5  
Old 01-05-2006
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
# 6  
Old 01-05-2006
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?????

Last edited by ahmedwaseem2000; 01-05-2006 at 08:22 AM..
# 7  
Old 01-05-2006
Quote:
Originally Posted by vino
[/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
A simple echo will do. See the above.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Detect error and exit

Hi, I am using Jenkins and have integrated JMeter for testing. JMeter is generating a file with results properly using shell script. I have to validate the results and exit the shell with non-zero so my jenkins job gets failed. I tried multiple ways to grep the output file and read the results... (2 Replies)
Discussion started by: sitaram
2 Replies

2. Shell Programming and Scripting

FTP exit and error codes

I have a script which connects to a remote server via FTP and gets some file from there. #!/bin/bash /usr/bin/ftp -n remote.hostname.com <<EOF quote USER user123 quote PASS password123 lcd /dir1/dir2/dir3 cd / mget file_pattern* close bye EOF rc=$? if ] ... (7 Replies)
Discussion started by: dhruuv369
7 Replies

3. Shell Programming and Scripting

How to exit a script with error ?

Hi, I have a script ABC which calls another script XYZ. Function of XYZ is to ftp a file from one server to another. code for ABC: #!/bin/ksh PATH=/usr/bin home/user/xyz "$@" exit $? ~ code for xyz: #!/bin/ksh HOSTNAME=$1 SRCNAME=$2 DSTNAME=$3 (4 Replies)
Discussion started by: Salil Gupta
4 Replies

4. Shell Programming and Scripting

If Error then Exit

HI I am just using cd Command and i want exit if error. Ex. cd /hp/pp/0720 If above folder in not available then stop the script Folder is change every day Thanks (3 Replies)
Discussion started by: asavaliya
3 Replies

5. UNIX for Dummies Questions & Answers

aCC exit error

Hi guys I would just like to know if aCC supports the command exit(0); in c++? I am always getting the error below: Error 328: "ac5.C", line 37 # Function 'exit' has not been defined yet; cannot call. exit(0); ^^^^ Anyone had this problem? Thanks! (2 Replies)
Discussion started by: khestoi
2 Replies

6. Shell Programming and Scripting

exit out of a while loop with error

im running a while loop as a file watcher, with incremental counter on the retries..however when the retries reach it's limit i want it exit and echo and error and stop the batch. Im not sure the code i have will do that already... Here is what i have that works: #!/usr/bin/ksh count=0... (2 Replies)
Discussion started by: sigh2010
2 Replies

7. Linux

tar error exit delayed form pervious error

Hi when use "tar cpvzf /dev/st0 --exclude=/proc --exclude-/lost+found --exclude=/mnt --exclude=/media --exclude=/sys /" to tape, get the following message "tar: error exit delayed form pervious error", What is the mean ? Please suggest some solution for these errors. thx (1 Reply)
Discussion started by: chayato
1 Replies

8. UNIX for Advanced & Expert Users

SAM Error non-zero exit status

Our machines HP-UX 10.20 Ace, Attempting to run various areas of SAM such as Backup and Restore, Printers and Plotters any areas of Time, etc give errors like the following: unexpected exit: process /usr/sam/lbin/samx/ -C -p 1358 -s br_sa_bdevs /usr/sam/lib///br.ui exited with a non-zero exit... (0 Replies)
Discussion started by: real-chess
0 Replies

9. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies
Login or Register to Ask a Question