cat file.txt |while read line
do
echo $line
done
或者:
while read line
do
echo $line
done < file.txt
注意:因爲使用while來讀入文件裏的行時,會整行讀入,不會關注行的內容(空格..),因此比for讀文件有更好的適用性,推薦使用while循環讀取文件html
for循環示例 linux
for循環語法:c++
shell
1編程
2數組
3bash
4tcp
5函數
6oop
forVARIABLEin1 2 3 4 5 .. Ndo command1 command2 commandNdone
1
2
3
4
5
6
7
8
9
10
<i>#!/bin/bash
for
i
in
1 2 3 4 5
do
echo
"Welcome $i times"
done
</i>
bash version 3.0+版本
#!/bin/bash foriin{1..5}do echo"Welcome $i times"done
bash version 4版本
#!/bin/bashecho"Bash version ${BASH_VERSION}..."foriin{0..10..2} do echo"Welcome $i times" done
含有「seq」命令的語法示例
#!/bin/bashforiin$(seq 1 2 20)do echo"Welcome $i times"done
for循環的三個表達式
語法以下:
for(( EXP1; EXP2; EXP3 ))do command1 command2 command3done
示例以下:
#!/bin/bash
for
(( c=1; c<=5; c++ ))
do
echo
"Welcome $c times..."
done
效果:
Welcome 1 timesWelcome 2 timesWelcome 3 timesWelcome 4 timesWelcome 5 times
for的無限循環
#!/bin/bashfor(( ; ; ))do echo"infinite loops [ hit CTRL+C to stop]"done
break條件語句
forIin1 2 3 4 5do statements1 #Executedforall values of''I'', up to a disaster-conditionifany. statements2 if(disaster-condition) then break#Abandon the loop. fi statements3 #While good and, no disaster-condition.done
下面的shell腳本將經過在/ etc目錄中存儲的全部文件。 for循環將放棄當/ etc / resolv.conf的文件中找到。
#!/bin/bashforfilein/etc/*do if["${file}"=="/etc/resolv.conf"] then countNameservers=$(grep -c nameserver /etc/resolv.conf) echo"Total ${countNameservers} nameservers defined in ${file}" break fidone
continue條件語句
forIin1 2 3 4 5do statements1 #Executedforall values of''I'', up to a disaster-conditionifany. statements2 if(condition) then continue#Go to next iteration of Iinthe loop and skip statements3 fi statements3done
利用這個腳本在命令行中指定的全部文件名的備份。若是。bak文件存在,它會跳過cp命令。
#!/bin/bashFILES="$@"forfin$FILESdo # if .bak backup file exists, read next file if[ -f ${f}.bak ] then echo"Skiping $f file..." continue# read next file and skip cp command fi # we are hear means no backup file exists, just use cp command to copy file /bin/cp $f $f.bakdone
linux中shell編程for in循環語句的用法:for in語句的格式:for 無$變量 in 字符串do$變量done一簡單的字符串枚舉遍歷法,利用for in格式對字符串按空格切份的功能SERVICES="22 80 25 110 8000 23 20 21 3306 "for x in $SERVICESdoiptables -A INPUT -p tcp --dport $x -m state --state NEW -j ACCEPTdonefor variable in values --------字符串數組依次賦值#!/bin/shfor i in a b c 字符串列表A B C字符串用空格分隔,沒有括號,沒有逗號, 而後循環將其依次賦給變量i變量沒有$doecho "i is $i"done[macg@machome ~]$ sh test.shi is ai is bi is cfor in 裏,變量和*不等價#!/bin/bashfor i in *.h ;#將list設置爲當前目錄下pwd的全部.h結尾文件,不包括以.開頭的隱藏文件docat ${i}.hdone[macg@vm test]$ ./tip.shcat: *.h.h: No such file or directory$i表明的是整個路徑,而不是*.h裏的.h前面的部分改正#!/bin/bashfor i in *.hdocat $idone[macg@vm test]$ echo hahaha >>1.h[macg@vm test]$ echo ha >>2.h[macg@vm test]$ ./tip.shhahahahafor i in /etc/profile.d/*.shdo$idone$i表明的是/etc/profile.d/color.sh,/etc/profile.d/alias.sh, /etc/profile.d/default.shfor in 對(命令行,函數)參數遍歷test(){local ifor i in $* ; doecho "i is $i"done}$*是字符串:以"參數1 參數2 ... " 形式保存全部參數$i是變量i的應用表示[macg@machome ~]$ sh test.sh p1 p2 p3 p4i is p1i is p2i is p3i is p4for in語句與通配符*合用,批量處理文件批量改文件名[root@vm testtip]# lsaaa.txt ccc.txt eee.txt ggg.txt hhh.txt jjj.txt lll.txt nnn.txtbbb.txt ddd.txt fff.txt go.sh iii.txt kkk.txt mmm.txt ooo.txt[root@vm testtip]# cat go.shfor i in *.txt #*.txt至關於一個字符串數組,依次循環賦值給idomv "$i" "$i.bak"done[root@vm testtip]# sh go.sh[root@vm testtip]# lsaaa.txt.bak ccc.txt.bak eee.txt.bak ggg.txt.bak hhh.txt.bak jjj.txt.bak lll.txt.bak nnn.txt.bak bbb.txt.bak ddd.txt.bak fff.txt.bak go.sh iii.txt.bak kkk.txt.bak mmm.txt.bak ooo.txt.bakfor in語句與` `和$( )合用,利用` `或$( )的將多行合爲一行的缺陷,實際是合爲一個字符串數組for i in $(ls *.txt)doecho $idone[macg@machome ~]$ sh test111-tmp.txt111.txt22.txt33.txt或者說,利用for in克服` `和$( ) 的多行合爲一行的缺陷用for in語句自動對字符串按空格遍歷的特性,對多個目錄遍歷LIST="rootfs usr data data2"for d in $LIST; domount /backup/$drsync -ax --exclude fstab --delete /$d/ /backup/$d/umount /backup/$ddone