Shell Scripting Functions and Loops

Shell Scripting Functions and Loops

This is the third part of our Shell Scripting series. In the previous blogs, we covered shell scripting introduction, conditional statements, different types of conditions such as numeric, string, and file conditions, user inputs, and many other important topics. If you have not read the previous parts, I strongly recommend to read those blog first. It will improve your understanding of the concepts discussed in this blog.

Now, in this blog, we will cover functions, loops, and commonly used scripts that are widely used in real-time jobs and automation tasks.

Let’s get started.


Functions

Sometimes, we use the same group of commands repeatedly in a script. Writing the same commands again and again is frustrating and inefficient. Instead of rewriting them every time, we use functions.

What Is a Function?

A function is a block of commands that performs a specific task. When a function is called, all the commands inside that function are executed. Functions make scripts reusable, cleaner, and easier to maintain.

There are mainly two types of functions in shell scripting:

  • Normal functions (without parameters)
  • Functions with parameters

Simple Function (Without Parameters)

A simple function does not accept any parameters. It performs the same task every time it is called.

#!/bin/bash
show_message() {
echo "Welcome to Shell Scripting"
}
show_message
show_message

Output:

Welcome to Shell Scripting
Welcome to Shell Scripting

Function with Parameters

Functions can accept parameters. These parameters are passed while calling the function and accessed using positional variables such as $1, $2, and so on.

#!/bin/bash
greet_user() {
echo "Hello, $1"
}
greet_user "Linux"
greet_user "DevOps"

Output:

Hello, Linux
Hello, DevOps

Here the function accepts one parameter and prints a greeting message based on the value passed during the function call.


Function with Parameters – Addition

#!/bin/bash
add_numbers() {
result=$(( $1 + $2 ))
echo "Sum is: $result"
}
add_numbers 10 20
add_numbers 5 7

Output:

Sum is: 30
Sum is: 12

Here the function takes two numbers as input, performs addition, and prints the result.


Loops

Loops are used in shell scripting when we want to execute a command or a group of commands repeatedly based on a condition. Instead of writing the same commands multiple times, loops allow us to automate repetitive tasks efficiently.

Loops make scripts shorter, easier to read, and less error-prone. Shell scripting provides different types of loops such as for loop, while loop, and until loop.


For Loop

A for loop is used when we know in advance how many times the loop should iterate, or when we want to iterate over a list or a range of numbers.

#!/bin/bash
for i in 1 2 3 4 5
do
echo "Iteration number: $i"
done

Output:

Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5

Here the loop runs five times because five values are provided in the for loop.


While Loop

A while loop is used when we do not know how many times the loop will iterate. The loop continues as long as the condition remains true.

#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo "Count is: $count"
count=$((count + 1))
done

Output:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5

Here the loop keeps running until the condition becomes false.


Until Loop

An until loop works opposite to a while loop. It continues executing commands until the condition becomes true.

#!/bin/bash
count=1
until [ $count -gt 5 ]
do
echo "Count is: $count"
count=$((count + 1))
done

Output:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5

Here the loop stops once the condition becomes true.


Continue Statement

The continue statement is used to skip the current iteration and move to the next iteration.

#!/bin/bash
for i in 1 2 3 4 5
do
if [ $i -eq 3 ]; then
continue
fi
echo "Value: $i"
done

Output:

Value: 1
Value: 2
Value: 4
Value: 5

Here when the condition $i -eq 3 is met, the continue statement skips that iteration, so the value 3 is not printed.


Break Statement

The break statement is used to exit the loop immediately, even if the loop condition is still true.

#!/bin/bash
for i in 1 2 3 4 5
do
if [ $i -eq 4 ]; then
break
fi
echo "Value: $i"
done

Output:

Value: 1
Value: 2
Value: 3

Here the loop stops completely when the value reaches 4.


Function with Loop

#!/bin/bash
print_values() {
for value in "$@"
do
echo "Value: $value"
done
}
print_values 10 20 30 40

Output:

Value: 10
Value: 20
Value: 30
Value: 40

Here $@ represents the list of all arguments passed to the function, and the loop processes each value one by one.


Functions and loops are the building blocks of shell scripting. Functions help reuse code, and loops help automate repetitive tasks. These concepts are widely used in real-time automation and DevOps scripts.

Be the first to comment

Leave a Reply

Your email address will not be published.


*