Skip to content

Instantly share code, notes, and snippets.

@Iristyle
Last active April 1, 2020 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Iristyle/de1be1415cd98eadd54c379bc52aa4a7 to your computer and use it in GitHub Desktop.
Save Iristyle/de1be1415cd98eadd54c379bc52aa4a7 to your computer and use it in GitHub Desktop.
POSIX shell error-handling with set -e
# ./test.sh
140529043738688:error:20087002:BIO routines:BIO_lookup:system lib:../crypto/bio/b_addr.c:694:Name or service not known
connect:errno=2
else branch - ignored error - output: foo
command output:
bar
140510615253056:error:20087002:BIO routines:BIO_lookup:system lib:../crypto/bio/b_addr.c:694:Name or service not known
connect:errno=2
entered loop - ignored error
bar
140520604876864:error:20087002:BIO routines:BIO_lookup:system lib:../crypto/bio/b_addr.c:694:Name or service not known
connect:errno=2
script complete
set -e
err() {
echo "$1"
printf "GET \foo\bar\n\n" | openssl s_client -connect puppet:8140 -ign_eof -quiet
return 1
}
output="none"
if output=$(err "foo") > ./output.txt; then
echo "if branch - ignored error - output: $output"
else
echo "else branch - ignored error - output: $output"
fi
exitloop=false
while ! (err "bar" || $exitloop); do
echo "entered loop - ignored error"
exitloop=true
done
echo "script complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment