首页 > 代码库 > 第八章 shell学习之循环和结构化命令

第八章 shell学习之循环和结构化命令

for循环

1.

列表for循环

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

do

...

done

如:

1.

[root@localhost 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

[root@localhost tmp]# ./b.sh 

1

2

3

4

5

2.

[root@localhost tmp]# cat b.sh 

#! /bin/bash

for i in $(seq 1 2 6)   #从1增加到6,步幅为2,相当于`seq 1 2 6`

do

echo $i

done

[root@localhost tmp]# ./b.sh 

1

3

5

3.

[root@localhost tmp]# cat b.sh 

#! /bin/bash

for i in $( ls )     #用命令ls的内容替换$( ls )

do

echo $i

done

[root@localhost tmp]# ./b.sh 

1c

a

a1

a1~

...

4.

[root@localhost tmp]# cat b.sh 

#! /bin/bash

for i in "$*"   #把输入的所有参数看做一个字符串整体

do

echo $i

done

[root@localhost tmp]# vim b.sh 

[root@localhost tmp]# ./b.sh 1 2 3

1 2 3

[root@localhost tmp]# cat b.sh 

#! /bin/bash

for i in "$@"  #把输入的参数分开看待

do

echo $i

done

[root@localhost tmp]# ./b.sh 1 2 3

1

2

3

2.

不带列表的for循环

for variable

do

...

done

相当于

for variable in "$@"

do

...

done

3.

类C风格的for循环

for((expr1;expr2;expr3))

do

...

done

如:

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

do

...

done

while循环

1.

[root@localhost 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

[root@localhost tmp]# ./b1.sh 

please enter start num:

6

6~10:

6

7

8

9

2.类C语言模式

[root@localhost 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

[root@localhost tmp]# ./b1.sh 

please enter start num:

6

6~10:

6

7

8

9

3.

[root@localhost 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

[root@localhost tmp]# ./b2.sh hello world !

you enter 3 arguments

hello

world

!

until循环

和while循环类似,只是while当条件为真时继续循环,而until当条件为假时继续循环

[root@localhost tmp]# cat b2.sh 

#! /bin/bash

echo "you enter "$#" arguments"

until [ "$#" = 0 ]

do

echo "$1"

shift

done

[root@localhost tmp]# ./b2.sh hello world !

you enter 3 arguments

hello

world

!

控制字符

break 跳出最内层的循环

continue 跳出当前循环执行下一次的循环

select结构

select结构为bash的扩展结构,类似于case,但交互性更好

例:

1.

[root@localhost 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"

[root@localhost tmp]# ./b3.sh

what‘s your favorite color?

1) red

2) blue

3) green

#? 2

you have selected blue

2.

[root@localhost tmp]# cat b3.sh 

#! /bin/bash

echo "what‘s your favorite color?"

select color          #相当于select color in "$@",类似于for color的情况

do

break

done

echo "you have selected $color"

[root@localhost 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