Friday, March 6, 2015

to test if a user is in a group

#!/bin/sh

# isUserInGroup user group
function isUserInGroup {
  # \b is word boundary.
  if id -Gn ${1} | grep &>/dev/null "\b${2}\b"; then
    return 0 # true. exit status code
  fi

  return 1 # false exit status code
}

if ! isUserInGroup apache dev; then
  echo "The user is not in the group"
else
  echo "The user is in the group"
fi

No comments: