Hi djs515!
I'm not quite sure how you're using the
group and
passwd "databases" on your system.
If I were going to use a mix of
bash and
awk, I might do something like the following to get a list of users in a particular group. Given what might be a difference in the way
getent works on our systems, what I'm about to illustrate is put together to be run as
root, and just access the text files directly. You can interpret it to apply it in the context of however your
getent works. Since I don't necessarily have an
mis group, I'll just use the
users group, so I can have actually run the code and make sure it works on my system.
Code:
group_id=`egrep '^users:' /etc/group | cut -d: -f3-3`
group_users_list=`awk -F: ' /^[^:]+:[^:]+:[^:]+:100:/ { print $1 } ' < /etc/passwd`
To me, the number of times you've used
awk in your
bash script, makes me wonder if it might be better if more of the code were written in
awk directly. So I might finish out the rest of the script mostly in
awk. In which case, the result would be this:
Code:
group_id=`egrep '^users:' /etc/group | cut -d: -f3-3`
awk -F: '
BEGIN {
# Who command for awk to run.
who_command="who -u"
}
# Use user name as index of associate array, value of array element is not important.
/^[^:]+:[^:]+:[^:]+:100:/ { group_users_list[ $1 ]=1 }
END {
# Have awk run "who -u" command in a shell, and pipe output into awk variable.
while ( who_command | getline who_output_line )
{
split( who_output_line , who_line_fields , /[ \t]+/ ) ;
# Make use of "in" operator to check if user name is an index of associate array.
if ( ( who_line_fields[1] != "root" ) && ! ( who_line_fields[1] in group_users_list ) )
print who_line_fields[1] " " who_line_fields[7]
}
}
' < /etc/passwd > killthem.dat
HTH.