8

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.

0
8

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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