• 售前

  • 售后

热门帖子
入门百科

shell下同时读取多个文件的方法

[复制链接]
随心所欲480 显示全部楼层 发表于 2021-10-26 13:02:57 |阅读模式 打印 上一主题 下一主题
                        1. 单个文件的读取

            在shell脚本下,可以多种方式实现按行读取文件,如下:
                                   
  1. <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">for</font></span> line <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">in</font></span> <span style="COLOR: #000000; FONT-WEIGHT: bold">`</span><span style="COLOR: #c20cb9; FONT-WEIGHT: bold">cat</span> <span style="COLOR: #800000">${input_filename}</span><span style="COLOR: #000000; FONT-WEIGHT: bold">`</span>
  2. <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">do</font></span>
  3.   <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">echo</font></span> <span style="COLOR: #007800"><font face="">$line</font></span>
  4. <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">done</font></span>
复制代码
                                                         
  1. <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">while</font></span> <span style="COLOR: #c20cb9; FONT-WEIGHT: bold"><font face="">read</font></span> line
  2. <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">do</font></span>
  3.   <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">echo</font></span> <span style="COLOR: #007800"><font face="">$line</font></span>
  4. <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">done</font></span> <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face=""><</font></span> <span style="COLOR: #800000"><font face="">${input_filename}</font></span>
复制代码
                                   其中第二种方式是将文件重定向到标准输入中
            2. 多个文件读取方法

            那怎样实现同时多个文件的读呢?
我们可以继续利用bash中的文件重定向功能,将文件重定向到特定的文件形貌符中,语法如下:
                                   
  1. n<span style="COLOR: #000000; FONT-WEIGHT: bold"><</span><span style="COLOR: #c20cb9; FONT-WEIGHT: bold">file</span>
  2. n<span style="COLOR: #000000; FONT-WEIGHT: bold">></span><span style="COLOR: #c20cb9; FONT-WEIGHT: bold">file</span>
  3. n<span style="COLOR: #000000; FONT-WEIGHT: bold">>></span><span style="COLOR: #c20cb9; FONT-WEIGHT: bold">file</span>
  4. n<span style="COLOR: #000000; FONT-WEIGHT: bold"><></span><span style="COLOR: #c20cb9; FONT-WEIGHT: bold">file</span>
复制代码
                                   这里的n代表打开文件file的文件形貌符,类似其他编程语言中的fd,如果没有指定n,则其默认行为如下:
                                   
  1. <span style="COLOR: #000000; FONT-WEIGHT: bold"><</span><span style="COLOR: #c20cb9; FONT-WEIGHT: bold">file</span>   <span style="FONT-STYLE: italic; COLOR: #666666"><font face="">#same as 0<file</font></span>
  2. <span style="COLOR: #000000; FONT-WEIGHT: bold">></span><span style="COLOR: #c20cb9; FONT-WEIGHT: bold">file</span>  <span style="FONT-STYLE: italic; COLOR: #666666"><font face="">#same as 1>file</font></span>
  3. <span style="COLOR: #000000; FONT-WEIGHT: bold"><></span><span style="COLOR: #c20cb9; FONT-WEIGHT: bold">file</span>   <span style="FONT-STYLE: italic; COLOR: #666666"><font face="">#same as 0<>file</font></span>
复制代码
                                   我们可以通过exec命令来打开所要重定向的文件:
                                   
  1. <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">exec</font></span> <span style="COLOR: #000000">7</span><span style="COLOR: #000000; FONT-WEIGHT: bold"><</span>file1
  2. <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">exec</font></span> <span style="COLOR: #000000">8</span><span style="COLOR: #000000; FONT-WEIGHT: bold"><</span>file2
复制代码
                                   然后我们可以通过read命令来读取对应文件的内容:
                                   
  1. <span style="COLOR: #c20cb9; FONT-WEIGHT: bold"><font face="">read</font></span> data <span style="COLOR: #000000; FONT-WEIGHT: bold"><&</span><span style="COLOR: #000000">7</span> <span style="FONT-STYLE: italic; COLOR: #666666"><font face="">#使用符合是为了区分7是文件描述符,而不是文件名</font></span>
  2. <span style="COLOR: #c20cb9; FONT-WEIGHT: bold"><font face="">read</font></span> data <span style="COLOR: #000000; FONT-WEIGHT: bold"><&</span><span style="COLOR: #000000">8</span>
复制代码
                                   关闭文件

                                   
  1. <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">exec</font></span> <span style="COLOR: #000000">7</span><span style="COLOR: #000000; FONT-WEIGHT: bold"></</span>dev<span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">/</font></span>null
  2. <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">exec</font></span> <span style="COLOR: #000000">8</span><span style="COLOR: #000000; FONT-WEIGHT: bold"></</span>dev<span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">/</font></span>null
