第八章 shell學習之迴圈和結構化命令

來源:互聯網
上載者:User

標籤:迴圈 結構化命令 shell

for迴圈

1.

列表for迴圈

for variable in {list}  #有些像C++/CLR中的for each

do

...

done

如:

1.

[[email protected] tmp]# cat b.sh 

#! /bin/bash

for i in 1 2 3 4 5      #1 2 3 4 5等價於{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)   #從1增加到6,步幅為2,相當於`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 )     #用命令ls的內容替換$( 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 "$*"   #把輸入的所有參數看做一個字串整體

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]"  #把輸入的參數分開看待

do

echo $i

done

[[email protected] tmp]# ./b.sh 1 2 3

1

2

3

2.

不帶列表的for迴圈

for variable

do

...

done

相當於

for variable in "[email protected]"

do

...

done

3.

類C風格的for迴圈

for((expr1;expr2;expr3))

do

...

done

如:

for((i=1;i<=100;i+=2))

do

...

done

while迴圈

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.類C語言模式

[[email protected] tmp]# cat b1.sh 

#! /bin/bash

echo "please enter start num:"

read i

echo ""$i"~10:"

while ((i!=10))   #注意(()),和[ "$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"  #$#為參數的個數

while [ "$*" != "" ]  #注意空格,等價於[ "$#" != 0 ](把"$#"當做字串)和[ "$#" -ne 0 ](吧$#當做數字)

do

echo "$1"

shift                        #shift為丟棄第一個參數($1),然後$1變為$2,$2變為$3以此類推

done

[[email protected] tmp]# ./b2.sh hello world !

you enter 3 arguments

hello

world

!

until迴圈

和while迴圈類似,只是while當條件為真時繼續迴圈,而until當條件為假時繼續迴圈

[[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

!

控制字元

break 跳出最內層的迴圈

continue 跳出當前迴圈執行下一次的迴圈

select結構

select結構為bash的擴充結構,類似於case,但互動性更好

例:

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          #相當於select color in "[email protected]",類似於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

本文出自 “flyclc” 部落格,請務必保留此出處http://flyclc.blog.51cto.com/1385758/1540232

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.