我现在有一个有两列数据的文件 test.dat,想使用shell读入。4.682800 1.794250
3.785700 3.588550
2.888600 5.382850
1.991400 7.177100
1.094300 8.971350
0.197100 10.765650
直接给出测试脚本 read_file.sh:#!/bin/sh
# Test reading content of file to variables by shell
# Email: xianbao.d # gmail.com
# Website: http://www.52souji.net/
infile=$1
while read xcoord ycoord
do
echo $ycoord
done < $infile
运行脚本,会将文件中第二列输出。
脚本中,$infile 指定了输入文件
read xcoord ycoord 实际上是将文件的每一行分成两部分来读取:第一部分读入xcoord,第二部分读入ycoord,两部分以空格分隔。
如果一行有三个数据,会是什么样呢?
这个你可以很容易测试一下,结果是xcoord读入了第一个数据,后面两个数据都会被读入ycoord。