Linux Tactic

Mastering the For Loop in Shell Scripting: A Comprehensive Guide

If you’re familiar with Linux or any other operating system that uses a shell, you’ve probably heard of shell scripting. Shell scripting is a powerful way to automate tasks, especially when working with large sets of data or files.

In this article, we will dive into the “for” loop in shell scripting, a critical component for automating operations. We will start by defining what a “for” loop is and how it is used in shell scripting, then explore different ways to define “for” loops in shell scripting.

What is a “for” loop in shell scripting? A “for” loop is a control structure in shell scripting used to execute a set of commands repeatedly.

The loop itself consists of a sequence of commands that are executed repeatedly, and a list of values that determine how many times the loop runs. The “for” loop is similar to the “for” loop in other programming languages, but with a syntax that is tailored to the shell environment.

Different ways to define “for” loops in shell scripting:

The “for” loop can be defined in several ways, depending on the task at hand. Three common ways to define “for” loops in shell scripting are: bracket-style, without brackets, and via a file.

Bracket-style: The bracket-style “for” loop is the most commonly used in shell scripting. It starts with the keyword “for,” followed by a variable or name that represents each element in the loop.

Curly brackets are then used around the list of values, and the “

do” statement is after the list. Without brackets: This loop is also referred to as the “infinite loop.” It operates indefinitely unless a break condition is inserted.

The “for” loop’s starting and ending values are controlled by the commands inside the loop. File-based: This method is useful for reading data from an external source, e.g., reading the contents of a text file.

We can use the “while” loop to read the content line by line and store it in a variable, which is then used as the condition for the “for” loop.

Examples of “for” loops in shell scripting:

In this section, we will take a look at some examples of “for” loops in shell scripting.

These examples will give us a better understanding of how the “for” loop works, and how to implement it in the shell environment. Example 01: “for” loop in shell script without writing in file:

“`

#!/bin/bash

for i in 1 2 3 4 5

do

echo $i

done

“`

This script outputs the numbers 1 through 5 because the $i variable is initially set to 1 and incremented by 1 until it reaches 5. Example 02: “for” loop using file-based approach:

“`

#!/bin/bash

file=”file.txt”

for i in $(cat $file)

do

echo $i

done

“`

This script reads the contents of the file.txt file line by line and stores it in the $i variable, which is then printed on each iteration. Example 03: Using a list to define “for” loop values:

“`

#!/bin/bash

list=”1 2 3 4 5″

for i in $list

do

echo $i

done

“`

This script uses a list of values to define the range of the “for” loop. The loop starts at the first item in the list ($list), and the last item defines the ending value of the loop.

Example 04: Using “if” statement within the “for” loop:

“`

#!/bin/bash

list=”1 2 3 4 5″

for i in $list

do

if [ $i -eq 3 ]

then

break

fi

echo $i

done

“`

This script uses an “if” statement to break the “for” loop once the value of $i equals 3. This code displays all the values before the “if” statement executes, then terminates.

Example 05: Using an array list in the “for” loop:

“`

#!/bin/bash

array=(“One” “Two” “Three” “Four” “Five”)

for i in “${array[@]}”

do

if [ $i == “Three” ]

then

break

fi

echo $i

done

“`

This script defines an array and uses it to populate the “for” loop’s range. The “if” statement is used to break the loop once $i is equal to “Three.”

Example 06: Using “for” loop to list files:

“`

#!/bin/bash

for file in /home/user/*

do

echo $file

done

“`

This script uses the “for” loop to search the /home/user directory for files. It then lists the names of all the files found in the directory.

Conclusion:

The “for” loop is a powerful command that can be used in shell scripts to automate tasks. In this article, we have seen how the “for” loop can be defined in different ways depending on the task at hand.

We have looked at several examples of “for” loops to illustrate how the command works, and how to use it in shell scripts. Whether working with arrays, files, or directories, the “for” loop is an essential tool in the shell scripting toolbox.

The “for” loop in shell scripting is a powerful tool for automating tasks and working with large sets of data or files. This loop consists of a sequence of commands that are executed repeatedly, and a list of values that determine how many times the loop runs.

There are different ways to define a “for” loop, depending on the task at hand. Examples of using a “for” loop include file-based approaches, defining a list of values, and using an array list.

“If” statements can also be used to control the loop and break out of it if needed. Understanding the “for” loop is a critical component of shell scripting, making it a crucial tool in any programmer’s toolbox.

Popular Posts