复制代码
                                   多文件读取示例代码如下:
                                   
  1. readfiles<span style="COLOR: #7a0874; FONT-WEIGHT: bold">(</span><span style="COLOR: #7a0874; FONT-WEIGHT: bold">)</span> <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">{</font></span>
  2.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> <span style="COLOR: #007800"><font face="">FD1</font></span>=<span style="COLOR: #000000"><font face="">7</font></span>
  3.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> <span style="COLOR: #007800"><font face="">FD2</font></span>=<span style="COLOR: #000000"><font face="">8</font></span>
  4.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> <span style="COLOR: #007800"><font face="">file1</font></span>=<span style="COLOR: #007800"><font face="">$1</font></span>
  5.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> <span style="COLOR: #007800"><font face="">file2</font></span>=<span style="COLOR: #007800"><font face="">$2</font></span>
  6.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> <span style="COLOR: #007800"><font face="">count1</font></span>=<span style="COLOR: #000000"><font face="">0</font></span>
  7.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> <span style="COLOR: #007800"><font face="">count2</font></span>=<span style="COLOR: #000000"><font face="">0</font></span>
  8.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> <span style="COLOR: #007800"><font face="">eof1</font></span>=<span style="COLOR: #000000"><font face="">0</font></span>
  9.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> <span style="COLOR: #007800"><font face="">eof2</font></span>=<span style="COLOR: #000000"><font face="">0</font></span>
  10.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> data1
  11.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">local</font></span> data2
  12.         <span style="FONT-STYLE: italic; COLOR: #666666"><font face=""># Open files.</font></span>
  13.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">exec</font></span> <span style="COLOR: #000000">7</span><span style="COLOR: #000000; FONT-WEIGHT: bold"><</span><span style="COLOR: #007800">$file1</span>
  14.         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">exec</font></span> <span style="COLOR: #000000">8</span><span style="COLOR: #000000; FONT-WEIGHT: bold"><</span><span style="COLOR: #007800">$file2</span>
  15.         <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">while</font></span> <span style="COLOR: #7a0874; FONT-WEIGHT: bold">[</span><span style="COLOR: #7a0874; FONT-WEIGHT: bold">[</span> <span style="COLOR: #007800"><font face="">$eof1</font></span> <span style="COLOR: #660033"><font face="">-eq</font></span> <span style="COLOR: #000000"><font face="">0</font></span>  <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">||</font></span>  <span style="COLOR: #007800"><font face="">$eof2</font></span> <span style="COLOR: #660033"><font face="">-eq</font></span> <span style="COLOR: #000000"><font face="">0</font></span> <span style="COLOR: #7a0874; FONT-WEIGHT: bold">]</span><span style="COLOR: #7a0874; FONT-WEIGHT: bold">]</span>
  16.         <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">do</font></span>
  17.                 <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">if</font></span> <span style="COLOR: #c20cb9; FONT-WEIGHT: bold"><font face="">read</font></span> data1<span style="COLOR: #000000; FONT-WEIGHT: bold"><&</span><span style="COLOR: #007800">$FD1</span>; <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">then</font></span>
  18.                         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">let</font></span> count1++
  19.                         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">printf</font></span> <span style="COLOR: #ff0000"><font face="">"%s, line %d: %s<span style="COLOR: #000099; FONT-WEIGHT: bold">\n</span>"</font></span> <span style="COLOR: #007800"><font face="">$file1</font></span> <span style="COLOR: #007800"><font face="">$count1</font></span> <span style="COLOR: #ff0000"><font face="">"<span style="COLOR: #007800">$data1</span>"</font></span>
  20.                 <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">else</font></span>
  21.                         <span style="COLOR: #007800"><font face="">eof1</font></span>=<span style="COLOR: #000000"><font face="">1</font></span>
  22.                 <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">fi</font></span>
  23.                 <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">if</font></span> <span style="COLOR: #c20cb9; FONT-WEIGHT: bold"><font face="">read</font></span> data2 <span style="COLOR: #000000; FONT-WEIGHT: bold"><&</span><span style="COLOR: #007800">$FD2</span>; <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">then</font></span>
  24.                         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">let</font></span> count2++
  25.                         <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">printf</font></span> <span style="COLOR: #ff0000"><font face="">"%s, line %d: %s<span style="COLOR: #000099; FONT-WEIGHT: bold">\n</span>"</font></span> <span style="COLOR: #007800"><font face="">$file2</font></span> <span style="COLOR: #007800"><font face="">$count2</font></span> <span style="COLOR: #ff0000"><font face="">"<span style="COLOR: #007800">$data2</span>"</font></span>
  26.                 <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">else</font></span>
  27.                         <span style="COLOR: #007800"><font face="">eof2</font></span>=<span style="COLOR: #000000"><font face="">1</font></span>
  28.                 <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">fi</font></span>
  29.         <span style="COLOR: #000000; FONT-WEIGHT: bold"><font face="">done</font></span>
  30. <span style="COLOR: #7a0874; FONT-WEIGHT: bold"><font face="">}</font></span>
  31. <span style="FONT-STYLE: italic; COLOR: #666666"><font face="">#read file1 and file2</font></span>
  32. readfiles file1 file2
复制代码
                                   

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作