与配合,而在子shell中使用的尾巴/休息不退出循环 [英] Using tail in a subshell in conjunction with while/break does not exit the loop

查看:158
本文介绍了与配合,而在子shell中使用的尾巴/休息不退出循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直面临着shell脚本一个非常奇特的问题。

I have been facing a very peculiar issue with shell scripts.

下面是该方案

SCRIPT1(背景鱼卵) - > SCRIPT2

Script1 (spawns in background)--> Script2

SCRIPT2具有以下code

Script2 has the following code

function check_log()
{
    logfile=$1
    tail -5f ${logfile} | while read line
    do
      echo $line
      if echo $line|grep "${triggerword}";then
        echo "Logout completion detected"
        start_leaks_detection
        triggerwordfound=true
        echo "Leaks detection complete"
      fi
      if $triggerwordfound;then
        echo "Trigger word found and processing complete.Exiting"
        break
      fi

    done
        echo "Outside loop"
        exit 0

}

check_log "/tmp/somefile.log" "Logout detected"

现在在while循环不利于在这里休息。我可以看到注销完成检测以及泄漏检测完成正在呼应的标准输出,而不是字符串外循环

Now the break in while loop does not help here. I can see "Logout completion detected" as well as "Leaks detection complete" being echoed on the stdout, but not the string "outside loop"

我假定这有做尾-f 创建一个子shell的东西。我想要做的是,退出该子shell以及出口SCRIPT2得到控制回SCRIPT1。

I am assuming this has to do something with tail -f creating a subshell. What I want to do is, exit that subshell as well as exit Script2 to get control back to Script1.

有人请阐明如何做到这一点一些轻?

Can someone please shed some light on how to do this?

推荐答案

试试这个,虽然它不是完全一样(它不会跳过启动日志文件的开头):

Try this, although it's not quite the same (it doesn't skip the beginning of the log file at startup):

triggerword=
while [ -z "$triggerword"; do
    while read line; do
        echo $line
        if echo $line|grep "${triggerword}";then
            echo "Logout completion detected"
            start_leaks_detection
            triggerwordfound=true
            echo "Leaks detection complete"
        fi
    done
done < "$logfile"
echo "Outside loop"

双回路有效地做同样的事情为尾-f

这篇关于与配合,而在子shell中使用的尾巴/休息不退出循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