Shell查看文件的最后5行,并对每行进行正则匹配,代码如下: #!/bin/sh pattern="HeartBeat" /home/test/log/log_20150205.log | while read line do if [[ $line =~ $pattern ]] then echo "Match" else echo "No match" fi echo $line done
shell切分字符串到数组 问题: 对于’aa,bb,cc,dd,ee’这样的字符串输出采用,分隔开的aa bb cc dd ee aa:bb is ok:/home/work按照":"分割开来的aa bb is ok /home/work 解决方法1: #!/bin/bash var=’aa,bb,cc,dd,ee’ var=${var//,/ } #这里是将var中的,替换为空格 for element in $var do echo $element done
写法一: #!/bin/bash while read line do echo $line done < file(待读取的文件) 写法二: #!/bin/bash cat file(待读取的文件) | while read line do echo $line done 写法三: for line in `cat file(待读取的文件)` do echo $line done 说明:for逐行读和while逐行读是有区别的,如: $ cat file aaaa bbbb cccc dddd