while loop example in linux shell script

Mastering Linux Shell Scripts: A Comprehensive Example of While Loop

By Olivia Rodriguez
AD

In the realm of Linux shell scripting, the while loop is a powerful tool that enables repetitive tasks until a specified condition is met. This looping construct is essential for automating routine tasks, processing data, and improving overall efficiency. Let's delve into the world of while loops in Linux shell scripts, exploring their syntax, usage, and practical examples.

Before we dive into the details, it's crucial to understand that while loops in Linux shell scripts are typically used when you don't know in advance how many times the loop needs to be executed. This makes them highly versatile and suitable for a wide range of tasks.

Understanding the Syntax of While Loops

The basic syntax of a while loop in a Linux shell script is as follows:

AD
Introduction to Shell Scripting - Topic
Introduction to Shell Scripting - Topic
C++ Loops Explained in 1 Minute 🔁 | for, while, do-while Made Easy 💻✨
C++ Loops Explained in 1 Minute 🔁 | for, while, do-while Made Easy 💻✨
40 Simple Yet Effective Linux Shell and Bash Script Examples
40 Simple Yet Effective Linux Shell and Bash Script Examples

while condition do command(s) done

The loop continues to execute the specified commands as long as the condition remains true. Once the condition evaluates to false, the loop terminates, and the script proceeds to the next line.

How to Use the for Loop in a Linux Bash Shell Script
How to Use the for Loop in a Linux Bash Shell Script
For Loop vs While Loop in Python — Explained Simply 🐍
For Loop vs While Loop in Python — Explained Simply 🐍
How to Run Shell Scripts in Linux [with Detailed Explanation for Beginners]
How to Run Shell Scripts in Linux [with Detailed Explanation for Beginners]

Initializing Variables and Conditions

To illustrate the syntax, let's consider a simple example where we initialize a counter variable and use it in the while loop's condition:

counter=0 while [ $counter -lt 5 ] do echo $counter counter=$((counter+1)) done

AD

In this example, the loop will execute five times, printing the counter value each time and incrementing it by one.

Introduction to Linux Shell and Shell Scripting - GeeksforGeeks
Introduction to Linux Shell and Shell Scripting - GeeksforGeeks
Linux Shell Scripting Command Cheat Sheet
Linux Shell Scripting Command Cheat Sheet
linux and windows reverse shell scripts with an image of a bird in the background
linux and windows reverse shell scripts with an image of a bird in the background
Kernel vs Shell – Core Concepts of Linux
Kernel vs Shell – Core Concepts of Linux
A Shell Script to Monitor Network, Disk, Uptime, Load & RAM
A Shell Script to Monitor Network, Disk, Uptime, Load & RAM
two different types of loop for white and black numbers on yellow background with text below
two different types of loop for white and black numbers on yellow background with text below

Using Until Loops

Another variation of the while loop is the until loop, which continues to execute the specified commands until the condition is false. The syntax for an until loop is as follows:

until condition do command(s) done

AD

Here's an example using the until loop to perform the same task as the previous while loop:

Bash if elif else Statement with Examples - Linux Shell Scripting Guide
Bash if elif else Statement with Examples - Linux Shell Scripting Guide
while loop in python
while loop in python
a computer screen with the words linux commands on it and an image of a keyboard
a computer screen with the words linux commands on it and an image of a keyboard
Using source in Shell Scripts
Using source in Shell Scripts
a flow diagram with the words white - loop and an exit from the loop on it
a flow diagram with the words white - loop and an exit from the loop on it
C# While Loop
C# While Loop
Python while Loops: Repeating Tasks Conditionally – Real Python
Python while Loops: Repeating Tasks Conditionally – Real Python
Linux Shell Guide
Linux Shell Guide
How to Perform Syntax Checking Debugging Mode in Shell Scripts
How to Perform Syntax Checking Debugging Mode in Shell Scripts
a computer screen with some type of programming program on it's side, and another screenshot of the code
a computer screen with some type of programming program on it's side, and another screenshot of the code
While Loop
While Loop
an image of a computer screen with text and symbols in neon colors on black background
an image of a computer screen with text and symbols in neon colors on black background
Unix Shell Commands (1983)
Unix Shell Commands (1983)
Learn Linux from Beginner to Advanced (Part-8) | Shell & Productivity Commands
Learn Linux from Beginner to Advanced (Part-8) | Shell & Productivity Commands

counter=0 until [ $counter -ge 5 ] do echo $counter counter=$((counter+1)) done

In this case, the loop will also execute five times, but the condition is checked at the end of each iteration, making it slightly different from the while loop.

AD

Practical Applications of While Loops

Now that we've covered the basics of while loops let's explore some practical applications and examples.

Processing Files Line by Line

One common use case for while loops is processing files line by line. The following example reads a file named 'data.txt' and prints each line:

while IFS= read -r line do echo "$line" done < data.txt

The IFS (Internal Field Separator) variable is set to an empty string to preserve whitespace in the input lines. The -r option prevents backslashes from being interpreted as escape characters.

Menu-Driven Scripts

While loops are also essential for creating menu-driven scripts that allow users to interact with the script by selecting options. Here's a simple example of a menu-driven script that displays a list of options and prompts the user to enter their choice:

while true do echo "Menu:" echo "1. Option 1" echo "2. Option 2" echo "3. Option 3" echo "4. Exit" read -p "Enter your choice: " choice case $choice in 1) echo "You chose Option 1" ;; 2) echo "You chose Option 2" ;; 3) echo "You chose Option 3" ;; 4) break ;; *) echo "Invalid choice. Please try again." ;; esac done

In this example, the loop continues indefinitely until the user enters '4' to exit the script.

In conclusion, while loops are a fundamental aspect of Linux shell scripting, enabling automation, data processing, and user interaction. Mastering while loops will significantly enhance your scripting capabilities and allow you to tackle more complex tasks efficiently. Happy scripting!

AD