Infinite Loop
Explore how to implement infinite loops in Bash using the while statement with true or colon conditions. Understand their applications in system software and scripts to automate repetitive tasks such as monitoring disk space. Learn practical script examples and alternatives like the watch utility for continuous command execution.
We'll cover the following...
Infinite loop
The while statement fits well when we need to implement an infinite loop. This kind of loop continues as long as the program is running.
Infinite loops are often used in system software. They run the whole time while a computer stays powered on. An example is the microcontroller firmware that checks some sensors cyclically. It happens in an infinite loop. Also, such loops are used in computer games, antiviruses, monitors of computer resources, etc.
The while loop becomes infinite if its condition always stays true. The easiest way to make such a condition is to call the built-in Bash function true. Here is an example for doing so:
while true
do
sleep 1
done
Run the commands discussed in this lesson in the ...