Redirecting loop output
You can redirect the loop output to a file using the done
command:
#!/bin/bash for (( v1 = 1; v1 <= 5; v1++ )) do echo "$v1" done > file
If there is no file, it will be created and filled with the loop output.
This redirection is helpful when you don't need to show the loop output on the screen and save it to a file instead.
Controlling the loop
Having entered our loop, we may need to either exit the loop prematurely or perhaps exclude certain items from processing. If we want to process only directories in a listing, rather than every file of any type, then to implement this, we have loop control keywords, such as break
and continue
.
The break
keyword is used to exit the loop, processing no more entries, whereas the continue
keyword is used to stop the processing of the current entry in the loop and resume the processing with the next entry.
Assuming we only want to process directories, we could implement a test within the loop and determine the file type...