Operating System - HP-UX
1829608 Members
1425 Online
109992 Solutions
New Discussion

Re: Error Codes in shell script.

 
sunil_34
Occasional Advisor

Error Codes in shell script.

Hi,

I wanted to know how can i trap the exact
error code and error message within the shell script for "mv " command to know what caused the actual problem ..ie was it file permission erro or no space on target or something else.

Are there any specific codes reflected and how can i trap them.

regards,

Sunil
7 REPLIES 7
Sanjay Kumar Suri
Honored Contributor

Re: Error Codes in shell script.

Each command returns a status when it terminates. If it is unsuccessful, it
returns a code which tells the shell to print an error message.

The default exit (no arguments) will exit the shell program with the status of
the last command executed. You can exit with a diferent exit status; see the
man pages for the exit statuses of each command. The usual exit
statuses are:

0 Success.
1 A built-in command failure.
2 A syntax error has occurred.
3 Signal received that is not trapped

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Manish Srivastava
Trusted Contributor

Re: Error Codes in shell script.

Hi,

You can do a set +x in the beginning of the script to see the script execution.
The error message coming out of mv will be sent to stderr so you can redirect the output to a file 2>file and see the error message.

apart from that I cannot think of anything now.

manish
blal
Frequent Advisor

Re: Error Codes in shell script.

Hi Sunil,

You can try to capture the standard o/p and err to a file .

mv file1 file2 >mv.log 2>&1

The log file should contain the errors message .

Regards
BL.
Live and let live.
sunil_34
Occasional Advisor

Re: Error Codes in shell script.

Yeah Sanjay i know about the error codes
like 0,1,2 but i believe their are some specifice error codes addressing some specific error messages viz. permission error
,space error etc.

How can i trap this erros in my shell scripts
to reflect the actual error.

regrds,
Sunil.
Shaikh Imran
Honored Contributor

Re: Error Codes in shell script.

Hi,
Don't know your script but,
echo $?
This will display the error code generated by the earlier command.
Maybe this will help you.

Regards,

I'll sleep when i am dead.
Manish Srivastava
Trusted Contributor

Re: Error Codes in shell script.

Hi Sunil,

The specific error which you can generate in a C program (syscall failure) like permission denied cannot be trapped in a shell script if the application (mv) is not givint it to the script.

manish
Gary L. Paveza, Jr.
Trusted Contributor

Re: Error Codes in shell script.

Sunil,

The only way this can be done is to trap the actual numeric value (0, 1, 2, etc) via the $? variable. I do this as follows:

mv >/dev/null 2>&1
rval=$?

Then I can test the value of rval (because $? is constantly changing).

Now, as to getting the actual error, you need to learn what the various return codes are (either via man pages or experimentation). To display a meaningful text message, you can do something like this:

case ${rval}
in
0) MESSAGE="Move successful";;
1) MESSAGE="Out of disk space";;
2) MESSAGE="File not found";;
*) MESSAGE="Unknown error - return code is ${rval}";;
esac

I've made up the messages for 1 and 2 (just to provide an example).

Over time, as you get "Unknown error - return code is ___" you just add on to the script and build up the case statement.