Skip to content

Instantly share code, notes, and snippets.

@0xPr0xy
Created August 25, 2015 07:13
Show Gist options
  • Save 0xPr0xy/7810f08ad1b2fda16246 to your computer and use it in GitHub Desktop.
Save 0xPr0xy/7810f08ad1b2fda16246 to your computer and use it in GitHub Desktop.
check errors
check_errs()
{
# Function. Parameter 1 is the return code
# Para. 2 is text to display on failure.
if [ "${1}" -ne "0" ]; then
echo "ERROR # ${1} : ${2}"
# as a bonus, make our script exit with the right error code.
exit ${1}
fi
}
grep "^${1}:" /etc/passwd > /dev/null 2>&1
check_errs $? "User ${1} not found in /etc/passwd"
USERNAME=`grep "^${1}:" /etc/passwd|cut -d":" -f1`
check_errs $? "Cut returned an error"
echo "USERNAME: $USERNAME"
check_errs $? "echo returned an error - very strange!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment