568数据 568数据


shell按行读取文件的3种方法

网络编程 shell按行读取文件的3种方法 06-21
方法有很多,下面写出三种方法:
写法一:
#!/bin/bash
while read line
do
echo $line
done < filename(待读取的文件)


写法二:
#!/bin/bash
cat filename(待读取的文件) | while read line
do
echo $line
done


写法三:
for line in `cat filename(待读取的文件)`
do
echo $line
done


说明:
for逐行读和while逐行读是有区别的,如:
$ cat file
1111
2222
3333 4444 555

$ cat file | while read line; do echo $line; done
1111
2222
3333 4444 555

$ for line in $(<file); do echo $line; done
1111
2222
3333
4444
555

shell脚本递归遍历目录及子目录的例子分享
用shell写的递归遍历目录的脚本,脚本实现递归遍历指定目录,打印目录下的文件名。实例1:#!/bin/shfunctionscandir(){localcur_dirparent_dirworkdirworkdir=$1cd${workdir

shell脚本正则匹配文件中的Email并写入到文件中代码分享
代码如下:#!/bin/bashfunctionread_file(){forlinein`cat$1`doif[`echo$line|grep"^[a-zA-Z0-9_-]*@[A-Za-z_-]*.[a-zA-Z_-]*$"`];thenecho$lineresult.txtelseecho"---"result.txtfidone}#egread_filea.txt

linux BASH shell下设置字体及背景颜色
BASHshell下设置字体及背景颜色echo-e"e[31mteste[41m"e[30m将字符的显示颜色改为黑色e[31m将字符的显示颜色改为红色e[32m将字符的显示颜色改为绿色e[33m将


编辑:568数据

标签:递归,遍历,颜色,文件,脚本