Hacker News
Bash scripts are brittle – simple error handling in bash
moebrowne
|next
[-]
porise
|root
|parent
|next
[-]
hks0
|next
|previous
[-]
Only if I could somehow mix `if` & `set -e`in a readable way... I wanted it to only capture errors of explicit `return 1` from bash functions, not from commands within those bash functions. But I guess I'm doing too much* of the job in bash now and it's getting messy.
vdm
|next
|previous
[-]
exit_code is not an environment variable?
https://www.gnu.org/software/bash/manual/html_node/Shell-Par...
hdaz0017
|next
|previous
[-]
writing in another language, how much more complexity do you want to introducing :)
I believe bash scripts can be anything you want them to be, if they are useful to you then happy day's.
if you hit performance issues or want to experiment else where cool.
error handling in ruby is almost impossible
error handling in python is wildly hit and miss
error handling in golang is rather annoying
error handling in powershell, good luck you will need it
error handling in perl ok i'll stop now you probably get the picture.
deafpolygon
|next
|previous
[-]
burnt-resistor
|previous
[-]
#!/usr/bin/env bash
set -eEuo pipefail # pipefail bash 4.2+, which is basically everything. There's a longer version for backwards compatibility, but it's rare.
die() {
echo >&2 "ERROR: $*"
exit 1
}
# e= & exit preserves the original exit code
# trap - ... prevents multiple cleanup() calls
# To only run on error instead of always, replace both EXITs with ERR
trap 'e=$?; trap - EXIT; cleanup; exit $e' EXIT
cleanup() {
: # Delete this line and place cleanup code here.
}
# Minimal script here.
# Use semantic-named functions.
# DRY.
# Don't use many levels of indentation.
# All ordinary variables must be defined before use, so use checks like [[ -n "${X-}" ]]
# All pipeline and bare command non-zero exit codes are errors.