Skip to content

Instantly share code, notes, and snippets.

@gsf
Last active December 29, 2015 03:29
Show Gist options
  • Select an option

  • Save gsf/7607995 to your computer and use it in GitHub Desktop.

Select an option

Save gsf/7607995 to your computer and use it in GitHub Desktop.
sh/bash try/catch on error message grep
#!/bin/sh
# Attempt command, printing all output to stdout, catching errors
# based on a regex, erroring out on all other errors
if output=`some-command 2>&1`
then
echo "$output"
else
if echo "$output" | grep 'regex string'
then
something --other --than bailing
else
echo "$output"
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment