ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'd like to write a bash script what launches a program with a lot of output (this program doesn't stop unless you kill it, but it runs like a daemon). So I need to filter its output in runtime and kill it if this output contains a specific string. Unfortunatelly
./dameon_program | grep -Ei "string"
doesn't a good solution because it cannot capture this string.
Any idea how can I solve this?
grep will wait until your daemon_program ends, so you need to tell grep to stop at the first match.
you do not need -E and -o to grep, but you would need -L. Try:
./daemon_program 2>&1 | grep -Li "LOADFAIL" && pkill daemon_program
bigearsbilly has posted a superior solution, which allows the pipeline to accumulate very little data by breaking the output of the child process into individual lines. I think the other solutions will accumulate the child process output in a buffer until an EOF is received (the child terminates), and then the grep will receive the full complement of buffered data.
bigearsbilly I think the other solutions will accumulate the child process output in a buffer until an EOF is received (the child terminates), and then the grep will receive the full complement of buffered data.
I made a "slow" cat:
while read a
# this line for debug only
echo $a > /dev/tty
done < searchfile | grep -L 'pattern'
without -L it will run until the end of searchfile - regardless of the pattern, but with -L it will be stopped after the first match, grep does not wait "any longer". After the match even the while loop will be finished because the pipe has been broken. (Actually the while loop will print out a few additional debug line by then)