Just playing with loops 𖦹..

Just playing with loops 𖦹..

A loop is a programming tool that repeats a set of instructions until a condition is true. They help avoid writing the same code multiple times. There are Three statements in loop on scripting. Used in programming to execute statements iterately during the period of a running program.

While : A while loop is used when the number of iterations is unknown. This loop runs as long as the condition is true the commands inside the loop are executed until the condition is no longer met.

Step 1 : We are going to create a file for while loops.

$ touch demo-while.sh
or
$ vim demo-while.sh

Step 2 : Lets write the while loop script and we want to this while statement meet only odd number till 20.

#!/bin/bash


num=0

while [[ $((num%2))==0 && $num -le 20  ]]
do
        echo $num
        num=$((num+3))
done

Step 3 : Lets execute the file with this command

$ ./demo-while.sh
or
bash demo-while.sh

Step 4 : Check the output of this file and see exact it matches the conditions..

For : A for loop is used when you know how many times you need to repeat something. We'll set a start and end point and the loop runs through a certain number of iterations.

Step 1 : Lets make a file to write the for loops script.

$ vim demo-for-loops.sh

Step 2 : Here we gonna write the loop file and take arguments from user

#!/bin/bash

# Here we going to mentioned and applying them in script


<< task
The For loops operates on the list of items it continiously iterates the 
commands until it matches the end
task 



for (( num=$2 ; num<=$3; num++ ))
do
        mkdir "$1$num"
done

Step 3 : After writting the script we need to save it and giving this file a partcular permission so who can do what to execute.

$ chmod 770 demo-for-loops.sh

Step 4 : To execute this particlar file we need to run this command. Here is the part to take a arguments from users.

$ bash demo-for-loops.sh Roll-Number 1 100
or 
$ ./ demo-for-loops.sh Roll-Number 1 100

Step 5 : Do ls to see the output whatever we did till now.

Until : A loop is used when you want to loop until a condition is true. The loop continues to execute the commands inside it until the condition returns false. For example We can keep trying to start a service and check if it's running repeating until the service is running.

Step 1 : First of all create a file in the name of until.sh like this with this command.

$ touch demo-until.sh
or 
$ vim demo-until.sh

Step 2 : Write a script for until loop it will run from 0 to till the given statement.

#!/bin/bash

num=0

until [[ ! $num -lt 21  ]]

do
        echo $num

        num=$((num+1))

done
~

Step 3 : Giving permission to the file so who can do what with the file depends on permission.

$ chmod 770 demo-until.sh

Step 4 : Lets execute the file with this command.

$ ./demo-until.sh
or 
bash demo-until.sh

Step 5 : Here let's see the output of the written file.

Here we tried to demonstrate three types among the loops uses in shell script.

Did you find this article valuable?

Support dailydoseindevops by becoming a sponsor. Any amount is appreciated!