Skip to content

Instantly share code, notes, and snippets.

@smbambling
Created July 7, 2017 12:41
Show Gist options
  • Save smbambling/a738d4b2435f9212fc574aafe031e64c to your computer and use it in GitHub Desktop.
Save smbambling/a738d4b2435f9212fc574aafe031e64c to your computer and use it in GitHub Desktop.
Bash Script using a function to test to make sure commands are available. Allows to test for multiple versions of a command
#!/usr/bin/env bash
cmd_list=(
"virtualenv-2.7 virtualenv-2.5 virtualenv"
"docker docker.io"
)
# Function to check if referenced command exists
cmd_exists() {
if [ $# -eq 0 ]; then
echo 'WARNING: No command argument was passed to verify exists'
fi
#cmds=($(echo "${1}"))
cmds=($(printf '%s' "${1}"))
fail_counter=0
for cmd in "${cmds[@]}"; do
command -v "${cmd}" >&/dev/null # portable 'which'
rc=$?
if [ "${rc}" != "0" ]; then
fail_counter=$((fail_counter+1))
fi
done
if [ "${fail_counter}" -ge "${#cmds[@]}" ]; then
echo "Unable to find one of the required commands [${cmds[*]}] in your PATH"
return 1
fi
}
# Verify that referenced commands exist on the system
for cmd in "${cmd_list[@]}"; do
cmd_exists "${cmd[@]}"
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
return $?
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment