Skip to content

Instantly share code, notes, and snippets.

@smbambling
Created February 11, 2016 13:43
Show Gist options
  • Save smbambling/5efac8ca0f20ac024215 to your computer and use it in GitHub Desktop.
Save smbambling/5efac8ca0f20ac024215 to your computer and use it in GitHub Desktop.
Check if a command called in a script exists.
# Reference Commands
cmd_list='usermod groupmod find chgrp getent id awk tr wc'
# Function to check if referenced command exists
cmd_exists() {
if [ $# -eq 0 ]; then
echo 'WARNING: No command argument was passed to verify exists'
fi
cmd=${1}
hash "${cmd}" >&/dev/null # portable 'which'
rc=$?
if [ "${rc}" != "0" ]; then
echo "${cmd} command not found" 1>&2
exit 1
#else
# echo "Found: ${cmd} at ${cmd_fullpath}"
fi
}
# Verify that referenced commands exist on the system
for cmd in ${cmd_list}; do
cmd_exists "$cmd"/
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment