Bash读取输出? [英] Bash read output?
本文介绍了Bash读取输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Ubuntu 11.4上的一个终端上运行它.
I'm running this at a terminal on Ubuntu 11.4.
说我执行一个bash脚本,输出为:
Say I execute a bash script and the output is:
Test1: Some text...
Test2: Some text...
Test3: Some text...
在相同的bash脚本中,如何将以上输出存储为一个或多个变量?
How would I, in the same bash script, store the above output as one or more variables?
理想的解决方案是准备在如下条件下使用:(输出的第一行将存储在$ln1
等中)
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 $ln1
etc.)
if [ $ln1 = "Test1: Some text..." ] ; then
推荐答案
所以你想要
output=$(command)
while read -r line; do
process "$line"
done <<< "$output"
请参见 Bash手册中的此处的字符串".
See "Here strings" in the Bash manual.
或流程替换
while read -r line; do
process "$line"
done < <(command)
这篇关于Bash读取输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文