What groups are shared by these users

Purpose: a simple Unix (anything similar needed on Windows?) utility that takes a list of users and reports on which groups they share in common.


A user dropped me an email asking if I knew of a way to take a list of users and to produce what groups they had in common.

(I just added leading spaces so the wiki wouldn't muck with it on display - bbh)

(Ok, now I added some comments on the code -marked with #bbh -

#! /usr/tcl84/bin/tclsh
# Name: common.groups
# Usage: common.groups user1 user2 user3 ...
# Example: common.groups larry darrell darrell2
#ccase  larry darrell darrell2
#dept   larry darrell darrell2
#pm     larry darrell
#win    larry darrell2
  
 
 #       Loop through the program arguments,
 #       for each user specified
 #               find their list of unix groups
 #               set an array element where the key is the group and the
 #                       value is a list of users in that group
 proc setarr { } {
    global argv set_of_groups
    foreach user $argv {
       foreach group [exec groups $user] { 
          if { ![info exists set_of_groups($group)] } {
             set set_of_groups($group) [list $user ]
           } else {
              #bbh: wrong: set set_of_groups($group) [list $set_of_groups($group) $user ]
              #bbh: using concat instead of list would give you beter results
              #bbh: otherwise the list is always 2 elements, the first being a list of 2 elements ...
              #bbh: but even better is lappend
              lappend set_of_groups($group) $user
 
           }
       }
    }
 }
 
 #       Loop through the array of existing groups
 #       If a group has a list with more than one member
 #               print it out
 #       Future: add sorting of the groups and sorting of the member names
 #bbh: the future is now, lsort is simple command, use it twice!

 proc prtarr { args } {
    global argv set_of_groups
    foreach group [lsort [array names set_of_groups ]] {
       if { [llength $set_of_groups($group)] > 1 } {
          puts [format "%s: %s\n" $group [lsort $set_of_groups($group)]]
       }
    }
 }

 setarr 
 prtarr