Skip to content

Instantly share code, notes, and snippets.

@alanhogan
Created May 5, 2022 15:44
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 alanhogan/3c10248254da57b4530e32feefcb5f47 to your computer and use it in GitHub Desktop.
Save alanhogan/3c10248254da57b4530e32feefcb5f47 to your computer and use it in GitHub Desktop.
Fail on pattern match of output while showing output. zsh + macOS compatible
# Allow failing on regex pattern match
# See https://superuser.com/a/1717757/60337
failon() (
pattern="$1"
shift
[ "$#" -eq 0 ] && set cat
FAILON_STATUS="${FAILON_STATUS:-125}"
set -o pipefail
{ "$@" \
| "$@" >&3 \
| { grep -q -- "$pattern" && return "$FAILON_STATUS" || return 0; }
} 3>&1
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment