rdm_
rdm_

Reputation: 67

verify user secondary groups

I have list of secondary group names eg group_1 group_2.. group_n and a username eg : user1

Now i need to do

  1. Make sure that all the groups are present

  2. Make sure no extra groups are present

I tried using id -nG user1 | grep <group_1> | grep <group_2> | .. | grep <group_ n> and evaluting the exitcode but that only makes sure that required groups are present.I'm not sure how to verify no extra groups (groups not in my list) are present.

Upvotes: 0

Views: 389

Answers (1)

Jahid
Jahid

Reputation: 22438

You can use a grep like this:

grep -oFf a_file_with_secondary_group_names_per_line

An example code how you can achieve what you want:

#!/bin/bash
user=username
file=file_with_secondary_groups
if [[ $(id -G "$user" |wc -w) == $(id -nG "$user" | grep -coFf "$file") ]]; then
  echo "*All groups are present"
  # i.e the number of group and the number of group matched is the same
  if [[ $(id -G "$user" |wc -w) == $(grep -co '.' "$file") ]]; then
    echo "*No extra groups"
    # i.e the number of groups and the number of groups in the file are same
  else
    echo "-Extra groups present"
  fi
else
  echo "-All groups are not present"
fi

Upvotes: 1

Related Questions