Skip to content

Instantly share code, notes, and snippets.

@SamYaple
Last active May 16, 2022 09:26
Show Gist options
  • Save SamYaple/d51e02b7e3651d8a8482067ef60bc228 to your computer and use it in GitHub Desktop.
Save SamYaple/d51e02b7e3651d8a8482067ef60bc228 to your computer and use it in GitHub Desktop.
[sam@compy686 ~]$ cat t.sh
#!/bin/bash
# I normally set these options (with x being only sometimes)
# In this example,none of them are useful for catching your error
#set -euxo pipefail
badfunction() {
for i in $(find /error); do
echo $i
done
}
goodfunction() {
local path=$1
shift
result=( $(find "${path}" $@) )
exit_code=$?
if [[ ${exit_code} != 0 ]]; then
# This will exit immediately after the stderr output from find is
# displayed. The stderr from the find command is not captured above
exit ${exit_code}
fi
for i in ${result[@]}; do
echo ${i}
done
}
echo "this failure isnt caught:"
badfunction
echo "this works as intended:"
goodfunction /tmp -maxdepth 1 -type f
echo "this failure is caught:"
goodfunction /error
echo "this never runs"
[sam@compy686 ~]$ ./t.sh
this failure isnt caught:
find: ‘/error’: No such file or directory
this works as intended:
/tmp/helloitsmesam
/tmp/xauth-1000-_0
/tmp/.X0-lock
/tmp/serverauth.en6c5WhhmD
this failure is caught:
find: ‘/error’: No such file or directory
[sam@compy686 ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment