Chapter 8 shell learning loop and structured commands

Source: Internet
Author: User
Tags control characters

For Loop

1.

List for Loop

For variable in {list} # For each in C ++/CLR

Do

...

Done

For example:

1.

[[Email protected] TMP] # Cat B. Sh

#! /Bin/bash

For I in 1 2 3 4 5 #1 2 3 4 5 is equivalent to {1 .. 5}

Do

Echo $ I

Done

[[Email protected] TMP] #./B. Sh

1

2

3

4

5

2.

[[Email protected] TMP] # Cat B. Sh

#! /Bin/bash

For I in $ (SEQ 1 2 6) # increase from 1 to 6 with a stride of 2, equivalent to 'seq 1 2 6'

Do

Echo $ I

Done

[[Email protected] TMP] #./B. Sh

1

3

5

3.

[[Email protected] TMP] # Cat B. Sh

#! /Bin/bash

For I in $ (LS) # Replace the content with the LS command $ (LS)

Do

Echo $ I

Done

[[Email protected] TMP] #./B. Sh

1c

A

A1

A1 ~

...

4.

[[Email protected] TMP] # Cat B. Sh

#! /Bin/bash

For I in "$ *" # regard all input parameters as a string

Do

Echo $ I

Done

[[Email protected] TMP] # Vim B. Sh

[[Email protected] TMP] #./B. Sh 1 2 3

1 2 3

[[Email protected] TMP] # Cat B. Sh

#! /Bin/bash

For I in "[email protected]" # separate input parameters

Do

Echo $ I

Done

[[Email protected] TMP] #./B. Sh 1 2 3

1

2

3

2.

For Loop without list

For variable

Do

...

Done

Equivalent

For variable in "[email protected]"

Do

...

Done

3.

Class C for Loop

For (expr1; expr2; expr3 ))

Do

...

Done

For example:

For (I = 1; I <= 100; I + = 2 ))

Do

...

Done

While Loop

1.

[[Email protected] TMP] # Cat b1.sh

#! /Bin/bash

Echo "Please enter start num :"

Read I

Echo "" $ I "~ 10 :"

While ["$ I"-ne 10]

Do

Echo $ I

Let "I ++"

Done

[[Email protected] TMP] #./b1.sh

Please enter start num:

6

6 ~ 10:

6

7

8

9

2. Class C mode

[[Email protected] TMP] # Cat b1.sh

#! /Bin/bash

Echo "Please enter start num :"

Read I

Echo "" $ I "~ 10 :"

While (I! = 10) # Note (), which is equivalent to ["$ I"-ne 10]

Do

Echo $ I

Let "I ++"

Done

[[Email protected] TMP] #./b1.sh

Please enter start num:

6

6 ~ 10:

6

7

8

9

3.

[[Email protected] TMP] # Cat b2.sh

#! /Bin/bash

Echo "you enter" $ # "arguments" # $ # indicates the number of parameters.

While ["$ *"! = ""] # Note that spaces are equivalent to ["$ #"! = 0] (use "$ #" as a string) and ["$ #"-Ne 0] (Bar $ # As a number)

Do

Echo "$1"

Shift # shift is to discard the first parameter ($1), then $1 is changed to $2, $2 is changed to $3, and so on.

Done

[[Email protected] TMP] #./b2.sh Hello world!

You enter 3 arguments

Hello

World

!

Until Loop

Similar to the while loop, The while loop continues when the condition is true, while the until loop continues when the condition is false.

[[Email protected] TMP] # Cat b2.sh

#! /Bin/bash

Echo "you enter" $ # "arguments"

Until ["$ #" = 0]

Do

Echo "$1"

Shift

Done

[[Email protected] TMP] #./b2.sh Hello world!

You enter 3 arguments

Hello

World

!

Control characters

Break out of the innermost Loop

Continue jumps out of the current loop and executes the next loop

Select Structure

The Select structure is an extended bash structure, similar to case, but with better interaction

Example:

1.

[[Email protected] TMP] # Cat b3.sh

#! /Bin/bash

Echo "What's your favorite color? "

Select color in "red" "blue" "green"

Do

Break

Done

Echo "you have selected $ color"

[[Email protected] TMP] #./b3.sh

What's your favorite color?

1) Red

2) Blue

3) Green

#? 2

You have selected blue

2.

[[Email protected] TMP] # Cat b3.sh

#! /Bin/bash

Echo "What's your favorite color? "

Select color # is equivalent to select color in "[email protected]", similar to for color.

Do

Break

Done

Echo "you have selected $ color"

[[Email protected] TMP] #./b3.sh Red blue green

What's your favorite color?

1) Red

2) Blue

3) Green

#? 2

You have selected blue

This article is from the "flyclc" blog, please be sure to keep this source http://flyclc.blog.51cto.com/1385758/1540232

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.