Bash Scripting – Looping on File Contents
Often, you must iterate through items stored inside a file. This requires combining two of the techniques covered:
- Using nested loops
- Changing the IFS environment variable
By changing the IFS environment variable, you can force the for command to handle each line in the file as a separate item for processing, even if the data contains spaces. After you’ve extracted an individual line in the file, you may have to loop again to extract data contained within it.
The classic example of this is processing data in the /etc/passwd file. This requires that you iterate through the /etc/passwd file line by line and then change the IFS variable value to a colon so you can separate the individual components in each line.
This script uses two different IFS values to parse the data. The first IFS value parses the individual lines in the /etc/passwd file. The innerfor loop next changes the IFS value to the colon, which allows you to parse the individual values within the /etc/passwd lines.
Values in rich:x:501:501:Rich Blum:/home/rich:/bin/bash - rich x 501 501 Rich Blum /home/rich /bin/bash Values in katie:x:502:502:Katie Blum:/home/katie:/bin/bash - katie x 506 509 Katie Blum /home/katie /bin/bash