- Unix / Linux - The vi Editor
- Unix / Linux - Communication
- Unix / Linux - Processes
- Unix / Linux - Pipes & Filters
- Unix / Linux - Basic Utilities
- Unix / Linux - Environment
- Unix / Linux - File Permission
- Unix / Linux - Directories
- Unix / Linux - File Management
- Unix / Linux - Getting Started
- Unix / Linux - Home
Unix / Linux Shell Programming
- Unix / Linux - Manpage Help
- Unix / Linux - Shell Functions
- Unix / Linux - IO Redirections
- Unix / Linux - Quoting Mechanisms
- Unix / Linux - Shell Substitutions
- Unix / Linux - Loop Control
- Unix / Linux - Shell Loops
- Unix / Linux - Decision Making
- Unix / Linux - Basic Operators
- Unix / Linux - Using Arrays
- Unix / Linux - Special Variables
- Unix / Linux - Using Variables
- Unix / Linux - What is Shell?
- Unix / Linux - Shell Scripting
Advanced Unix / Linux
- Unix / Linux - Signals and Traps
- Unix / Linux - System Logging
- Unix / Linux - System Performance
- Unix / Linux - User Administration
- Unix / Linux - File System Basics
- Unix / Linux - Regular Expressions
Unix / Linux Useful Resources
- Unix / Linux - Discussion
- Unix / Linux - Useful Resources
- Unix / Linux - Commands List
- Unix / Linux - System Calls
- Unix / Linux - Builtin Functions
- Unix / Linux - Quick Guide
- Unix / Linux - Useful Commands
- Unix / Linux - Questions & Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Unix / Linux - Shell Loop Types
In this chapter, we will discuss shell loops in Unix. A loop is a powerful programming tool that enables you to execute a set of commands repeatedly. In this chapter, we will examine the following types of loops available to shell programmers −
You will use different loops based on the situation. For example, the while loop executes the given commands until the given condition remains true; the until loop executes until a given condition becomes true.
Once you have good programming practice you will gain the expertise and thereby, start using appropriate loop based on the situation. Here, while and for loops are available in most of the other programming languages pke C, C++ and PERL, etc.
Nesting Loops
All the loops support nesting concept which means you can put one loop inside another similar one or different loops. This nesting can go up to unpmited number of times based on your requirement.
Here is an example of nesting while loop. The other loops can be nested based on the programming requirement in a similar way −
Nesting while Loops
It is possible to use a while loop as part of the body of another while loop.
Syntax
while command1 ; # this is loop1, the outer loop do Statement(s) to be executed if command1 is true while command2 ; # this is loop2, the inner loop do Statement(s) to be executed if command2 is true done Statement(s) to be executed if command1 is true done
Example
Here is a simple example of loop nesting. Let s add another countdown loop inside the loop that you used to count to nine −
#!/bin/sh a=0 while [ "$a" -lt 10 ] # this is loop1 do b="$a" while [ "$b" -ge 0 ] # this is loop2 do echo -n "$b " b=`expr $b - 1` done echo a=`expr $a + 1` done
This will produce the following result. It is important to note how echo -n works here. Here -n option lets echo avoid printing a new pne character.
0 1 0 2 1 0 3 2 1 0 4 3 2 1 0 5 4 3 2 1 0 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0Advertisements