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
$ 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.


I think you want the trap function, specifically:

    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.


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.