Simple For Loop
The following is the simpliest loop you can find within a sh-based shell.
for i in *; do [..] doneNow, a few things you need to watch out for. The "*" can be replaced with `cat file` or `ls foo*`, but since the for is SPACE delimited any entires that have spaces in them will resolved in correctly. If you need to process something with an entry per line look at the while loop below.
Simple While Loop
The next simpliest loop is the while loop. It is more flexible than the
for loop. The example below is an example of how you can process a a
file line by line:
cat [file name] | while read x; do [..] done
You can also use a while loop based on a condition of a command. The below will check the output of "/bin/true" to see if it is true, and if so you keep running.
while /usr/bin/true; do [..] done
Xargs Loops
The examples I'll use will have take the output of find and send it to xargs,
but anything you can pipe any space or null terminated lines into xargs for
processing.
Example of space based parsing.
find [..] | xargs [..]
Example of NULL based parsing.
find [..] -print0 | xargs -0 [..]
OK, why would I use NULL over space in parsing? The main reason is like above. If your dataset may have spaces ("/path to/file name/with spaces in name.html") you may want to use -0 (assuming your find/xargs supports this. Some older systems don't).
Click to return to main page.
























