Skip to content

Instantly share code, notes, and snippets.

@roktas
Created January 27, 2025 06:38
Show Gist options
  • Save roktas/e4307c4c8291c7df736c11f26eeb52f6 to your computer and use it in GitHub Desktop.
Save roktas/e4307c4c8291c7df736c11f26eeb52f6 to your computer and use it in GitHub Desktop.
Shell gothcas
#!/bin/bash
set -eu
foo() {
false
echo Expected to not reach here
}
fooer=foo
bar() {
(set -ex && $fooer)
}
test_bad() {
if bar; then
echo OK
else
echo NOTOK
fi
}
test_good() {
set +e
bar
# shellcheck disable=2181
if [[ $? -eq 0 ]]; then
echo OK
else
echo NOTOK
fi
set -e
}
test_cleanup() {
ensure_cleanup() {
declare -ag cleanup=("$@") # OR -rag for readonly
cleanup() {
set -- "${cleanup[@]}"
# CLEANUP CODE HERE
for arg; do
echo "-> $arg"
done
# echo $1
# err=3
echo cleaning up
ls sdfdsfsddf &>/dev/null
# exit 129
}
# shellcheck disable=2154
trap 'readonly err=$? && cleanup || exit $err' EXIT HUP INT QUIT TERM
}
ensure_cleanup 'fooooooooooooo' 'baaaaaaaaaaaa'
# trap 'readonly err=$? && cleanup || exit $err' EXIT HUP INT QUIT TERM
# trap 'cleanup' EXIT HUP INT QUIT TERM
exit 123
echo OK
}
test_cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment