After talking about the for loop, it is time to discuss the second type of loops: the while loop. Unlike the for loop, the while loop doesn’t rely on a loop variable that iterates on a range or list of possible values. Instead, it depends on a condition that controls whether the loop would iterate one more time or not. In other words, a while loop continues to iterate as long as the specified condition (in its definition) evaluates to true. Once the condition fails, the loop exits. The while loop can be a replace to the for loop in some situations. Also it has its unique usages that for loops can’t be used in. In this article, we are going to talk about the while loop, its usages, and will illustrate them by examples.
- Line 6: initializes the two variables result and i to 1.
- Line 8: defines the while loop. The loop should iterates as long as the variable i is less than or equal to the integer entered by the user x.
- Lines 9 and 12: the do and done statements that delimit the loop body.
- Lines 10 and 11: the loop body:
- Line 10: uses the let command to make an accumulative multiplication. It multiplies the current value of the loop variable i by the value stored in the variable result, and then stores the result in the variable result.
- Line 11: uses the self-increment operator “++” to increment the value of the loop variable i.
- Line 13: prints the result back to the user.
So simple and logic, isn’t it?!
Using while Loop to Read the Contents of a File
In the previous example, we have made something that the for loop has already achieved (in the previous article). Now, we are going to investigate one of the unique usages of the while loop: reading a file contents, line by line.
while read line
done < filename
Consider you have a file that contains a list of directory names to be created, one per line. Again, you have two choices: the first is the manual method: to open two sessions to your Linux box, in the first one you open the file in a text editor, copy line by line, and then paste the copied line in the second session as argument to the command mkdir. You will need to do this for every line in the file. This is an option. The second one is to think as a programmer and write the following script.
Given the file dir_list.txt whose contents as follows:
Let’s execute the script:
Now, to the explanation:
- Line 6: defines the while loop that uses the read command to read a line from the file and assign it to the variable DIR.
- Line 7: the do statement that marks the loop body start.
- Lines 8 and 9: the loop body:
- Line 8: prints a message that script is about to create a directory.
- Line 9: creates the directory in hand.
- Line 10: the done statement, followed by the input redirection operator ‘<’, then the name of the file to be read.
Using while Loop to Read Input from User
Now, consider a case wherein we need to repeatedly prompt the user to enter a line of input. The input should be written to a file. This should continue as long as the user enters non-empty string.
How could we achieve this?! The while loop has the solution.
Read the following script:
Now, let’s see the script in action.
Let’s explain it:
This statement initializes the variable input to a temporary value. This is necessary to prevent the while loop from exiting at its first execution.
- while [ “$input” != “” ]
This defines a while loop that continues to execute as long as the value of the variable input is not equal to the empty string “”.
- The loop body:
- read -p “Enter a Line of input: ” input
This line prompts the user to enter a line of text, and then assigns the input to the variable input.
- echo $input >> outputfile.txt
This statement uses the echo command to print the value of the variable input as a line, and redirects that line to the file outputfile.txt instead of being printed to the screen.
* * * * * *
In this article, we have talked about the second type of loops: the while loops. We have learned its syntax, and how it works. We have seen how it could be used to do common things for loops could also do, and that it has unique usages like reading the contents of files line by line. We have illustrated our discussion with examples. In the next article, we are going to talk about the third and last type of loops: the until loops. Also, we will learn how to control loops to skip iteration, or to completely exit the loop. Until that time, don’t go anywhere. We won’t be late.