error checking in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting error checking in bash
# 1  
Old 06-06-2006
error checking in bash

Could someone please advise on a good way to implement error checking in a script?
i am taking input from a file, iserting it's values into two commands and sending the output of each to seperate log files. i need to check those log files for for an occurance of 'error', stop the command if 'error' exists,. if it does not, then continue to next command. i have this so far....using mkdir for testing purposes.
...exerpt
for COMPONENT in `cat $1|awk '{print $1, $2}'`
do
printf "\n \n Creating components for $COMPONENT \n"
mkdir $COMPONENT >> ~/scripts/$COMPONENTERROR 2>&1
if grep error $COMPONENTERROR ! 1 || { echo "check error log"; exit 1; } fi

printf "Creating Island $2 \n"
mkdir $2 >> ~/scripts/$ISLANDERROR 2>&1
#if [stop_this -eq 1] then exit "STOP" else
#fi
done
....
or should i take another approach?
# 2  
Old 06-06-2006
Many commands and scripts return an exit code of non zero if an error occurred. This would be a good place to start checking but what you are attempting with grep is common when log files are the only means for communicating error messages.

Code:
mkdir $2 2> yourlog
if [[ $? -ne 0 ]]
then
    start looking for errors in yourlog
fi

This redirect stderr to yourlog and if the exit code from "mkdir" is non zero, the log is checked. You can also capture the error message into arrays or variables whatever you prefer. I prefer arrays.
Code:
set -A ERROR_ARRAY $(mkdir $2 2>&1)
for i in ${ERROR_ARRAY[@]}
do
    ...
done

# 3  
Old 06-06-2006
Put this at the top of every script you write.

LOG=/tmp/mylog

##################
function if_error
##################
{
if [[ $? -ne 0 ]]; then # check return code passed to function
print "$1 TIME:$TIME" | tee -a $LOG # if rc > 0 then print error msg and quit
exit $?
fi
}

Example below:

hostname
if_error "hostname command failed to return hostname"

chmod 775 /etc/hosts
if_error "chmoding /etc/hosts to 775 has failed"


-X
# 4  
Old 06-06-2006
Thanks guys, I really appreciate it. Both work for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash : Checking Large file for specific lines

Morning .. I have a file with approximately 1000 lines. I want to check that the file contains, for example, 100 lines. Something like whats given below is ugly. And even if I create a function I have to call it 100 times. I may need to look through multiple files at times. Is there a... (4 Replies)
Discussion started by: sumguy
4 Replies

2. Shell Programming and Scripting

Bash for checking and copy Daily files and send an email

Hello, Trying to get a bash for files received on daily basis and want to copy those files to different directory and send an email notification if file exists then send the file counts else Alert with no file received. FileName format: DailyTransaction_2015-03-09_ 200.csv before 200 their is... (1 Reply)
Discussion started by: krux_rap
1 Replies

3. Shell Programming and Scripting

checking first argument for tag in bash script

I have a bash script where I pass an argument ./chris.bash "\argv Test" I want to detect if the user supplied \argv at the start of the argument (3 Replies)
Discussion started by: kristinu
3 Replies

4. Homework & Coursework Questions

bash error checking problems[solved]

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to make a script called showtime that displays the current time for a given city. The problem is on... (6 Replies)
Discussion started by: kevin298
6 Replies

5. Shell Programming and Scripting

Multiple condition checking in bash

Hi All, I am trying to check if two variables have value assigned to it. i am doing it like if ] then echo "Please specify either single hostname or host file for the report" usage exit fi But its not working for it.Even i specify values for both variables it dont go... (6 Replies)
Discussion started by: kailash19
6 Replies

6. Shell Programming and Scripting

Error checking help.

I am currently wrapping up a assignment for one of my classes. I need help writing some error checking logic. The problems I am having are: keeping track of the records I have deleted and reported it back using echo. I have no idea how to do this. ensuring that line numbers fall... (1 Reply)
Discussion started by: Boltftw
1 Replies

7. Shell Programming and Scripting

bash: checking file size -solved

Hello I have srv RHEL5, file system UTDM (EMC DiskXtender Unix/Linux File System Manager 3.5 & EMC Centera). it all works under the scheme: have disk is formatted with a file system UTDM, drive open network - NFS, it write data, then migrate the data in the repository - EMC Centera. There are... (0 Replies)
Discussion started by: moskovets
0 Replies

8. Programming

Error Checking

Hey guys i am facing a problem in my sql statement. I am trying to check if there is such a value in the database. Code: string NewMovie = "ww"; string queryText ; queryText = "Select * from movie_info WHERE movie_title = '"+ NewTitle +"'"; ... (1 Reply)
Discussion started by: gregarion
1 Replies

9. Shell Programming and Scripting

bash if loop for checking multiple parameters

Hello, I've got next problem: I want to examine at the beginning of a script in an if loop that: 1. Is there 4 parameters given 2. If first state is true then: is there switches -e and -d? 3. At the end, how can i indentify them as variebles regardlees to its order. I was thinking like... (2 Replies)
Discussion started by: szittyafergeteg
2 Replies

10. Shell Programming and Scripting

Need help with error checking

I am creating a script that will automatically use sftp to connect to a site and download a file, extract the tar and then delete the tar file once completed. What I am stuck on is the error checking for this process. Here is the code so far: Now this works for me as is, but i need... (0 Replies)
Discussion started by: xianoth
0 Replies
Login or Register to Ask a Question