| by Arround The Web | No comments

Creating Bash Infinite Loop by Example Scripts

Every loop has a finite lifespan and depending on the loop, it ends when the condition is either true or false. The bash infinite loop is simply a series of instructions that loops indefinitely. It has no ending condition, a condition that is never met, or a condition that stimulates a new iteration of the loop. The bash infinite loop can be created with the for, while, and until loops. With just a little modification to the infinite loop conventional syntax, we can implement the bash infinite loop scripts. Here, we are required to create a loop that executes the commands continuously until it is forcibly stopped from outside the program.

Example # 1: Infinite While Loop by Example Scripts

When the loop is infinitely executed without any termination, then we regard that loop as an infinite loop. The while loop can also become an infinite loop when the condition is specified with the “true” command. The true command does not perform any task and successfully returns exit code “0”. Here, we have given a bash script where the Infinite while loop operation is performed. The while loop takes the “true” command which is just used for readability. We can also leave the null command to the while loop as it is executed the same as the true command. Further, with the “do” statement, we have executed the “echo” command. The while loop is closed with the “done” keyword.

while true

do

echo "Do some task; Press [CTRL+C] to stop!"

done

At the time of the execution, we opened the terminal and ran the bash command “./bash1.sh”. When the bash file is executed, it runs endlessly and prints the echo command infinitely in the terminal. Note that, here, we have taken a “true” command for the while loop, but the false command can also be specified.

Example # 2: Infinite for Loop by Example Scripts

The simple structure of the infinite loop is demonstrated with the example script. Not only does the while loop has the infinite looping feature but we can have the for loop which also runs infinitely. The infinite for loop also behaves the same as the infinite while loop. Here, we have an infinite for loop script in which we have first declared the “for loop” which is specified without any condition and a parameter. It also executed successfully because, by default, it is set with the true command. After that, we called the echo statement by using the “do” keyword. We have also given “sleep” commands instructions with the echo statement and then the while loop ended with the “done” statement.

for (( ; ; ))

do

echo "Hello Bash Shell"

sleep 1

done

The infinite for loop script continues to print the echo statement in the terminal which is displayed in the image.

Example # 3: Infinite Until Loop by Example Scripts

Another way to create the bash infinite loop is by using the until loop. The until loop iterates through a block of commands as long as the required condition becomes false. The until loop becomes infinite when the condition in the statement of the loop is always false. The infinite until the loop created is the same as we have given scripts for the infinite while and for loops. The infinite until loop just uses the “until” keyword. We have provided the infinite until loop bash script below where we begin the script with the declaration of the variable “statement” and set it with the “false” value. Next, we have defined the “loop_no” variable which is set with the counter “0”.

After that, we specified the “until” statement which takes the “$statement” as an expression. Then, the echo keyword is used between the do and done statement. The echo command here prints the number of the loop cycles until the false results are returned. On each loop cycle, the number will be incremented. We have used the sleep command in the infinite until loop to execute the script periodically.

#!/bin/bash

statement=false

loop_no=0

until $statement

do

echo "Loop no : $loop_no"

((loop_no++))

sleep 1

done

The infinite until loop script on execution continues to display the loop cycles endlessly on the terminal.

Example # 4: Infinite Loop by Example Scripts with a Break Statement

Sometimes, we need to break out of a never-ending loop. The Infinite loop can also exist with the break statement. The infinite loop should end if a particular criterion is fulfilled. When a break statement is used, the loop will end, or the control will move on to the next statement. Here, we have used the break statement in the bash script. Firstly, we have set the script with the while loop expression which takes the true value. Then, we have given a printed command by using the echo modifier. After that, we have an if statement which verifies whether the condition is satisfied or not. Then, we have a “break” statement which breaks the while loop here.

#!/usr/bin/env bash

while true

do

echo "Hit [CTRL+C] to exit from the loop..."

sleep 2

if [ condition ]

then

break

fi

done

When the echo statement is executed successfully then the break statement terminates the while loop as the output image is provided below.

Example # 5: Infinite Loop with the Condition by Example Scripts

Now, we have taken a scenario to overcome this infinite loop execution by another technique. When the loop is given a condition, it terminates where that specified condition is satisfied. The while loop bash script is implemented below where we have first set the variable “x” with the value “10”. Then, we specified the while loop where the expression “while [ $x -gt 5 ]” is given. The expression has the variable “$x” which should be greater than “5” as the operator “gt” between the value “5” and the variable “$x”. After that, we defined the echo statement where the “$x” is called out to print the values. The “x–” statement decrements the value for the “$x” upon each iteration.

#!/usr/bin/bash

x=10

while [ $x -gt 5 ];

do

echo $x

((x--))

done

echo "Results out of the loop"

The image displayed the termination of the while loop when the “x” value reached the value “6”. Here, the condition becomes false because the next iteration value will be “5” which does not satisfy the while condition. Moreover, we can also use the continue statement which is used to prevent the code block from being executed.

Conclusion

The guide helps us to create the bash infinite loop which continues to run constantly because no ending condition is given to them. We have provided the bash script example where we have created the infinite loop by using the while loop, for loop, and the until loop. After creating the infinite loop bash example, we have specified the ways to overcome these never-ending loops.

Share Button

Source: linuxhint.com

Leave a Reply