Skip to content

Instantly share code, notes, and snippets.

@christopher-hopper
Last active December 28, 2015 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopher-hopper/7538559 to your computer and use it in GitHub Desktop.
Save christopher-hopper/7538559 to your computer and use it in GitHub Desktop.
Reset the groups for all users matching "user.name" to be primary group "developers", supplementary group "user.name"
#!/bin/bash
function usage() {
echo -e "Usage: " 1>&2;
echo -e "\t${0} [-h|-f|-i]" 1>&2;
echo -e "Description: " 1>&2;
echo -e "\tReset all users to have \`developer' as their primary group" 1>&2;
}
while getopts ":hfi" FLAG; do
case "${FLAG}" in
f)
ACTION=1
;;
h)
usage
exit 0
;;
i)
ACTION=0
;;
*)
echo "Invalid option: ${FLAG}"
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "${ACTION}" ]; then
gawk 'BEGIN { FS = ":" } /^\w+\.\w+/ { print "usermod -g developer -G " $1 " " $1 }' /etc/passwd
echo
echo "Use \`-f' to force reset of groups "
elif [ $ACTION -eq 0 ]; then
gawk 'BEGIN { FS = ":" } /^\w+\.\w+/ { system("groups " $1) }' /etc/passwd
echo
echo "Use \`-f' to force reset of groups "
elif [ $ACTION -eq 1 ]; then
gawk 'BEGIN { FS = ":" } /^\w+\.\w+/ { system("usermod -g developer -G " $1 " " $1) }' /etc/passwd
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment