Skip to content

Instantly share code, notes, and snippets.

@Gabelbombe
Last active June 3, 2024 01:02
Show Gist options
  • Save Gabelbombe/47d3102dc722a111c2505adf98504ae9 to your computer and use it in GitHub Desktop.
Save Gabelbombe/47d3102dc722a111c2505adf98504ae9 to your computer and use it in GitHub Desktop.
Yell Die Try functions for bash...

Explanation

  • yell: print the script name and all arguments to stderr:
    • $0 is the path to the script
    • $* are all arguments.
    • >&2 means > redirect stdout to & pipe 2. pipe 1 would be stdout itself.
  • die does the same as yell, but exits with a non-0 exit status, which means "fail".
  • try uses the || (boolean OR), which only evaluates the right side if the left one didn’t fail.
#!/usr/bin/env bash -xe
function yell ()
{
echo "$0: $*" >&2
}
function die ()
{
yell "$*"; exit 111
}
function try ()
{
"$@" || die "cannot $*"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment