I'm struggling to understand how to use bash's trap
command correctly.
I have a script where I want to
- Do A
- Do B, which might fail
- Whether B succeeded or failed, do C
- If B failed, also do D
I think I get how to do 1-3. That'd look something like;
echo "I am A"
function B {
echo "I am C"
}
trap B EXIT
echo "I am B"
But where do I put D? If it goes inside function B, it executes whether or not B fails. If it's outside, it only happens on success. Am I using trap
wrong?