Control loop in Unix / Linux

The previous chapter has been shown how to create loops and work with them to perform various tasks. Sometimes you need to stop a loop or continue their repetitive process.

In this chapter, you will learn two commands used in Shell loop control:

  1. Break command
  2. Continue statement

Infinite loop in Unix / Linux

All loops have a limited lifecycle and they end when the condition is true or false depending on the loop.

A loop can continue to proceed permanently because it does not match the required conditions. An infinite loop is a loop that runs for countless times.

For example

Here is a simple example of using a while loop to display numbers from 0 to 9.

 #! / bin / sh a = 10 while [ $ a - ge 10 ] by echo $ aa = `expr $ a + 1` done 

This loop is an infinite loop because a is always greater than or equal to 10 and it will never be less than 10.

Break command in Unix / Linux

The break command is used to end the running process of a full loop, after completion of running all the lines in the code to this break command. It then steps out of the code after finishing the loop.

Syntax

The following break statement is used to exit a loop.

 break 

The break command can be used to exit a loop using the following format:

 break n 

Here n identifies the nth loop to be surrounded to exit the word:

For example

The following example only the following loop will end immediately after the condition becomes 5:

 #! / bin / sh a = 0 while [ $ a - lt 10 ] by echo $ a if [ $ a - eq 5 ] then break fi a = `expr $ a + 1` done 

It will display the following result:

 0 1 2 3 4 5 

Here is a simple example of loop nesting. Script exits both loops if var1 equals 2 and var2 equals 0.

 #! / bin / sh for var1 in 1 2 3 due to var2 in 0 5 because if [ $ var1 - eq 2 - a $ var2 - eq 0 ] then break 2 else echo "$ var1 $ var2" fi done done 

It will produce the following result. In the inner loop you have a break statement with argument 2. It indicates that if the condition is satisfied you should exit the outer loop.

 1 0 1 5 

The continue command in Unix / Linux

This continue statement is similar to the break statement, except that it causes the current iteration of the loop to exit, not the entire loop.

This command is used when an error has occurred and you want to try to run the next iteration of the loop.

Syntax:

 tiếp tục 

As with the break statement, an integer argument can be provided to the continue statement to jump through this command from the nested loop.

 continue n 

Here n identifies the n-loop loop to continue from there.

For example

The following loop uses the continue command that comes back from the continue command and starts the next command.

 #! / bin / sh NUMS = "1 2 3 4 5 6 7" for NUM in $ NUMS do Q = `expr $ NUM% 2` if [ $ Q - eq 0 ] then echo " Number is an even number !! " continue "Found odd number" done 

It will produce the following result:

 Tìm số thứ tự số không là một số thứ tự !! Tìm số thứ tự số không là một số thứ tự !! Tìm số thứ tự số không là một số thứ tự !! Found odd number 

According to Tutorialspoint

Previous article: Loop in Unix / Linux

Next article: Shell replacement

4 ★ | 1 Vote

May be interested

  • The loop technique in PythonThe loop technique in Python
    in this python lesson, you'll learn how to control the execution of a loop using loop control statements like break and continue.
  • Useful commands in Unix / LinuxUseful commands in Unix / Linux
    this chapter lists the commands, including syntax and brief descriptions. for more details about these commands, you use.
  • Instructions for installing Unix / LinuxInstructions for installing Unix / Linux
    an important unix concept is the environment, which is defined by environment variables. some are set up by the system, others are set up by you, by the shell or any program you download.
  • File Management in Unix / LinuxFile Management in Unix / Linux
    all 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.
  • Regular Expression in Unix / LinuxRegular Expression in Unix / Linux
    a 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 / LinuxNavigation IO in Unix / Linux
    most 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 / LinuxBasic file system in Unix / Linux
    a 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 / LinuxTechniques cited in Unix / Linux
    shell 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.
  • Loop in programming CLoop in programming C
    similar to other programming languages, c also provides us with many control structures and allows you to perform complex parts.
  • Basic utilities: print, email in UnixBasic utilities: print, email in Unix
    by 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.