Skip to content

Instantly share code, notes, and snippets.

@simbo1905
Created February 27, 2026 20:53
Show Gist options
  • Select an option

  • Save simbo1905/1b381d20b9632559f33931ead44cdd87 to your computer and use it in GitHub Desktop.

Select an option

Save simbo1905/1b381d20b9632559f33931ead44cdd87 to your computer and use it in GitHub Desktop.
Generate a new macOS group above a given GID and populate it with specified users
#!/usr/bin/env sh
#
# create_shared_group.sh GROUPNAME MINGID USER [USER ...]
#
# Example:
# ./create_shared_group.sh datameshsolutions 1234 simon consensussolutions
# POSIX sh: no fancy arrays, use positional parameters
if [ "$#" -lt 3 ]; then
printf 'Usage: %s GROUPNAME MINGID USER [USER ...]\n' "$0" 1>&2
exit 1
fi
GROUP_NAME="$1"
MIN_GID="$2"
shift 2
# Gather existing GIDs sorted numerically
EXISTING_GIDS=$(dscl . -list /Groups PrimaryGroupID | awk '{print $2}' | sort -n)
find_free_gid() {
CURRENT="$1"
# Loop forever until we find an unused GID
while :; do
# Check if CURRENT is in the EXISTING_GIDS list
echo "$EXISTING_GIDS" | grep -qx "$CURRENT" || {
printf '%s\n' "$CURRENT"
return
}
CURRENT=$((CURRENT + 1))
done
}
# WARNING THIS WAS CREATED BY LLM AFTER WE DID IT MANUALLY PLEASE USE WITH CAUTION MAY BE BROKEN
# Compute an available GID
NEW_GID=$(find_free_gid "$MIN_GID")
printf 'Using free GID: %s for group %s\n' "$NEW_GID" "$GROUP_NAME"
# Create the group and assign the chosen GID
sudo dscl . -create "/Groups/$GROUP_NAME"
sudo dscl . -create "/Groups/$GROUP_NAME" PrimaryGroupID "$NEW_GID"
sudo dscl . -create "/Groups/$GROUP_NAME" RealName "$GROUP_NAME"
# Add each user to the group
for user in "$@"; do
printf 'Adding user %s to group %s\n' "$user" "$GROUP_NAME"
sudo dscl . -append "/Groups/$GROUP_NAME" GroupMembership "$user"
done
printf 'Group %s created with GID %s and members: %s\n' \
"$GROUP_NAME" "$NEW_GID" "$*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment