Do a tail -F until matching a pattern

后端 未结 10 1369
遥遥无期
遥遥无期 2020-11-28 09:21

I want to do a tail -F on a file until matching a pattern. I found a way using awk, but IMHO my command is not really clean. The problem is that I need to d

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 10:13

    sh -c 'tail -n +0 --pid=$$ -f /tmp/foo | { sed "/EOF/ q" && kill $$ ;}'
    

    Here the main problem is with $$. If you run command as is, $$ is set not to sh but to the PID of the current shell where command is run.

    To make kill work you need to change kill $$ to kill \$$

    After that you can safely get rid of --pid=$$ passed to tail command.

    Summarising, following will work just fine:

    /bin/sh -c 'tail -n 0 -f /tmp/foo | { sed "/EOF/ q" && kill \$$ ;}
    

    Optionally you can pass -n to sed to keep it quiet :)

提交回复
热议问题