Need help with bash script

I have the following code for a bash script:

passfile=/etc/passwd
for i in `grep 2000 $passfile`
do
fullname=`echo $i | cut -d: -f5`
useracc=`echo $i | cut -d: -f1`
userid=`echo $i | cut -d: -f3`
lastcom=`last | grep $useracc | head -1 | cut -c40-55`
echo "Full Name: " $fullname "User Account: " $useracc "User ID: " $userid "Last time logged in: " $lastcom
done

Which then gives me the following output:

Full Name:  Abel User Account:  asanchez User ID:  1001 Last time logged in:  Thu Feb 25 11:24
Full Name:  User Account:  Sanchez User ID:  /bin/bash Last time logged in:
Full Name:  Cody User Account:  banegasc User ID:  1002 Last time logged in:  Mon Feb 22 00:03
Full Name:  B. User Account:  B. User ID:  B. Last time logged in:
Full Name:  User Account:  Banegas User ID:  /bin/bash Last time logged in:
Full Name:  Selayoa User Account:  scloud User ID:  1003 Last time logged in:  Thu Feb 25 05:16
Full Name:  S. User Account:  S. User ID:  S. Last time logged in:  Sun Feb 21 23:02
Full Name:  User Account:  Bayless User ID:  /bin/bash Last time logged in:

...

And so on and so forth.

So somehow field #5 is getting cut up into pieces due to the spaces involved in the names, but I have no idea, nor can I find out how to suppress the chopping up of that field.

Anyone know how I could fix this so that the full names can come out only one time and completely then?

PS: This is my first post. If I somehow screwed up on the posting etiquette, then I'll learn. I swear!

You don't need cut to read things in like that here. In fact you almost never do, and shouldn't -- external process calls are pretty heavy things to be doing in a loop that's supposed to be small and fast.

You can run 'last username' to show the last login info for that username, instead of parsing through the list of ALL usernames every single loop..

What is that 2000 you're grepping for? the group ID? You could do something like:

while IFS=":" read UNAME PASS USERID GROUPID UINFO HOMEDIR SHELL OTHER
do
        [ "$GROUPID" == 2000 ] || continue

# DO other stuff
done < /etc/passwd

Yes, 2000 is the group ID.

So what could I use instead of cut to read exactly what I want?

Preferably, is there a way using cut to get the full name in one go per loop?

Well, you could use the code I gave you.

You use the following script.This script is similar to yours.The only change I did is extracting the first field from user informations which is the full username entry in the /etc/passwd file.

passfile=/etc/passwd
for i in `grep 100 $passfile`
do
fullname=`echo $i | cut -d: -f5|cut -d, -f 1` #here,only I added one more cut command to parse only the username from the fifth field of the /et/passwd file
useracc=`echo $i | cut -d: -f1`
userid=`echo $i | cut -d: -f3`
lastcom=`last | grep $useracc | head -1 | cut -c40-55`
echo "Full Name:"$fullname "User Account:"$useracc "User ID:"$userid" Last time logged in:"$lastcom
done