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:

$ for f in * ; do
[ -d "$f" ] || continue
chmod 3777 "$f"
done

Within the loop we want to set permissions including the SGID and Sticky bits, but for the directories only. The * search will return all files, the first statement within the loop will ensure that we only process directories. If the test is done for the current loop, the target fails the test and is not a directory; the continue keyword retrieves the next loop-list item. If the test returns true and we are working with a directory then we will process the subsequent statements and execute the chmod command.

If we need to run the loop until we found a directory and then exit the loop we can adjust the code so that we can iterate though each file. If the file is a directory then we exit the loop with the break keyword:

$ for f in * ; do
[ -d "$f" ] &&break
done
echo "We have found a directory $f"

Within the following screenshot, we can see the code that I just wrote in action:

Controlling the loop

By working with the same theme, we can print each directory found in the listing using the following code:

for f in * ; do
[ -d "$f" ] || continue
dir_name="$dir_name $f"
done
echo "$dir_name"

We can achieve a result by processing the loop item only if it is a directory and within the loop. We can work with regular files only using the if test. In this example, we append the directory name to the dir_name variable. Once we exit the loop, we print he complete list of directories. We can see this in the following screenshot:

Controlling the loop

Using these examples and your own ideas, you should now be able to see how you can control loops using the continue and break keywords.

While loops and until loops

When using the for loop we iterate through a list, it's either the one that we create or the one that is dynamically generated. Using the while or until loops, we loop based on the fact that the condition becomes either true or false.

A while loop loops while the condition is true and conversely an until loop will loop while the condition is false. The following command will count from 10 through to zero. Each iteration of the loop printing the variable and then reducing the value by 1:

$ COUNT=10
$ while (( COUNT >= 0 )) ; do
echo -e "$COUNT c"
(( COUNT-- ))
done ; echo

We can see the output of this command in the following screenshot; thus, confirming the countdown to zero:

While loops and until loops

Note

The use of the c escape sequence used here allows the suppression of the line-feed normally used with echo. In this way, we can keep the countdown on the single-line of output. I think you will agree that its a nice effect.

The functionality of this loop can be gained using the until loop; just a quick rethink of the logic is required, as we will want to loop until the condition becomes true. Generally, it is a personal choice and the way the logic works best for you about which loop to use. The following example shows the loop written with the until loop:

$ COUNT=10
$ until (( COUNT < 0 )) ; do
echo -e "$COUNT c"
(( COUNT-- ))
done ; echo
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.196.146