Is there a linux bash command like the java try catch finally? Or does the linux shell always go on?
try {
`executeCommandWhichCanFail`
mv output
} catch {
mv log
} finally {
rm tmp
}
Is there a linux bash command like the java try catch finally? Or does the linux shell always go on?
try {
`executeCommandWhichCanFail`
mv output
} catch {
mv log
} finally {
rm tmp
}
Well, sort of:
Based on your example, it looks like you are trying to do something akin to always deleting a temporary file, regardless of how a script exits. In Bash to do this try the
trap
builtin command to trap theEXIT
signal.The
rm tmp
statement in thetrap
is always executed when the script exits, so the file "tmp" will always tried to be deleted.Installed traps can also be reset; a call to trap with only a signal name will reset the signal handler.
For more details, see the bash manual page:
man bash
mv
takes two parameters, so may be you really wanted to cat the output file's contents: