shell高级技巧-在循环中使用管道的技巧

在Bash Shell中,管道的最后一个命令都是在子Shell中执行的。这意味着在子Shell中赋值的变量对父Shell是无效的。所以当我们将管道输出传送到一个循环结构,填入随后将要使用的变量,那么就会产生很多问题。一旦循环完成,其所依赖的变量就不存在了。
      [root@xieqichao ~]# cat > test8_1.sh
      #!/bin/sh
      #1. 先将ls -l命令的结果通过管道传给grep命令作为管道输入。
      #2. grep命令过滤掉包含total的行,之后再通过管道将数据传给while循环。
      #3. while read line命令从grep的输出中读取数据。注意,while是管道的最后一个命令,将在子Shell中运行。
      ls -l | grep -v total | while read line
      do
          #4. all变量是在while块内声明并赋值的。
          all="$all $line"
          echo $line
      done
      #5. 由于上面的all变量在while内声明并初始化,而while内的命令都是在子Shell中运行,包括all变量的赋值,因此该变量的值将不会传递到while块外,因为块外地命令是它的父Shell中执行。
      echo "all = " $all
      CTRL+D
      [root@xieqichao ~]# ./test8_1.sh
      -rw-r--r--.  1 root root 193 Nov 24 11:25 outfile
      -rwxr-xr-x. 1 root root 284 Nov 24 10:01 test7.sh
      -rwxr-xr-x. 1 root root 108 Nov 24 12:48 test8_1.sh
      all =
  为了解决该问题,我们可以将while之前的命令结果先输出到一个临时文件,之后再将该临时文件作为while的重定向输入,这样while内部和外部的命令都将在同一个Shell内完成。
  <span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># cat &gt; test8_2.sh</span>
  <span class="token comment">#!/bin/sh</span>
  <span class="token comment">#1. 这里我们已经将命令的结果重定向到一个临时文件中。</span>
  <span class="token function">ls</span> -l <span class="token operator">|</span> <span class="token function">grep</span> -v total <span class="token operator">&gt;</span> outfile
  <span class="token keyword">while</span> <span class="token function">read</span> line
  <span class="token keyword">do</span>
      <span class="token comment">#2. all变量是在while块内声明并赋值的。</span>
      all<span class="token operator">=</span><span class="token string">"<span class="token variable">$all</span> <span class="token variable">$line</span>"</span>
      <span class="token keyword">echo</span> <span class="token variable">$line</span>
      <span class="token comment">#3. 通过重定向输入的方式,将临时文件中的内容传递给while循环。</span>
  <span class="token keyword">done</span> <span class="token operator">&lt;</span> outfile
  <span class="token comment">#4. 删除该临时文件。</span>
  <span class="token function">rm</span> -f outfile
  <span class="token comment">#5. 在while块内声明和赋值的all变量,其值在循环外部仍然有效。</span>
  <span class="token keyword">echo</span> <span class="token string">"all = "</span> <span class="token variable">$all</span>
  CTRL+D
  <span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># ./test8_2.sh</span>
  -rw-r--r--.  1 root root   0 Nov 24 12:58 outfile
  -rwxr-xr-x. 1 root root 284 Nov 24 10:01 test7.sh
  -rwxr-xr-x. 1 root root 140 Nov 24 12:58 test8_2.sh
  all <span class="token operator">=</span>  -rwxr-xr-x. 1 root root 284 Nov 24 10:01 test7.sh -rwxr-xr-x. 1 root root 135 Nov 24 13:16 test8_2.sh

  上面的方法只是解决了该问题,然而却带来了一些新问题,比如临时文件的产生容易导致性能问题,以及在脚本异常退出时未能及时删除当前使用的临时文件,从而导致生成过多的垃圾文件等。下面将再介绍一种方法,该方法将同时解决以上两种方法同时存在的问题。该方法是通过HERE-Document的方式来替代之前的临时文件方法。
  <span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># cat &gt; test8_3.sh</span>
  <span class="token comment">#!/bin/sh</span>
  <span class="token comment">#1. 将命令的结果传给一个变量    </span>
  OUTFILE<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">ls</span> -l <span class="token operator">|</span> <span class="token function">grep</span> -v total<span class="token variable">`</span></span>
  <span class="token keyword">while</span> <span class="token function">read</span> line
  <span class="token keyword">do</span>
      all<span class="token operator">=</span><span class="token string">"<span class="token variable">$all</span> <span class="token variable">$line</span>"</span>
      <span class="token keyword">echo</span> <span class="token variable">$line</span>
  <span class="token keyword">done</span> <span class="token operator">&lt;&lt;</span>EOF
  <span class="token comment">#2. 将该变量作为该循环的HERE文档输入。</span>
  <span class="token variable">$OUTFILE</span>
  EOF
  <span class="token comment">#3. 在循环外部输出循环内声明并初始化的变量all的值。</span>
  <span class="token keyword">echo</span> <span class="token string">"all = "</span> <span class="token variable">$all</span>
  CTRL+D
  <span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># ./test8_3.sh</span>
  -rwxr-xr-x. 1 root root 284 Nov 24 10:01 test7.sh
  -rwxr-xr-x. 1 root root 135 Nov 24 13:16 test8_3.sh
  all <span class="token operator">=</span>  -rwxr-xr-x. 1 root root 284 Nov 24 10:01 test7.sh -rwxr-xr-x. 1 root root 135 Nov 24 13:16 test8_3.sh
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了357 篇原创文章</span> · <span>获赞 54</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
发布了272 篇原创文章 · 获赞 272 · 访问量 8144
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览