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