views:

37

answers:

1

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.

+4  A: 

I think you want the trap function, specifically:

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

trap error_func ERR

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

Matthew Flaschen