while loop in shell script example

Mastering While Loops in Shell Script: A Hands-On Example

By Kevin Brown
AD

A while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. In the context of shell scripting, a while loop is particularly useful for executing a block of code as long as a certain condition is true. Let's delve into the syntax and usage of while loops in shell scripts with practical examples.

Before we dive into the examples, it's crucial to understand the basic syntax of a while loop in shell scripting. The general format is as follows:

```bash while condition do command(s) done ```

The condition is a command that returns a status code. If the status code is 0 (which means the command was successful), the command(s) within the loop are executed. This process continues until the condition is no longer true.

AD
Learn Interactive Shell Scripting in Linux
Learn Interactive Shell Scripting in Linux
Fun with Control Structures in Shell Scripting - SlideServe
Fun with Control Structures in Shell Scripting - SlideServe
Linux Shell Guide
Linux Shell Guide

Infinite While Loops

An infinite while loop in a shell script is one where the condition always evaluates to true, causing the loop to run indefinitely. This can be useful for creating interactive shell scripts or daemons.

Here's an example of an infinite while loop that displays a menu and prompts the user for input until they choose to exit:

How to Run Shell Scripts in Linux [with Detailed Explanation for Beginners]
How to Run Shell Scripts in Linux [with Detailed Explanation for Beginners]
Client Challenge
Client Challenge
The Beginner's Guide to Shell Scripting 2: For Loops
The Beginner's Guide to Shell Scripting 2: For Loops
```bash while true do echo "Menu:" echo "1. Option A" echo "2. Option B" echo "3. Exit" read -p "Enter your choice: " choice case $choice in 1) echo "You chose Option A";; 2) echo "You chose Option B";; 3) break;; *) echo "Invalid choice. Please try again.";; esac done ```

Using break to Exit a While Loop

The break statement is used to exit a loop prematurely, even if the condition is still true. In the above example, when the user enters '3', the break statement is executed, causing the loop to terminate.

Here's another example where the loop continues to ask for user input until a valid integer is provided:

AD
```bash while true do read -p "Enter a number: " num if ! [[ $num =~ ^[0-9]+$ ]] then echo "Invalid input. Please enter a number." else break fi done ```

Using until to Create a While Loop

In some cases, it might be more intuitive to use an until loop, where the loop continues until a condition is true. The syntax for an until loop in shell scripting is as follows:

Linux Shell script to display Files and Folders in Specific Directory | ls command in linux #shorts
Linux Shell script to display Files and Folders in Specific Directory | ls command in linux #shorts
Bash scripting cheatsheet
Bash scripting cheatsheet
PowerShell Script to Copy All OneDrive Files to Another User
PowerShell Script to Copy All OneDrive Files to Another User
How to use IF Loop in C# ???
How to use IF Loop in C# ???
How to Send Email using Shell Script | Send mails using a Bash Script | Shell Scripting for DevOps
How to Send Email using Shell Script | Send mails using a Bash Script | Shell Scripting for DevOps
Shell Scripting for Automation (Beginner Guide)
Shell Scripting for Automation (Beginner Guide)
```bash until condition do command(s) done ```

Here's an example of an until loop that counts down from a given number:

```bash read -p "Enter a number: " num until [ $num -lt 0 ] do echo $num num=$((num - 1)) done ```

While Loops with Counters

While loops are often used in conjunction with counters to execute a block of code a specific number of times. Here's an example that prints the Fibonacci sequence up to the 10th term:

AD
```bash a=0 b=1 n=0 while [ $n -lt 10 ] do echo -n "$a " fn=$((a + b)) a=$b b=$fn n=$((n + 1)) done echo ```

In this example, the counter n is incremented in each iteration of the loop. Once n reaches 10, the loop terminates.

some type of calligraphy written in cursive writing, with the letters below it
some type of calligraphy written in cursive writing, with the letters below it
Mastering bash String Operators and Arrays in UNIX Shells
Mastering bash String Operators and Arrays in UNIX Shells
an image of a computer screen with text
an image of a computer screen with text
Linux Shell Roundup: 15 Most Popular Open Source Linux Shells
Linux Shell Roundup: 15 Most Popular Open Source Linux Shells
Handwritten Script Layer Art
Handwritten Script Layer Art
Willow Script Punctuations
Willow Script Punctuations
a drawing of a seashell with bubbles coming out of it's shell and the word sea written in cursive writing
a drawing of a seashell with bubbles coming out of it's shell and the word sea written in cursive writing
Scripting
Scripting
Font - shelley script allegro
Font - shelley script allegro
credit to tania.fontanet on ig!!
credit to tania.fontanet on ig!!
an image of a document with the words script example and arrows pointing in different directions
an image of a document with the words script example and arrows pointing in different directions
a drawing of two spirals on top of each other
a drawing of two spirals on top of each other
Amelia Written In Cursive, Amelia In Cursive, Amelia In Cursive Font, Amelia Script Font, Amelia Name Calligraphy Elegant, Amelia Cursive, Amelia Script, Amelia Name Calligraphy, Amelia In Calligraphy
Amelia Written In Cursive, Amelia In Cursive, Amelia In Cursive Font, Amelia Script Font, Amelia Name Calligraphy Elegant, Amelia Cursive, Amelia Script, Amelia Name Calligraphy, Amelia In Calligraphy

Shell scripting while loops are a powerful tool for controlling the flow of your scripts. By mastering their syntax and usage, you can create more dynamic and efficient shell scripts. Happy scripting!

AD