This is homework. I was writing a bash script that accepts a list of group names on the command line and produces a group report detailing the group id and the number of group members identified in /etc/passwd for each group. Output will look like:
Group Report
Group GID Count
------ ----- ------
css 301 439
faculty 300 78
To try and get this, I use several loops to go through the files and if statements as comparisons. Here is my code:
#!/bin/bash
FILENAME=/etc/passwd
FILENAME2=/etc/group
UIDMAX=1000
echo -e "\tGroup Report"
echo
echo "Group GID Count"
echo "------- ------- -------"
for g in "$@";
do
for groupname in [$(cut -d: -f1 $FILENAME2)];
do
if [ "$@" == "$groupname" ]
then
GID=[ cut -d: -f3 $FILENAME2 ]
for grpid in [$(cut -d: -f4 $FILENAME)];
do
Count=0
uidtest=[ cut -d: -f3 $FILENAME ]
if [[ "$uidtest" -gt "$UIDMAX" ]]
then
if [ $GID=$grpid ]
then
Count=`expr $Count + 1`
fi
fi
done
echo "$groupname $GID $Count"
fi
done
done
However, it iterates through the files and outputs most of the group ids. Here is the output if I run with the argument "adm" (a group that has one member on my machine (me)):
Group Report
Group GID Count
------ ----- ------
0
1
2
3
4
5
6
7
8
9
10
13
33
34
38
39
41
65534
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
1000
115
cut: ]: No such file or directory
adm 0
It does that several dozen times - exact same output of the numbers and the last two lines (error message, group name). I didn't bother posting all the rest of the output, as it seems to do it every time the for loop for grpid in [$(cut -d: -f4 $FILENAME)];
runs.
If anyone could point out what I'm doing wrong, that would be great.