I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…
Overview of Unix Shell Loops and Different Loop Types like:
Unix Do While Loop
Unix For Loop
Unix Until Loop
In this tutorial, we will cover the control instructions that are used to iterate a set of commands over a series of data.
Unix offers three loop structures of which we can repeat a part of a program at a specified number of times.
Unix Video #17:
Loops in Unix
You may use different loops based on the situation.
They are:
#1) Unix For loop statement
Example: This program will add 1+2+3+4+5 and the result will be 15
for i in 1 2 3 4 5
do
sum=`expr $sum + $i`
done
echo $sum
#2) Unix While loop statement
Example: This program will print the value of ‘a’ five times, from 1 to 5.
a=1
while [ $a -le 5 ]
do
echo “value of a=” $a
a=`expr $a + 1`
done
#3) Unix Until loop statement
This program will print the value of ‘a’ two times from 1 to 2.
a=1
until [ $a -ge 3 ]
do
echo “value of a=” $a
a=`expr $a + 1`
done
While running these loops, there may be a need to break out of the loop in some condition before completing all the iterations or to restart the loop before completing the remaining statements. This can be achieved with the ‘break’ and ‘continue’ statements.
The following program illustrates the ‘break’ operation:
num=1
while [ $num -le 5 ]
do
read var
if [ $var -lt 0 ]
then
break
fi
num=`expr $num + 1`
done
echo “The loop breaks for negative numbers”
Our upcoming tutorial will brief you more about working with Functions in Unix.
Overview of Unix Switch Case Statements: In this tutorial, we will see how a switch case block can be used when creating conditional flow depending on the various values of a single expression. Unix Shell The case-esac Statement Unix Video #16: The Shell Switch Case Syntax and Examples: case <word> in…
Overview of Unix Shell Functions: Shell Functions are used to specify the blocks of commands that may be repeatedly invoked at different stages of execution. The main advantages of using Unix Shell Functions are to reuse the code and to test the code in a modular way. This tutorial will…
Introduction to Unix Shell Scripting: In Unix, the Command Shell is the native command interpreter. It provides a command line interface for the users to interact with the operating system. Unix commands may also be executed non-interactively in the form of a Shell Script. The script is a series of…
Overview of Pipes in Unix Programming: In this tutorial, we will learn more about Unix Pipes. Later, we will work with some of the remaining filter commands and see an example of piping them together. Unix Video #20: Pipes in Unix A series of filter commands can be piped together…
Introduction to Unix Operating System: Let's start with Tutorial #1: 'What is Unix' in this series. In this tutorial, you will be able to understand the basic concepts of operating systems, the features of Unix, along its Architecture. => Click here for the Complete Unix Tutorial series Unix Video #1:…
Overview of Unix Filters Text Processing Utilities: In this tutorial, we will learn about filters and work with various filter commands. Filters are commands that read input from stdin and write output to stdout. By default, when using a shell terminal, the stdin is from the keyboard, and the stdout is…
In this tutorial, we will discuss Java While Loop, comparison between For Loop vs While Loop, etc with programming examples: All the necessary details related to Java while loop will be explained in this tutorial. It includes the syntax, description, and some relevant programming examples along with the logic of…
An In-Depth Look At Loops In C++ Along With Its Types. In addition to Decision-making Constructs that we have seen in our last tutorial, there may arise some situations wherein we have to execute a block of statements repeatedly. Such a situation requires that we have a condition that checks…