Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Using VMS DCL command scripts one can catch errors by using the on error command:

$ on error then goto MyErrorHandler
$ directory samiam.txt
$ delete x.txt
$ exit
$MyErrorHandler:
$ print "Something."

How does one do this in Linuxland?

In the example an error with directory or delete will cause control to go to MyErrorHandler.

share|improve this question

migrated from stackoverflow.com Feb 12 '11 at 20:20

This question came from our site for professional and enthusiast programmers.

1  
VMS, my old friend! –  Luc M Feb 15 '11 at 3:34

1 Answer 1

up vote 8 down vote accepted

I think you want the trap function, specifically:

error_func() 
{
    echo 'An error occurred!'
    exit 1
}

trap error_func ERR

Errors later will jump to the function. This is supported by at least bash, zsh, and ksh.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.