美文网首页
continue,break,shift

continue,break,shift

作者: 素瑾汐 | 来源:发表于2017-07-04 15:36 被阅读0次

循环控制语句

循环控制语句:continue,break,shift
用于循环体中

continue

continue [N]:提前结束第N层的本轮循环,而直接进入下一轮判断;最内层为第1层

while CONDITION1;do
      CMD1
      ...
      if CONDITION2; then
                continue
      fi
      CMDn
      ...
done

break

break[N]:提前结束第N层循环,最内层为第1层

while CONDITION1; do
      CMD1
      ...
      if CONDITION2; then
                break
      fi
      CMDn
      ...
done

continue和break
continue:提前结束本次循环,提前进入下一轮循环,continue 2 跳出本次内层循环,进入外层循环
break:结束本次循环(整个),退出脚本

[root@centos SC]#vim test.sh
#!/bin/bash
for i in {1..10}
do
        [ $i -eq 5 ] && break
        echo i=$i
        sleep 0.5
done
echo test is finished
~                                                                                            
                                                                                             
▽                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
"test.sh" [New] 8L, 104C written                                           
[root@centos SC]#chmod +x test.sh
[root@centos SC]#./test.sh       
i=1
i=2
i=3
i=4
test is finished
[root@centos SC]#vim test.sh     
#!/bin/bash
for i in {1..10}
do
        [ $i -eq 5 ] && continue
        echo i=$i
        sleep 0.5
done
echo test is finished
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
"test.sh" 8L, 107C written                                                 
[root@centos SC]#./test.sh  
i=1
i=2
i=3
i=4
i=6
i=7
i=8
i=9
i=10
test is finished

shift

shift[N]:用于将参量列表 list 左移指定次数,缺省为左移一次。
参量列表 list 一旦被移动,最左端的那个参数就从列表中删 除。while 循环遍历位置参量列表时,常用到 shift
./doit.sh a b c d e f g h
./shfit.sh a b c d e f g h

[root@centos SC]#vim doit.sh
#!/bin/bash
while [ $# -gt 0 ]
do
        echo $*
        shift
done
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
                                                                                             
▽                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
[root@centos SC]#./doit.sh a b c d e f g h
a b c d e f g h
b c d e f g h
c d e f g h
d e f g h
e f g h
f g h
g h
h
[root@centos SC]#vim shift.sh             
#!/bin/bash
until [ -z "$1" ]
do
        echo "$1"
        shift
done
echo
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
[root@centos SC]#./shfit.sh a b c d e f g h    
-bash: ./shfit.sh: No such file or directory
[root@centos SC]#./shift.sh a b c d e f g h  
a
b
c
d
e
f
g
h

相关文章

网友评论

      本文标题:continue,break,shift

      本文链接:https://www.haomeiwen.com/subject/xgpthxtx.html