Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Processing the Output of a Loop
Finally, you can either pipe or redirect the output of a loop within your shell script. You do this by adding the processing command to the end of the done command:
for file in /home/rich*
do
if [ -d “$file” ]
then
echo “$file is a directory”
elif
echo “$file is a file”
fi
done > output.txt
Instead of displaying the results on the monitor, the shell redirects the results of the for command to the file output.txt.
Consider the following example of redirecting the output of a for command to a file:
$ cat test23
#!/bin/bash
# redirecting the for output to a file
for (( a = 1; a < 10; a++ ))
do
echo “The number is $a”
done > test23.txt
echo “The command is finished.”