記錄:436
場景:?Shell腳本break和continue語句應(yīng)用。在for、while循環(huán)中使用break和continue語句。
版本:CentOS Linux release 7.9.2009。
1.break和continue語句
break語句用來結(jié)束循環(huán)語句,會(huì)跳出循環(huán),不再執(zhí)行循環(huán)語句。比如for循環(huán)、while循環(huán)等。
continue語句用來結(jié)束本次循環(huán),直接跳到下一次循環(huán),如果循環(huán)條件成立,還會(huì)繼續(xù)循環(huán)語句。
2.使用break語句與for循環(huán)語句
2.1腳本
腳本名稱:b2023052831.sh
腳本內(nèi)容:
#!/bin/bash
#1.定義數(shù)組
cityArray=("上海" "蘇州" "南京" "杭州")
#2.獲取數(shù)組長度
lenth=${#cityArray[*]}
#3.使用for循環(huán)遍歷數(shù)組
echo "請輸入需要找的城市名稱:"
read city
index=0
for ((i=0; i<lenth; i++))
do
if [[ "${cityArray[index]}" = ${city} ]];then
echo "循環(huán)$((index+1))次,在列表中找到城市: ${cityArray[index]}"
echo "結(jié)束循環(huán)."
break;
fi
((index++))
done
2.2執(zhí)行與輸出
執(zhí)行命令:bash b2023052831.sh
執(zhí)行結(jié)果:
[root@hadoop211 tutorial]# bash b2023052831.sh
請輸入需要找的城市名稱:
南京
循環(huán)3次,在列表中找到城市: 南京
結(jié)束循環(huán).
3.使用break語句與while循環(huán)語句
3.1腳本
腳本名稱:b2023052832.sh
腳本內(nèi)容:
#!/bin/bash
#1.定義數(shù)組
cityArray=("上海" "蘇州" "杭州" "寧波")
#2.獲取數(shù)組長度
lenth=${#cityArray[@]}
#3.使用while循環(huán)遍歷數(shù)組(使用字符串判斷,=號(hào)兩端需要空格)
index=0
echo "請輸入需要找的城市名稱:"
read city
while :
do
if [[ "${cityArray[index]}" = ${city} ]];then
echo "在數(shù)組中,第$((index+1))個(gè)城市名稱是: ${cityArray[index]}"
echo "已經(jīng)找到符合條件城市,結(jié)束循環(huán)."
break;
fi
((index++))
done
3.2執(zhí)行與輸出
執(zhí)行命令:bash b2023052832.sh
執(zhí)行結(jié)果:
[root@hadoop211 tutorial]# bash b2023052832.sh
請輸入需要找的城市名稱:
寧波
在數(shù)組中,第4個(gè)城市名稱是: 寧波
已經(jīng)找到符合條件城市,結(jié)束循環(huán).
4.使用break語句與select in循環(huán)循環(huán)語句
4.1腳本
腳本名稱:b2023052833.sh
腳本內(nèi)容:
#!/bin/bash
echo "請按照編號(hào)選擇你喜歡的城市名稱."
select item in "上海" "蘇州" "杭州" "南京"
do
if [[ ${item} = "" ]];then
echo "請選擇正確編號(hào)."
else
echo "你選擇城市: ${item}."
break
fi
done
echo "你喜歡的城市是: ${item}."
echo "系統(tǒng)為你推薦${item}相關(guān)內(nèi)容."
4.2執(zhí)行與輸出
執(zhí)行命令:bash b2023052833.sh
執(zhí)行結(jié)果:
[root@hadoop211 tutorial]# bash b2023052833.sh
請按照編號(hào)選擇你喜歡的城市名稱.
1) 上海
2) 蘇州
3) 杭州
4) 南京
#? 3
你選擇城市: 杭州.
你喜歡的城市是: 杭州.
系統(tǒng)為你推薦杭州相關(guān)內(nèi)容.
5.使用continue語句與for循環(huán)循環(huán)語句
5.1腳本
腳本名稱:b2023052834.sh
腳本內(nèi)容:
#!/bin/bash
#1.定義數(shù)組
cityArray=("上海" "蘇州" "南京" "杭州")
#2.獲取數(shù)組長度
lenth=${#cityArray[*]}
#3.使用for循環(huán)遍歷數(shù)組
echo "請輸入不需要打印的城市名稱:"
read city
index=0
for ((i=0; i<lenth; i++))
do
if [[ "${cityArray[index]}" = ${city} ]];then
((index++))
continue
fi
echo "第$((index+1))個(gè)城市名稱: ${cityArray[index]}"
((index++))
done
5.2執(zhí)行與輸出
執(zhí)行命令:bash b2023052834.sh
執(zhí)行結(jié)果:
[root@hadoop211 tutorial]# bash b2023052834.sh
請輸入不需要打印的城市名稱:
南京
第1個(gè)城市名稱: 上海
第2個(gè)城市名稱: 蘇州
第4個(gè)城市名稱: 杭州
6.使用continue語句與while循環(huán)循環(huán)語句
6.1腳本
腳本名稱:b2023052835.sh
腳本內(nèi)容:
#!/bin/bash
#1.定義數(shù)組
cityArray=("上海" "蘇州" "杭州" "寧波")
#2.獲取數(shù)組長度
lenth=${#cityArray[@]}
#3.使用while循環(huán)遍歷數(shù)組(使用字符串判斷,=號(hào)兩端需要空格)
index=0
echo "請輸入不需要找的城市名稱:"
read city
while true
do
if [[ "${cityArray[index]}" = ${city} ]];then
((index++))
continue
fi
echo "在數(shù)組中,第$((index+1))個(gè)城市名稱是: ${cityArray[index]}"
((index++))
if [[ ${index} -ge ${lenth} ]];then
break
fi
done
6.2執(zhí)行與輸出
執(zhí)行命令:bash b2023052835.sh
執(zhí)行結(jié)果:
[root@hadoop211 tutorial]# bash b2023052835.sh
請輸入不需要找的城市名稱:
蘇州
在數(shù)組中,第1個(gè)城市名稱是: 上海
在數(shù)組中,第3個(gè)城市名稱是: 杭州
在數(shù)組中,第4個(gè)城市名稱是: 寧波
以上,感謝。文章來源:http://www.zghlxwxcb.cn/news/detail-463457.html
2023年5月28日文章來源地址http://www.zghlxwxcb.cn/news/detail-463457.html
到了這里,關(guān)于Shell腳本break和continue語句應(yīng)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!