I want to create a shell that reads a text file.
The file, users_test.txt
, looks like:
Headers - Group : Team : User
Group_A:Team_Red Team_Green:worker_1 worker_2 worker_2 worker_3
Group_B:Team_Blue Team_Black Team_Grey:worker_4 worker_2 worker_5 worker_6
I want to loop through the .txt file above which is colon separated. Here is my current solution.
#!/bin/bash
# set filename
FILENAME="users_test.txt"
# loop through file, each line is divided into 3 fields separated by a colon (:)
while IFS=':' read -r groups teams users
do
echo "group $groups has teams $teams and user $users"
for team in $teams; do
echo "hey: "$team
done
for group in $groups; do
echo "hi: "$group
done
# Iterate the string variable using for loop
for user in $users; do
echo "hello: "$user
done
done < "$FILENAME"
Why does it end after the first loop iteration?
output:
admin@computer:$ ./setup-test1.sh
group has teams Team_Red Team_Green and user worker_1 worker_2 worker_2 worker_3
hey: Team_Red
hey: Team_Green
hi: Group_A
hello: worker_1
hello: worker_2
hello: worker_2
hello: worker_3
admin@computer5:$
It seems to work fine but it ends after the first line, as a result Group B isn't printed.
Can anyone tell my why the script only reads the first line containing Group_A but not the second line with Group_B?