How to catch errors in a shell script ( multiple commands )?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to catch errors in a shell script ( multiple commands )?
# 1  
Old 05-04-2015
How to catch errors in a shell script ( multiple commands )?

Hi
I have a shell script like that
Code:
Main() {
DAY=$(date +"%d-%m-%Y")
TIME=$(date +"%T")

Command 1
Command 2
...
Command n
}

I would like to catch errors from all commands in Main() and write these errors into a file , something likes this:
Code:
Main
if < error >  
then
       echo $DAY $TIME "ERROR:" $ERROR >> error.log
else
       echo $DAY $TIME "OK" >> error.log
fi

and file error.log would look like this:
Code:
04-05-2015 06:00:00:OK
04-05-2015 07:00:00:OK
04-05-2015 08:00:00:ERROR : < error output ... >
04-05-2015 09:00:00:OK
04-05-2015 10:00:00:OK

Somebody help please SmilieSmilieSmilie
# 2  
Old 05-04-2015
You could use $? (exit status variable) creatively and log errors. Try it out.
# 3  
Old 05-04-2015
Quote:
Originally Posted by balajesuri
You could use $? (exit status variable) creatively and log errors. Try it out.
I know it but it seems not work with funtion, or maybe I missed something, so could you give me something more details
# 4  
Old 05-04-2015
If you want to know the exit status of the 3rd command in a function that contains more than 3 commands, you need to capture the exit status of the 3rd command in that function in that function. You can't get the exit status of anything but the last command executed unless you saved its exit status before you ran another command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check status of long running multiple curl commands in shell script

I am working on script. it reads a file which contains multiple lines Ex; curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=1 curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=2 curl --write-out %{http_code} --silent ... (2 Replies)
Discussion started by: oraclermanpt
2 Replies

2. Open Source

Help with writing Shell Script to automate process using multiple commands

Hello! Need help to write a Linux script that can be run from windows using command/Cygwin/any other way. I am new to scripting, actually i am trying to automate server health check like free disk space, memory along with few services status, if any services is not running then start services ,... (7 Replies)
Discussion started by: Sayed Ibrahim
7 Replies

3. Shell Programming and Scripting

Shell script to catch PL/SQL return values

I tried searching the forum for similar posts but its closed now. Would appreciate any help on this. I am trying to capture return value from a select query into a variable. DB is Oracle I am able to spool it to a file but I donot intend to use it. Here is my script that does not work ;) I... (27 Replies)
Discussion started by: monie2717
27 Replies

4. Shell Programming and Scripting

Not able to catch psftp errors

Hi, While running the psftp with the below code,it is able to connect and open the ftp connection and closed the connection. But my scriptfile Test.ftp,is having the code "cd directoryname" where the directoryname does not exists. In this case i should be able to catch the error,instead it is... (1 Reply)
Discussion started by: nsrihari
1 Replies

5. Shell Programming and Scripting

How to catch ENTER key inside the shell script?

Hi, I have a script in which i have to ask user to press the ENTER key to proceed further. can you please help me how can i achive this in my scripting? echo "All the executables builded Successfully " echo " Press Enter to Go Back to the Main Menu" ... (2 Replies)
Discussion started by: plaban.rout
2 Replies

6. Shell Programming and Scripting

Shell script to catch PL/SQL return values

Hello, I need some help from the experts on PL/SQL and Shell scripting. I need a shell script that runs a PL/SQL procedure and gets the values returned from the PL/SQL procedure into the shell variables. The PL/SQL procedure returns multiple values. I was able to assign a single return value... (1 Reply)
Discussion started by: Veera_Raghav
1 Replies

7. Linux

Help req for...shell script to catch "db2 connect" errors ...

Hello friends, Assume that, I am trying to execute a "db2 connect" command from Linux shell prompt via a shell script called "sample" sample db2 connect to bas39 $sample If the database is not present its should display a custom error message by catching the error message given by db2.... (1 Reply)
Discussion started by: frozensmilz
1 Replies

8. UNIX for Dummies Questions & Answers

catch unzip errors

Hi everybody, I'm new to linux world and I need your help!! I'm using vi to create a .sh script that process files moving them from a directory to another and unzipping a file. I need to catch errors while moving or unzipping files. For move command, I do: mv -f... (2 Replies)
Discussion started by: Laetitia
2 Replies

9. Shell Programming and Scripting

Shell script to call multiple java commands

Hi, I want to call multiple java commands through a .sh file. I have drafted one with my least knowledge. But its not working. Pls help. I am trying to run this from Sun Solaris (OS 5.10) server with 10g oracle database. echo \* starting script AUTORUN echo \* get the Monitor path... (4 Replies)
Discussion started by: vivekdn
4 Replies

10. UNIX for Dummies Questions & Answers

How to catch the rscyn errors?

Hi, In my code, I am running rsync, if any error comes, I have to wirte the error to temp file and I want to send this temp file content to specified email address, I am getting starnge outpout, can you pls help to solve this? My code is: tempfile=error.`date '+%m%d%Y_%H%M%SGMT'` rsync -az -e... (3 Replies)
Discussion started by: redlotus72
3 Replies
Login or Register to Ask a Question