Shell Tutorial (iv): Conditional statements, looping statements, Break/continue_shell

Source: Internet
Author: User


In writing a shell script, there may be a situation when you need to take a path of two paths. So, you need to use conditional statements to make the program make the right decisions and perform the right actions.



The UNIX shell supports conditional statements, which perform different operations under different conditions. Here, we will explain the following two decision statements:



· If...else statement



· CASE...ESAC Conditional Statement If...else statement:



The If Else statement is a useful decision statement that you can use to select an option from a given option.



The UNIX shell supports the following form of If. Else statement:



· if...fi statement



· if...else...fi statement



· if...elif...else...fi statement



Most if statements examine the relationship between the relational operators discussed in the previous section. CASE...ESAC statement:



If...elif can perform multiple branches using multiple ELIF statements. However, this is not always the best solution, especially when all branches depend on the value of a single variable.



The UNIX Shell supports CASE...ESAC statement processing precisely because of this situation, it does so more efficiently than IF...ELIF statements.



There is currently only one form of case where detailed CASE...ESAC statements are:



· Case...esacstatement



The UNIX Shell's CASE...ESAC statements are much like the switch...case in other programming languages, such as C or c+ + and Perl.









Loops are a powerful programming tool that allows you to repeat a set of commands. In this tutorial, you will learn the following types of loop shell programs:



· While loop



· For loop



· Until cycle



· Select loop



You will use different loops depending on the situation. For example, a while loop executes a command until the given condition is ture, and loops until a given condition is executed to false.



There are good programming habits that will start to use the situation based on the appropriate loops. Here while and for loops are similar in most other programming languages, such as c,c++ and Perl. Nested loops:



All the concepts that support nested loops, which means that you can put a loop within other similar or different loops. This nesting can go up to an unlimited number of times as needed.



Nested while loops and similar ways that you can nest other loops based on programming requirements The following is an example: nested while loops:



As part of the body of another while loop, it is possible to use a while loop. Grammar:


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
For example:


Here's a simple example of looping nesting, let's add a loop within another countdown loop, 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 results. It is important to pay attention to how echo-n works. Here, the-n option is echo to avoid printing a new line 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 0


So far, we've seen, creating loops and using loops to accomplish different tasks. Sometimes you need to stop the loop or skip the loop iteration.



In this tutorial, you will learn the following two statements to control the Shell loop:



1. Break statement



2. Continue statement infinite Loop:



Cycles of limited life, they jump out, once the condition is false or false depends on the loop.



Because the required condition is not conforming to a loop that may last forever. Never terminates execution of a loop execution infinite number of times. For this reason, such loops are called infinite loops. Example:



The following is a simple example of using a while loop to display the numbers 0 through 9:


#!/bin/sh
 
a=10
 
while [ $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done


This cycle will last forever, because it is usually greater than 10, and it will never become less than 10. So this is the real example of infinite loops. Break statement:



The break statement is used to terminate the execution of the entire loop, and the execution of all line code break statements after completion. And then, it's step by step code with the end of the loop. Grammar



The following break statement will be used to exit the loop:


Break


The following break statement will be used to exit the loop:


Break n


Here n Specifies the nth closed loop exit. Example:



The following is a simple example showing that the loop will terminate as soon as one becomes 5:


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


This will produce the following results:


0
1
2
3
4
5


The following is an example of a simple nested loop. This script breaks two loops, if the var1 equals 2 and var2 equals 0:


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


This will produce the following results. The inner loop has a break command with parameter 2. This shows that if the condition is met it should jump out of the outer loop and eventually jump out of the inner loop.


1 0
1 5
Continue statement:


The Continue statement break command is similar, but it causes the loop exit of the current iteration rather than the entire loop.



This parameter is useful when an error has occurred, but you want to try to execute the next loop iteration. Grammar


Continue


As with the break statement, an integer parameter can skip the command of a nested loop for the Continue command.


Continue N


Here n Specifies the nth closed loop continue. Example:



The following loop returns using the Continue statement and begins processing the next statement:


#!/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
   fi
   echo "Found odd number"
done


This will produce the following results:


Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number

from: http://www.yiibai.com/shell/what_is_shell.html#
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.