十年专注于品牌网站建设 十余年专注于网站建设_小程序开发_APP开发,低调、敢创新、有情怀!
南昌百恒网络微信公众号 扫一扫关注
小程序
tel-icon全国服务热线:400-680-9298,0791-88117053
扫一扫关注百恒网络微信公众号
扫一扫打开百恒网络微信小程序

百恒网络

南昌百恒网络

介绍for循环中continue命令的用法

百恒网络 2017-09-05 12727

for循环中continue命令可以提前中止某次循环中的命令,但并不会完全终止整个循环。可以在循环内部设置shell不执行命令的条件。为了让大家能够对continue命令的用法更加了解,下面南昌网络公司百恒网络在这里就简单举个在for循环中使用continue命令的例子。

$ cat test21

#!/bin/bash

# using the continue command

for (( var1 = 1; var1 < 15; var1++ ))

do

if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]

then

continue

fi

echo "Iteration number: $var1"

done

$ ./test21

Iteration number: 1

Iteration number: 2

Iteration number: 3

Iteration number: 4

Iteration number: 5

Iteration number: 10

Iteration number: 11

Iteration number: 12

Iteration number: 13

Iteration number: 14

$

当if-then语句的条件被满足时(值大于5且小于10),shell会执行continue命令,跳过此 次循环中剩余的命令,但整个循环还会继续。当if-then的条件不再被满足时,一切又回到正轨。

也可以在while和until循环中使用continue命令,但要特别小心。记住,当shell执行 continue命令时,它会跳过剩余的命令。如果你在其中某个条件里对测试条件变量进行增值,问题就会出现。

$ cat badtest3

#!/bin/bash

# improperly using the continue command in a while loop

var1=0

while echo "while iteration: $var1"

[ $var1 -lt 15 ]

do

if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]

then

continue

fi

echo " Inside iteration number: $var1"

var1=$[ $var1 + 1 ]

done

$ ./badtest3 | more

while iteration: 0

Inside iteration number: 0

while iteration: 1

Inside iteration number: 1

while iteration: 2

Inside iteration number: 2

while iteration: 3

Inside iteration number: 3

while iteration: 4

Inside iteration number: 4

while iteration: 5

Inside iteration number: 5

while iteration: 6

while iteration: 6

while iteration: 6

while iteration: 6

while iteration: 6

while iteration: 6

while iteration: 6

while iteration: 6

while iteration: 6

while iteration: 6

while iteration: 6

$

你得确保将脚本的输出重定向到了more命令,这样才能停止输出。在if-then的条件成立之前,所有一切看起来都很正常,然后shell执行了continue命令。当shell执行continue命令时,它跳过了while循环中余下的命令。不幸的是,被跳过的部分正是$var1计数变量增值的地方, 而这个变量又被用于while测试命令中。这意味着这个变量的值不会再变化了,从前面连续的输出显示中你也可以看出来。

和break命令一样,continue命令也允许通过命令行参数指定要继续执行哪一级循环:

continue n

其中n定义了要继续的循环层级。下面南昌百恒网络继续为大家介绍外部for循环的例子。

$ cat test22

#!/bin/bash

# continuing an outer loop

for (( a = 1; a <= 5; a++ ))

do

echo "Iteration $a:"

for (( b = 1; b < 3; b++ ))

do

if [ $a -gt 2 ] && [ $a -lt 4 ]

then

continue 2

fi

var3=$[ $a * $b ]

echo " The result of $a * $b is $var3"

done

done

$ ./test22

Iteration 1:

The result of 1 * 1 is 1

The result of 1 * 2 is 2

Iteration 2:

The result of 2 * 1 is 2

The result of 2 * 2 is 4

Iteration 3:

Iteration 4:

The result of 4 * 1 is 4

The result of 4 * 2 is 8

Iteration 5:

The result of 5 * 1 is 5

The result of 5 * 2 is 10

$

其中的if-then语句:

if [ $a -gt 2 ] && [ $a -lt 4 ]

then

continue 2

fi

此处用continue命令来停止处理循环内的命令,但会继续处理外部循环。注意,值为3的那次迭代并没有处理任何内部循环语句,因为尽管continue命令停止了处理过程,但外部循环依 然会继续。

以上就是南昌网络公司百恒网络为大家介绍的关于for循环中continue命令的用法,通过以上例子,大家是不是觉得很简单呢?确实,只要用心,是真的很简单!如果大家还有哪些不懂得地方,可随时来电和我们联系。此外,本公司专业从事网站建设、APP开发、微信开发等服务,如有需要,欢迎大家来电咨询,洽谈合作!


400-680-9298,0791-88117053
扫一扫关注百恒网络微信公众号
扫一扫打开百恒网络小程序

欢迎您的光顾,我们将竭诚为您服务×

售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售后服务 售后服务
 
售后服务 售后服务
 
备案专线 备案专线
 
×