Linux Bash 读取输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5853400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:53:25  来源:igfitidea点击:

Bash read output?

linuxbashubuntuterminal

提问by wKavey

I'm running this at a terminal on Ubuntu 11.4.

我在 Ubuntu 11.4 的终端上运行它。

Say I execute a bash script and the output is:

假设我执行一个 bash 脚本,输出是:

Test1: Some text...
Test2: Some text...
Test3: Some text...

How would I, in the same bash script, store the above output as one or more variables?

我如何在同一个 bash 脚本中将上述输出存储为一个或多个变量?

The ideal solution would be for it to be ready to be used in a conditional like so: (line one of output would be stored in $ln1etc.)

理想的解决方案是准备好在这样的条件下使用:(输出的第一行将存储在$ln1等中)

if [ $ln1 = "Test1: Some text..." ] ; then

采纳答案by glenn Hymanman

So you want

所以你要

output=$(command)
while read -r line; do
    process "$line"
done <<< "$output"

See "Here strings" in the Bash manual.

请参阅Bash 手册中的“此处字符串” 。

or process substitution

过程替换

while read -r line; do
    process "$line"
done < <(command)