linux跳出循环的三种方式,shell study-13day--跳出循环(break、continue)
1、跳出循環(break與continue)
(1)跳出循環
在使用循環語句進行循環的過程中,有時候需要在未達到循環結束條件時強制跳出循環,Shell提供了兩個命令來實現該功能:break和continue。
Break:跳出整個循環
break 概述:跳出當前整個循環或結束當前循環,在 for、while 等循環語句中,用于跳出當前所在的循環體,執行循環體之后的語句,后面如果什么也不加,表示跳出當前循環等價于 break 1,也可以在后面加數字,假設 break3 表示跳出第三層循環.
Continue:跳過本次循環,進行下次循環
continue 概述:忽略本次循環剩余的代碼,直接進行下一次循環;在 for、while 等循環語句中,用于跳出當前所在的循環體,執行循環體之后的語句,如果后面加的數字是 1,表示忽略本次條件循環,如果是 2 的話,忽略下來 2 次條件的循環.
(2)實例1
[root@test?shell]#?cat?case.sh
#!/bin/bash
for?((i=0;i<=4;i++))?;?do
echo?$i
case?$i?in
1)
echo?"This?is?one"
;;
2)
continue??#跳出本次循環
echo?"This?is?two"
;;
3)
break??#跳出整個循環
echo?"This?is?three"
;;
4)
echo?"This?is?four"
;;
esac
done
[root@test?shell]#?sh?case.sh
0
1
This?is?one
2
3
[root@test?shell]#
(3)使用交互式方法批量添加用戶[root@test?shell]#?vi?useradd.sh
#!/bin/bash
echo?"*********************"
read?-p?"請輸入要創建的用戶名:"?name
read?-p?"請輸入要創建的用戶數:"?num
read?-p?"請輸入要創建用戶密碼:"?pas
echo?"*********************"
for?((i=1;i<=$num;i=i+1))
do
useradd?$name$i?&>?/dev/null
echo?"$pas"?|?passwd?--stdin?$name$i?&>?/dev/null
done
echo?"創建用戶完成,結果是..."
tail?-$num?/etc/passwd
[root@test?shell]#?sh?useradd.sh
*********************
請輸入要創建的用戶名:test
請輸入要創建的用戶數:1
請輸入要創建用戶密碼:123456
*********************
創建用戶完成,結果是...
test1:x:504:504::/home/test1:/bin/bash
[root@test?shell]#
個人公眾號:
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的linux跳出循环的三种方式,shell study-13day--跳出循环(break、continue)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓makefile与linux,分析l
- 下一篇: 凯迪拉克ct42020款28时尚开多少公