2

I want to write a shell script that generates a list of users. for each user display the username and ID and all groups the user is a member of

it means like GROUPS command but for all users and with their id I used cat -d: -f1,3 /etc/passwd to show the users with their id but I don't know how can I use the groups for each line

would you please help me to write this script thanks

1
  • example :" Ubuntu:1000: ubuntu adm dialout cdrom plugdv lpadmin admin " Nov 22 '14 at 12:29
3

Use this script:

#! /bin/bash
#
for i in $(cat /etc/passwd  | cut -d: -f1); do
   echo -n $i ": "
   grep $i /etc/group | cut -d: -f1 | tr "\n" " "
   echo
done

It will list all users in the system (included system) and print the list of groups near them. With a trivial modification you can print the numeric id too.

2
  • example :" Ubuntu:1000: ubuntu adm dialout cdrom plugdv lpadmin admin " Nov 22 '14 at 12:25
  • ShowUserNGroups now in ~/bin directory! I'm going to analyse it more carefully in the morning when I'm not so "tired". ;) (upvoted too!)
    – Fabby
    Nov 22 '14 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.