Chinaunix

标题: 【关于一个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




我就是不明白这个rec2是怎么出来的。。。。还有为什么是一对一对输出的。。谢谢了。


作者: qq447085256    时间: 2012-09-11 13:56
本帖最后由 qq447085256 于 2012-09-11 14:20 编辑

我能理解他读rec1是每次读一行,因为只指定了一个域,所以第一次肯定读出record 1这个没错,我不太理解的是里层的这个 read rec2。而且接下来输出的record 2
作者: rdcwayx    时间: 2012-09-11 14:53
  1. 07.        read rec2
复制代码

作者: qq447085256    时间: 2012-09-11 18:14
楼上大哥能否说详细点。。。
作者: Shell_HAT    时间: 2012-09-12 13:21
回复 2# qq447085256
  1. #!/bin/sh
  2. # readpair.sh
  3. # first record
  4. while read rec1
  5. do
  6.         # second record
  7.         read rec2
  8.         # further processing/testing goes here to test or compare both records
  9.         echo "我是外层 read rec1 读到的内容 $rec1"
  10.         echo "我是内层 read rec2 读到的内容 $rec2"
  11.         echo "----------------------------"
  12. done < record.txt
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2