Shell中的while循環(huán)幾種使用實(shí)例詳解
1.利用while循環(huán)計(jì)算1到100的和:
示例代碼1:
#!/bin/bash i=1 sum=0 while [ $i -le 100 ] do let sum=sum+$i let i++ done echo $sum
示例代碼2:利用while循環(huán)計(jì)算1到100之間所有奇數(shù)之和
#!/bin/bash i=1 sum=0 while [ $i -le 100 ] do let sum=sum+$i let i+=2 done echo $sum
示例代碼3:利用while循環(huán)計(jì)算1到100之間所有偶數(shù)之和
#!/bin/bash i=2 sum=0 while [ $i -le 100 ] do let sum=sum+$i let i+=2 done echo $sum
2.利用while循環(huán)打印**
示例代碼:利用while循環(huán)打印一個(gè)5x5的*
#!/bin/bash i=1 j=1 while [ $i -le 5 ] do while [ $j -le 5 ] do echo -n "* " let j++ done echo let i++ let j=1 done
3.使用read結(jié)合while循環(huán)讀取文本文件:
示例代碼1:
#!/bin/bash file=$1 #將位置參數(shù)1的文件名復(fù)制給file if [ $# -lt 1 ];then #判斷用戶是否輸入了位置參數(shù) echo "Usage:$0 filepath" exit fi while read -r line #從file文件中讀取文件內(nèi)容賦值給line(使用參數(shù)r會(huì)屏蔽文本中的特殊符號(hào),只做輸出不做轉(zhuǎn)譯) do echo $line #輸出文件內(nèi)容 done < $file
示例2:按列讀取文件內(nèi)容
#!/bin/bash file=$1 if [[ $# -lt 1 ]] then echo "Usage: $0 please enter you filepath" exit fi while read -r f1 f2 f3 #將文件內(nèi)容分為三列 do echo "file 1:$f1 ===> file 2:$f2 ===> file 3:$f3" #按列輸出文件內(nèi)容 done < "$file"
4.while循環(huán)中的死循環(huán):
示例:利用死循環(huán),讓用戶做選擇,根據(jù)客戶的選擇打印相應(yīng)結(jié)果
#!/bin/bash #打印菜單 while : do echo "********************" echo " menu " echo "1.tima and date" echo "2.system info" echo "3.uesrs are doing" echo "4.exit" echo "********************" read -p "enter you choice [1-4]:" choice #根據(jù)客戶的選擇做相應(yīng)的操作 case $choice in 1) echo "today is `date +%Y-%m-%d`" echo "time is `date +%H:%M:%S`" read -p "press [enter] key to continue..." Key #暫停循環(huán),提示客戶按enter鍵繼續(xù) ;; 2) uname -r read -p "press [enter] key to continue..." Key ;; 3) w read -p "press [enter] key to continue..." Key ;; 4) echo "Bye!" exit 0 ;; *) echo "error" read -p "press [enter] key to continue..." Key ;; esac done
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Shell中的循環(huán)語(yǔ)句for、while、until實(shí)例講解
- linux shell循環(huán):for、while、until用法詳解
- Shell中的for和while循環(huán)詳細(xì)總結(jié)
- shell命令while循環(huán)中使用sleep命令代碼示例
- Shell腳本while、until循環(huán)語(yǔ)句簡(jiǎn)明教程
- shell腳本實(shí)戰(zhàn)-while循環(huán)語(yǔ)句
- linux shell常用循環(huán)與判斷語(yǔ)句(for,while,until,if)使用方法
- Windows Powershell Do While 循環(huán)
- Shell循環(huán)語(yǔ)句的使用(for循環(huán)、while循環(huán)、until循環(huán))
- Shell腳本之while循環(huán)應(yīng)用具體案例
相關(guān)文章
Linux 中awk 提取包含某個(gè)關(guān)鍵字的段落
AWK是一種處理文本文件的語(yǔ)言,是一個(gè)強(qiáng)大的文本分析工具。這篇文章主要介紹了Linux 中awk 提取包含某個(gè)關(guān)鍵字的段落實(shí)例代碼,需要的朋友可以參考下2020-01-01linux數(shù)據(jù)庫(kù)備份并通過(guò)ftp上傳腳本分享
這篇文章主要介紹了linux數(shù)據(jù)庫(kù)備份并通過(guò)ftp上傳腳本,腳本還可以備份網(wǎng)站,需要的朋友可以參考下2014-03-03linux文本過(guò)濾grep基礎(chǔ)命令介紹(5)
這篇文章主要為大家詳細(xì)介紹了linux文本過(guò)濾grep基礎(chǔ)命令,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Shell編程之case語(yǔ)句實(shí)戰(zhàn)(小結(jié))
這篇文章主要介紹了Shell編程之case語(yǔ)句實(shí)戰(zhàn)(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Shell中set與shopt命令選項(xiàng)的含義和使用示例
這篇文章主要給大家介紹了關(guān)于Shell中set與shopt命令選項(xiàng)的含義與使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09