Shell---loop structure

Source: Internet
Author: User

The loop structure under the Shell is Class C, also has a for loop, while loop, in addition, the shell also has a until loop. For Loop the basic syntax for a for loop

For variable in {list}
do
    statement1
    statement2
... Done

The syntax meaning of the For loop is the same as the C language, that is, the number of times to execute the loop body is determined by the list. The list here is a listing, which can be a series of numbers or strings, separated by spaces between elements

Example: Print out the 1~10 in turn.

1, listing list:

#! /bin/bash for

 I, 1 2 3 4 5 6 7 8 9 do
 echo ' number is
     $i ' done
~            

2. The list can be expressed as: {1..8}

#! /bin/bash for

 i ' {1..8} do
 echo ' number is
     $i '
 done

Run Result:
change the For loop growth step
In the For loop above, the variable can only grow at a time 1,shell allows the user to customize the growth step.
The basic syntax is:

For variable in {start ... End.. Step}
do
    statement1
    statement2
... Done

Start represents the starting value, end indicates the ending value, and step represents the growth value.

For example: output from the For loop 100 odd and:

#! /bin/bash

sum=0 for
i in {1..100..2} does let
    "sum+=i";
Done
echo ' result is: $sum '
for loop with no list
Let's take a look at the positional arguments in several shells:
$: Equivalent to main function parameter Argv[0]
$, $ ...: called positional parameter, the equivalent of main function parameter argv[1],argv[2] ....
$#: Equivalent to the main function of the argc-1, where the # does not represent annotation
$@: The argument list "$", "$", can be used in the for loop in the back
$?: Exit code for the previous command
$$: Process number of the current shell

Example: Output per command line for each parameter

For Val in $@
does
    echo "$val"
done

A loop with no list is implemented as:

For Val
doing
    echo "$val"
done

The above two implementations are equivalent. Class C-style for loops

For ((Expression1;expression2;expression3))
do
    statement1;
    Statement2;
    ...
Done

For example: Print 1~10 in this way

For ((i=1;i<10;i++))
do
    echo ' number is $i '
done
For loop processing array
Basic syntax:
For variable in ${array[*]}
do
    statement1;
    Statement2;
    ...
Done

Example: Print array contents

Array= (1 2 3 4 5 6 7 8)     #这句表示定义一个数组 for
i in ${array[*]}
doing
    echo $i
done
While Loop While loop basic usage
Basic syntax:
While expression
do
    statement1
    statement2
... Done

The syntax of the while loop is still the same as the C language while loop, as long as the condition is satisfied.

Example: Using a while loop to output 1~9 squared

I=1 while
["$i"-lt] does
let
    "Square=i*i"
    echo "$i * $i = $square" Let
    "i+=1"
done

Output results:
until cycle basic usage of until cycle
Basic syntax:

Until expression
do
    statement1
    statement2
... Done

The until cycle is similar to the Do...while loop in C language, which repeats the loop body until a condition is set to exit the loop body.

Example: Use the Until loop to perform the example of the while loop, which prints the square of the 1~9.

I=1
until ["$i"-GT] #这就需要判断i大于10时退出循环 do-let
    "square=i*i"
    echo "$i * $i = $square" Let
    " I+=1 "Done
using break and continue control loops use break to control loops
The function of a break is to jump out of a loop immediately.

Example: the multiplication table within the output 5

#! /bin/bash for ((i=1;i<9;i++)) does for ((
    j=1;j<=i;j++)) does let
        "result= $i * $j"
        printf "$i * $j = $result   "
    done
    printf "\ n"
#当i变量的值为5时退出循环
    If [$i-eq 5]
    then
        break
    fi
Done

The output results are:
using continue control loop
The function of continue is to skip the statement after the sentence in the current loop body and start again from the loop body.

Example: An even number within the output 10

#打印10以内的偶数 for
val in {1..10}
do
    if [["$val 2"-eq 1]]
    then
        continue
    fi
    printf "$val  "
done
printf "\ n"

The results of the operation are:

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.