【问题标题】:How to loop through output of a command?如何循环输出命令?
【发布时间】:2016-09-26 22:04:04
【问题描述】:

我在 bash 脚本中有一个命令,它给出以下输出

repository: docker/images
tags:
- 0.1-1
- 0.1-2
- 0.1-3
- 0.1-6
- 0.1-7
- 0.1-9

但是,从上面的输出来看,我只需要浏览0.1-1, 0.1-2 ...并运行不同的命令。

请告诉我如何实现这一点

【问题讨论】:

    标签: arrays linux bash shell loops


    【解决方案1】:

    使用grep添加测试以检查行是否以-开头:

    ... | grep '^-' | while read -r line; do echo "$line"; ## Do stuffs; done
    

    由于grep 的输出被缓冲,你可能需要行缓冲,需要GNU grep

    ... | grep --line-buffered '^-' | while ...; do ...; done
    

    或者使用stdbuf:

    ... | stdbuf -oL grep '^-' | while ...; do ...; done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 2012-11-29
      • 2015-11-16
      相关资源
      最近更新 更多