Linux For DevOps | Shell Script Using Loops

Linux For DevOps | Shell Script Using Loops

This blog will cover 4 loops while, until, for, select.

A loop is a powerful programming tool that enables you to execute a set of commands repeatedly. This helps to create code which can be run multiple times as per the requirements. In this blog we will see 4 types of loops

  1. While Loop
  2. Until Loop
  3. For Loop
  4. Select Loop

Usually shells are interactive that mean, they accept command as input from users and execute them. However some time we want to execute a bunch of commands routinely, so we have type in all commands each time in terminal. As shell can also take commands as input from file we can write these commands in a file and can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with .sh file extension eg. myscript.sh

There are many reasons to write shell scripts –

  • To avoid repetitive work and automation
  • System admins use shell scripting for routine backups
  • System monitoring
  • Adding new functionality to the shell etc.
  • Advantages of shell scripts

While Loop

The while loop enables you to execute a set of commands repeatedly until some condition occurs. It is usually used when you need to manipulate the value of a variable repeatedly.

while command
do
   Statement(s) to be executed if command is true
done

Example :- Loop to display the numbers zero to nine. Get github code here

count=1
while [ $count -le 10 ]
do
echo $((count++))
done

Until Loop

The until loop is used to execute a given set of commands as long as the given condition evaluates to false. The condition is evaluated before executing the commands. If the condition evaluates to false, commands are executed.

until command
do
   Statement(s) to be executed until command is false.
done

Example :- If the resulting value is false, given statement(s) are executed. If the command is true then no statement will be executed and the program jumps to the next line after the done statement. Get github code here

count=1
until [ ! $count -lt 10 ]
do
echo $((count++))
done

For Loop [Loop statements]

The for loop operates on lists of items. It repeats a set of commands for every item in a list. Get github code here

for var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done
pets="dogs cat parrot fish"
for pet in $pets
do
  echo $pet
done

Select

The select loop provides an easy way to create a numbered menu from which users can select options. It is useful when you need to ask the user to choose one or more items from a list of choices.

select var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done

Example :-

#!/bin/bash
select fruit in Apple Mango Banana Kiwi
do
echo "You have chosen $fruit"
done

Did you find this article valuable?

Support Dheeraj Choudhary by becoming a sponsor. Any amount is appreciated!