1833803 Members
2346 Online
110063 Solutions
New Discussion

Re: error handling

 
harikrishna
Advisor

error handling

hi,
i would like to know any error handling mechanism in unix scripting i.e when i used the cat command if there as no file,then it throws an error.is there any methood to catch that error,if so please let me know.

THANKING YOU,

HARI KRISHNA
4 REPLIES 4
T G Manikandan
Honored Contributor

Re: error handling

you can check for whether the file exists and then open the file

like

if [ -f // ]
then
cat
else
echo file not present
fi
Sanjay Kumar Suri
Honored Contributor

Re: error handling

I have extracted following info from Shell's User Guide for HP9000 Servers:

When a function or a command terminates, it sets a flag indicating the status
of the termination. In other words, if the function or command was successful
in executing, it returns a value indicating its success. The values (or error
codes) normally used are listed in the exit section. These values are only
conventions; shell scripts normally use these conventions, but programs in
general do not.

When you execute a shell command incorrectly, you usually get an error
message. What usually happens is the shell command returns an error code.
If the error code is, say, 2, you will receive a message indicating a syntax error
has occurred.

You can return error codes from your shell programs and functions in two
ways. The exit statement can return any value you specify by using the
following format:

exit n

where n can be an integer from 0 to 255. You can return error codes from
functions by using the return statement:
return n

To check the return value of the last command you executed, you can use a
parameter called $?.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Elmar P. Kolkman
Honored Contributor

Re: error handling

It depends on what you want to do if an error occurs. A method might be to test the exit code of every command in your shell script by using a function you send your commands to which checks the exit code. But then you would have the same error handling for every command you run, which is sometimes not what you want.

You could also define a function that checks $? and is called after every command with possible error-exit codes. This is the way the SG package control scripts use, for instance. It would look something like this:

function test_exitcode {
ec=$?
if $ec
then
echo "Command returned non-null exitcode. This is not right. Exiting."
exit
fi
}

In your case, your script would start with the function definition and then:

cat
test_exitcode
Every problem has at least one solution. Only some solutions are harder to find.
Jean-Louis Phelix
Honored Contributor

Re: error handling

Hi,

You can also use 'trap ERR' or '||'. See man sh-posix.

#!/usr/bin/sh
trap "echo no file found" ERR
cat file 2>&-

or

#!/usr/bin/sh
cat file 2>&1 || echo no file found

Regards.
It works for me (© Bill McNAMARA ...)