下面的脚本可以用来查看linux中每个组中的用户,它依次读取/etc/group每一行,对于其中每一行,都去遍历一遍/etc/passwd,如果group id匹配,打打印出组名和用户名。
#!/bin/sh # This script will print all groups and all users in each group. while read gline do groupname=`echo $gline | awk -F":" '{print $1}'` groupidgrp=`echo $gline | awk -F":" '{print $3}'` hasUser=0 while read uline do usename=`echo $uline | awk -F":" '{print $1}'` groupidpwd=`echo $uline | awk -F":" '{print $4}'` if [ "$groupidgrp" = "$groupidpwd" ]; then echo "$groupname:$usename" hasUser=1 fi done < /etc/passwd if [ $hasUser -eq 0 ]; then echo "Group $groupname has no user in it" fi done < /etc/group