You can output the file with removed trailing spaces like this:
sed 's/[[:space:]]*$//' "$file"
example:
> echo "123 " > file > echo "+$(cat file)+" +123 + > echo "+$(sed 's/[[:space:]]*$//' file)+" +123+
and another example:
> echo "123 " > file > echo "+$(cat file)+" +123 + > sed -i -e 's/[[:space:]]*$//' file > echo "+$(cat file)+" +123+
or remove it from a string saved in a variable:
sed 's/[[:space:]]*$//' <<<"$line"
example:
> string="alallal "; > string=$(sed 's/[ ]*$//' <<<"$string") > echo "+${string}+" +alallal+
The [[:space:]]* matches one or more whitespaces characters (tabs, spaces). If you want only spaces, replace that with just [ ]*. The $ is used to indicate end of line.
To get count of lines in file, use wc -l:
index=$(wc -l < "$FILE")
Note that:
while read name
by iteself removes trailing and leading whitespace characters. Also allows backslash to escape characters. Use:
while IFS= read -r name
More about that topic can be found here.
To read a file into an array without trailing whitespaces, use:
mapfile -t get_user_group_from_file < <(sed 's/[[:space:]]*$//' file)