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.
The following is an example of doing just that:
#!/bin/bash
# changing the IFS value
IFS.OLD=$IFS
IFS=$‘\n’
for entry in $(cat /etc/passwd)
do
echo “Values in $entry –”
IFS=:
for value in $entry
do
echo “ $value”
done
done
$
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.
When you run this script, you get output something like this:
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
The inner loop parses each individual value in the /etc/passwd entry. This is also a great way to process comma-separated data, a common way to import spreadsheet data.