MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Shell Scripting Tutorials: Looping & Iteration statatement using while


The bash while loop is a control flow statement that allows code or commands to be executed repeatedly based on a given condition.

Most languages have the concept of loops: If we want to repeat a task twenty times, we don’t want to have to type in the code twenty times, with maybe a slight change each time.
As a result, we have for and while loops in the Bourne shell.


Types of Loops in Shell Scripting


Loops in Shell Scripting graphic listing while, for and until loops with their syntax

Syntax of Shell Scripting


Bash while loop syntax: while condition, do commands, done

Control Flow of While Loop

While loop flowchart: test true runs commands and loops back, false exits to done

Example of While Loop in Shell Script


Bash while loop script that runs ps and asks again (y/n) until the user enters n

Boolean Operators When Working With Shell Scripts


Apart from the boolean operators that youโ€™ve seen in other programming languages, including ==, <, >, <=, >=, etc., these are shell script specific operators. The major usage difference is that these operators will only work when numeric values are provided. With string value, the operation will simply fail.

OperatorDescriptionExample
-neChecks if both operands are not equal[ 1 -ne 2 ] is TRUE
โ€“eqChecks if both the operands are equal[ 1 -eq 2 ] is FALSE
โ€“ltCheck if the left operand is lesser than the right operand[1-lt 2 ] is TRUE
โ€“gtCheck if the left operand is greater than the right operand[1-gt 2 ] is FALSE
โ€“leCheck if the left operand is less than or equal to the right.[1-le 2 ] is TRUE
โ€“geCheck if the left operand is greater than or equal to the right.[1-ge 2 ] is FALSE

Creating a While Loop in Shell Script

A while loop doesnโ€™t have a repetition limit by default. We have to explicitly provide it a condition that at some point during the code execution, will turn to false. Else youโ€™ll have a loop that doesnโ€™t end unless an interrupt signal is sent.

Bash while loop printing True while i is less than or equal to 10, then echoing False

Save the script above by any name with the .sh extension. To run the file, you can either run it with the bash command like bash <filename>.sh or make the file executable using the chmod command. To make the script executable, run chmod +x <filename>.sh and then ./<filename>.sh will allow you to run the script.

Letโ€™s understand the script line by line.

  • i=0 โ€“ Here we set the variable $i to 0. Learn more about variables in our previous tutorial
  • while [ $i -le 10 ] โ€“ Run the while loop only until the variable $i is lesser than or equal to 10. So this loop will run 11 times including the ZERO-th run.
  • do โ€“ Marks the beginning of the while loop in shell scripts
  • echo True โ€“ A simple command that will print the word โ€œTrueโ€ on our terminal
  • ((i++)) โ€“ C style variable increments to add 1 to the variable i with every loop. There are various other ways to perform an increment include $i=((i+1)), ((i=i+1)), etc. Feel free to use either one.
  • done โ€“ Marks the end of the while loop
  • echo False โ€“ This again simply prints out the word False on the screen to indicate that condition has now turned false.

Example of nested While Loop

Three nested while loops where repeat 3 break jumps out to echo Out of all loops

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Shell Bash Scripting: Assignment & Excercise โ€“ 16

This exercise asks for a Bash script that packs a file or directory into a compressed tar archive and then encrypts it with a passphrase. The solution…

Read More

Shell Bash Scripting: Assignment & Excercise โ€“ 15

This exercise builds a Bash script that takes an IP address, sends it to the free ip-api.com geolocation API with curl, and uses jq to print the…

Read More

Shell script to git add and commit and push each directory at a time

This page shows how to use a Bash script to stage, commit and push changes one directory at a time. That can mean a folder full of…

Read More

AWS Tutorials: AWS CodeBuild – Buildspec code for creating AMI from running instances

This page has an AWS CodeBuild buildspec that finds every running EC2 instance tagged environment=uat and creates an AMI from each one with the AWS CLI. It…

Read More

Shell Bash Scripting: Assignment & Excercise – 7

This exercise uses Bash, rsync and SSH to copy a local file to a remote backup server, then sends an email when the copy finishes. Below the…

Read More

Shell Bash Scripting: Assignment & Excercise – 4

This exercise asks for a Bash script that checks CPU usage and sends an email when it goes over a threshold. The script above reads /proc/stat, but…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x