Press "Enter" to skip to content

shell之按行取文件(及查找失效链接)

shell之按行取文件的三种方法,记录一下。

写法一:

#!/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查看当前目录(及子目录)下的失效链接:

for i in `find . -type l  -print`; do if [ ! -e $i ]; then echo not exists: $i; fi; done
Leave a Reply

Your email address will not be published. Required fields are marked *