Handling Errors in Shell Scripts


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Handling Errors in Shell Scripts
# 1  
Old 12-24-2006
Handling Errors in Shell Scripts

I have a shell script, which calls a load script to load a database. How can i handle errors in Unix(similar to 'error level' in Batch scripts)? I am trying to use 'mailx' to send a Success/failure message based on the error level returned by the load script.

I have already used an error log file to find out success or failure of the load script, but sometimes there will be some errors even though it does not create an error log file, so its not 100% accurate.

Thanks
# 2  
Old 12-24-2006
In bash the return code of a prog is in $?. Usually exit status 0 = success and 1 = failure.

If we look in the manpage for grep we find:

Normally, exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred, unless the -q or --quiet or --silent option is used and a selected line is found.


Based on that, an example of usage:

---------------------------
#!/bin/bash

/bin/grep search_string file

if [ $? -eq 1 ];then
echo "String not found."
fi
---------------------------

Hope that gets you on your way.
Kent
# 3  
Old 12-24-2006
That worked thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with shell script handling processes

Hello I have a file which has around 120 lines of commands. I am trying to write a shell script like which reads the 'command' file and executes line by line with some additional (common argument) with maximum 6 commands active at a time. Each of these commands when executed takes time... (5 Replies)
Discussion started by: JackyShane_36
5 Replies

2. Shell Programming and Scripting

Handling scripts in two different servers

Hi , My Script work as below 1- On server 1 execute script1.sh , through this script one parameter file is generated as file.txt this is to transfer on server 2 2- After reaching on server2 other shell script script2.sh execute using parameter file file.txt This generate file... (1 Reply)
Discussion started by: kaushik02018
1 Replies

3. Shell Programming and Scripting

Handling tape errors in script

AIX 7.1 Here's the script #!/bin/ksh find . -print > filelist.txt backup -ivqf/dev/rmt0 < filelist.txt > backup.log if ; then echo "Backup to tape failed!" >> backup.log else echo "Backup to tape successfull!" >> backup.log fi mail -v -s "Backup report" maillist < backup.log ... (4 Replies)
Discussion started by: landog
4 Replies

4. Shell Programming and Scripting

Strange errors with shell scripts..plzz help

Hi all... Please help me with this questions. I am not good at shell scripting. 1) It seems like there are many ways that we can run the shell scripts. what is the difference between each one? i)./shell_script.sh ii).(space)./ shell_script.sh iii)sh shell_script.sh ... (3 Replies)
Discussion started by: sanskumar2003
3 Replies

5. Shell Programming and Scripting

Handling parameters in Shell Functions

Hi, Please help me with the below situation where I have to handle the parameters passed to a function in a unique way. Below is the code, which I am trying to execute. I basically want to pass the parameter to a function, where I am trying to get user input into array(s). I want to name... (7 Replies)
Discussion started by: bharath.gct
7 Replies

6. Shell Programming and Scripting

Advanced error handling in shell scripts

Hi all I've got a question regarding error handling in shell scripts. My background is mainly object oriented programming languages, but for a year or so I've been doing more and more (bash) shell scripting (which I quite enjoy by the way). To handle errors in my scripts I... (3 Replies)
Discussion started by: script_man
3 Replies

7. Shell Programming and Scripting

Interrupt handling in k shell

Hi All, Is interrupt handling possible in k shell? Say if the user press CTRL-C or CTRl-D,I want to perform a particular action before terminating? Thnaks! (2 Replies)
Discussion started by: prasperl
2 Replies

8. Shell Programming and Scripting

How to find out Errors in FTP Scripts

Hi Please let me know is there any way through which I can find out errors & do debugging in FTP scripts. Thanks Sourabh TCS (1 Reply)
Discussion started by: sourabhshakya
1 Replies

9. UNIX for Advanced & Expert Users

Error Handling in Korn Shell scripts

Hi, I am using few ISQL statements to update and delete from a few tables in sybase, now i want to roll back the transaction when any of the statements fail.How i can i capture these errors in the shell scripts.Please advise. Thanks, Gopi (4 Replies)
Discussion started by: bhgopi
4 Replies

10. Shell Programming and Scripting

Null handling in scripts

Hi, I face some problem with handling of nulls. I declare a variable - say i - and intialise to 0. Later I read it from console, wherein if I dont give any variable and press return key, I get this error: "0403-004 Specify a parameter with this command" Is there anyway to handle this error? ... (3 Replies)
Discussion started by: mohanprabu
3 Replies
Login or Register to Ask a Question