Linux - members in group less than 99

Member of groups which have gid less than 99 ( root or any )  , I need a command that will give me output of the usere which are in this groups

Please help me with the command

apunkabollywoodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BlackAdderDKCommented:
Hi

The following script should do the trick:

#!/bin/bash
let userid=100

IFS="
"
for i in `cat /etc/passwd`
do
 let j=`echo $i | cut -f 3 -d ':' 2>/dev/null`

 if [ $j -le $userid ]
  then
    echo $i|awk -F":" '{ print "Username: " $1 "\t\tUID:" $3 }'
 fi
done
Regards
'Adder
0
Brad_WosmekCommented:
Assuming all users are in /etc/passwd you could use this grep:

grep -E .*\:.*\:.*\:\([0-9][0-9]\|[0-9]\)\:.*  /etc/passwd | cut -d: -f1

This expression will include gids <=99 not <99.  I suspect this is what you want though.
0
BlackAdderDKCommented:
Hi... again

Maybe I should read the whole ;)

This fits :)

#!/bin/bash
IFS="
"
for i in `cat /etc/passwd`
do
let j=`echo $i | cut -f 4 -d ':' 2>/dev/null`

if [ $j -le "99" ]; then
   echo $i|awk -F":" '{ print "Username: " $1 "\t\tGID:" $4 }'
fi
done

regards
'Adder
0
singhjagsirCommented:
Just one beautification to output, Idea/Script is same as BlackAdder

#!/bin/bash
IFS="
"
for i in `cat /etc/passwd`
do
let j=`echo $i | cut -f 4 -d ':' 2>/dev/null`

if [ $j -le "99" ]; then
   echo $i|awk -F":" '{ print "GID: " $4 "\t\tUsername: " $1 }'
fi
done |
 sort -n -k2

_J
#!/bin/bash
IFS="
"
for i in `cat /etc/passwd`
do
let j=`echo $i | cut -f 4 -d ':' 2>/dev/null`

if [ $j -le "99" ]; then
   echo $i|awk -F":" '{ print "GID: " $4 "\t\tUsername: " $1 }'
fi
done |
 sort -n -k2

Open in new window

0
simon3270Commented:
None of the above allows for auxiliary members in /etc/group.  In fact, it is better to start from /etc/group, which should list all groups, and find out which entries in /etc/passwd have each group as its main one.

The attached script reads through each group in /etc/group and, for each one 99 or less, lists out the /etc/passwd users with that group as the main group, and the auxiliary members listed in /etc/group.

If, for a given group ID, there are no users in /etc/passwd  with that group as their main group, and there are no auxiliary members, the output will just show the group ID followed by a ":".

If there are any users with that group as their main one, or there are auxiliary members, both of these are listed after the groupID and ":".

If a user has a particular group as its main group ID, and is also listed in the auxiliary group members in /etc/group for that group ID, it will be shown twice (see "root" for group ID 0 in the list below, or "bin" for group ID 1.)

The output from my Linux box starts:
0: root,sync,shutdown,halt,operator,admin,root,admin
1: bin,root,bin,daemon
2: daemon,root,bin,daemon
3: mcadmin,root,bin,adm
4: adm,root,adm,daemon
5:
6: root
7: lp,daemon,lp
8:
9:
10: root

awk -F: '$3 <= 99{print $3,$4}' /etc/group | sort -n | while read gid mems
do
  mainmem=$(awk -F: '$4 ~ /^'${gid}'$/{print $1}' /etc/passwd)
  mainmem=$(echo $mainmem | tr ' ' ',')
  if [ "X$mainmem" = "X" ]
  then
    if [ "X$mems" = "X" ]
    then
      echo ${gid}:
    else
      echo ${gid}: ${mems}
    fi
  else
    if [ "X$mems" = "X" ]
    then
      echo ${gid}: ${mainmem}
    else
      echo ${gid}: ${mainmem},${mems}
    fi
  fi
done

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.