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."
$ ./test23
The command is finished.
$ cat test23.txt
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is...