Created
February 11, 2016 13:43
-
-
Save smbambling/5efac8ca0f20ac024215 to your computer and use it in GitHub Desktop.
Check if a command called in a script exists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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