I have a bunch of generic cleanup code that needs to be done whenever a certain bash script exits, whether it exited normally or was interrupted. I figured I'd use the trap "..." EXIT
pseudosignal to achieve this.
In addition to the generic cleanup stuff, there's also one piece of specific cleanup that should only be done if the script completes normally. I thought I could trigger this by having the 'trap' block test a variable, like so:
#!/bin/bash
done=false;
trap "{
#generic cleanup code goes here.
if $done
then
#cleanup to be done only on completion goes here.
echo Test;
fi
}" EXIT
#main script goes here
done=true;
However, this doesn't work. Running the following code will never echo "Test". Adding an explicit exit
call after done=true;
doesn't change anything. What am I missing?
Cheers!