Skip to content

Instantly share code, notes, and snippets.

@dialex
Last active September 11, 2019 08:08
Show Gist options
  • Save dialex/176c9ab22abcbda3deb5376a3e987012 to your computer and use it in GitHub Desktop.
Save dialex/176c9ab22abcbda3deb5376a3e987012 to your computer and use it in GitHub Desktop.
check if tool is installed before running command
check_file_exists () {
local what=${1:-}
if [[ ! -f "$what" ]]; then
>&2 echo "Error: Could not find '$what'" # stderr
exit 1
fi
}
check_tool_exists () {
local what=${1:-}
local how=${2:-brew install $what}
if ! command -v $what > /dev/null; then
>&2 echo "Error: '$what' command not found" # stderr
>&2 echo "Try: $how" # stderr
exit 1
fi
}
check_var_exists () {
local what=${1:-}
if [[ -z "${!what}" ]]; then
>&2 echo "Error: Did not find value for '$what'" # stderr
>&2 echo "Try: export $what=something" # stderr
exit 1
fi
}
# Example for command "jq"
if ! command -v jq > /dev/null; then
>&2 echo "Error: 'jq' command not found"
>&2 echo "See how to install for your system in https://stedolan.github.io/jq/download/"
exit 1
fi
# run your command HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment