bash 评估来自标准输出的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5783375/
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-09-17 23:50:55  来源:igfitidea点击:

evaluating lines from stdout

bashscripting

提问by j.lee

I have a bash script that is executing a program in a loop. I want to evaluate each line from the stdout and do something if it matches my condition.

我有一个 bash 脚本,它在循环中执行程序。我想评估标准输出中的每一行,如果它符合我的条件就做一些事情。

I still want to be able to see stdout on the screen. Is there a simple way to accomplish this? Thanks!

我仍然希望能够在屏幕上看到标准输出。有没有简单的方法来实现这一点?谢谢!

回答by Oliver Charlesworth

There are several variants of looping over input, but one possibility is thus:

循环输入有多种变体,但一种可能性是:

my_cmd | while read line; do
    echo "$line"
    my_process "$line"
done

回答by Bacon

This should do what you want:

这应该做你想做的:

for string in "a" "b" "c"
do
    output=`echo ${string}`
    echo ${output}
    if [ ${output} == "b" ] ; then
        echo "do something"
    fi
done

Just replace the first echo with your program.

只需用您的程序替换第一个 echo 即可。