< Previous | Contents | Next >
Reading Files With Loops
while and until can process standard input. This allows files to be processed with while and until loops. In the following example, we will display the contents of the dis- tros.txt file used in earlier chapters:
#!/bin/bash
# while-read: read lines from a file while read distro version release; do
printf "Distro: %s\tVersion: %s\tReleased: %s\n"
$distro
$version
$release done < distros.txt
#!/bin/bash
# while-read: read lines from a file while read distro version release; do
printf "Distro: %s\tVersion: %s\tReleased: %s\n"
$distro
$version
$release done < distros.txt
To redirect a file to the loop, we place the redirection operator after the done statement. The loop will use read to input the fields from the redirected file. The read command will exit after each line is read, with a zero exit status until the end-of-file is reached. At that point, it will exit with a non-zero exit status, thereby terminating the loop. It is also possible to pipe standard input into a loop:
#!/bin/bash
# while-read2: read lines from a file
sort -k 1,1 -k 2n distros.txt | while read distro version release; do printf "Distro: %s\tVersion: %s\tReleased: %s\n"
$distro
$version
$release
done
#!/bin/bash
# while-read2: read lines from a file
sort -k 1,1 -k 2n distros.txt | while read distro version release; do printf "Distro: %s\tVersion: %s\tReleased: %s\n"
$distro
$version
$release
done
Here we take the output of the sort command and display the stream of text. However, it is important to remember that since a pipe will execute the loop in a subshell, any vari- ables created or assigned within the loop will be lost when the loop terminates.