Link to home
Start Free TrialLog in
Avatar of apunkabollywood
apunkabollywoodFlag for United States of America

asked on

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

Avatar of BlackAdderDK
BlackAdderDK

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
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.
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
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

ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial