标题: 【关于一个whlie语句的问题。急。谢谢】 [打印本页] 作者: qq447085256 时间: 2012-09-11 13:49 标题: 【关于一个whlie语句的问题。急。谢谢】 每次读一对记录
有时可能希望每次处理两个记录,也许可从记录中进行不同域的比较。每次读两个记录
很容易,就是要在第一个w h i l e语句之后将第二个读语句放在其后。使用这项技术时,不要忘
了不断进行检查,因为它实际上读了大量的记录。
[root@localhost ~]# cat record.txt
record 1
record 2
record 3
record 4
record 5
record 6
每次读两个记录,下面的例子对记录并不做实际测试。
脚本如下:
01.#!/bin/sh
02.# readpair.sh
03.# first record
04.while read rec1
05.do
06. # second record
07. read rec2
08. # further processing/testing goes here to test or compare both records
09. echo "This is record on of a pair rec1"
10. echo "This is record on of a pair rec2"
11. echo "----------------------------"
12.done < record.txt
首先来检查确实读了很多记录,可以使用w c命令:
[root@localhost ~]# cat record.txt | wc -l
6
共有6个记录,观察其输出:
This is record on of a pair :record 1
This is record on of a pair :record 2
----------------------------
This is record on of a pair :record 3
This is record on of a pair :record 4
----------------------------
This is record on of a pair :record 5
This is record on of a pair :record 6