Loop in Unix / Linux
A loop is a program that has powerful features, allowing you to repeat a set of commands. In this chapter, you will learn through practical examples of loops contained in shell programs.
- While loop
- For loop
- Until loop
- Loop select
You will use different loops based on different situations. For example, the while loop will run the command until the given condition remains true, while the until loop will run until the given condition becomes true.
Once you have good program exercises, you will start using the appropriate loop based on the given situation. Here, while and for loops are available in most other program languages such as C, C ++ and PERL etc.
Insert loops in Unix / Linux
All loops support nested concepts, which means you can put a loop inside a similar loop or other loops. This nesting may not limit the maximum number of loops, it depends on your requirements.
Here's an example of nesting a while loop, and in the same way, other loops can be nested according to program requirements.
Insert the while loop in Unix / Linux
It is possible to use a while loop as part of the body of another while loop.
Syntax:
while command1 ; # Day is a big loss, a lot of money is left due to the fact that the command1 is true while command2 ; # Day is dead long, it is dead in the game because of the command of the command2 is true done
For example:
The following is a simple example of loop nesting:
#! / bin / sh a = 0 while [ "$ a" - lt 10 ] # this is loop1 because b = "$ a" while [ "$ b" - ge 0 ] # this is loop2 do echo - n "$ b " b = ` expr $ b - 1` done echo a = `expr $ a + 1` done
It produces the following result. It is important to remember how echo -n works here. Here -n option allows echo to avoid printing a new character line.
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 0
According to Tutorialspoint
Last lesson: Flow control in Shell
Next lesson: Loop control in Unix / Linux
You should read it
May be interested
- File Management in Unix / Linuxall data in unix is organized in files. all files are organized in folders. these directories are organized in a tree structure that is called the file system.
- Signal and Trap in Unix / Linuxsignals are software interrupt signals that are sent to a program that indicates a serious event has occurred. these events can be very diverse from user requests to illegal memory access. some signals, like signal interrupts, indicate that a user has asked the program to do something without being in control.
- Regular Expression in Unix / Linuxa regular expression is a string that can be used to describe different sequences (arrangement) of characters. regular expression is often used by various unix commands, including ed, sed, awk, grep and micro domains.
- Navigation IO in Unix / Linuxmost unix system commands receive input from your terminal and send the output back to your terminal. a command usually reads its input from a location called standard input, which happens to your terminal by default.
- Basic file system in Unix / Linuxa file system is a logical collection of files on a partition or a disk. a partition is an information store and can be combined into a hard disk if desired.
- Techniques cited in Unix / Linuxshell provides a variety of characters that have special meaning while using them in any shell script and cause a limit of a word unless quoted.
- Basic utilities: print, email in Unixby this chapter, you have a few basic insights about unix systems and some of its basic commands. this chapter will briefly discuss some of the basic but important utilities of unix utilities that you will use in your daily activities.
- User management in Unix / Linuxunix supports a concept of group account, which groups a number of accounts logically. each account will be part of any group account. the unix team plays an important role in executing process management and allowing access to files.
- Use variables in Shella variable is a string of characters from which we assign a value. the assigned value can be a number, text, file name, device or any other type of data.
- Micro editor in Unix / Linuxthere are many ways to edit files in unix and for me, one of the best ways is to use the editor to edit the micro screen orientation. this editor lets you edit the lines of content with other lines in the file.