[英]How to loop through output of a command?
我在 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 ...
并运行不同的命令。
请让我知道如何实现
添加测试以检查该行是否以-
开头,使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.