小技巧,linux下三种方式读取txt文件内容脚本

最近学习bash和shell。

这里提供三种读取txt文件中内中的脚本

方式一

#! /bin/bash
num=0
oldIFS=$IFS
IFS=$'
'
for i in `cat file.txt`;do
    echo $i;
    ((num++))
done
echo "num:$num"
IFS=$oldIFS

方式二

#! /bin/bash
num=0
lines=`cat file.txt | wc -l`
for((i=1;i<=lines;i++));do
	line=`head -$i file.txt | tail -1`
	echo $line
	((num++))
done
echo "num:$num"

  方式三

#! /bin/bash
num=0
while read line;do
    echo $line
    ((num++))
done <file.txt
echo "num:$num"