简体   繁体   中英

How do I redirect input typed in a terminal running 'tail -f' and 'grep'?

I have a device that uses the serial line to interface with the user, accepting input and printing traces in return. However, these traces are very verbose and most of the time I'm watching them through another terminal running the commands

tail -f serial.log | grep <myfilter>

However, when I need to input data, I have to switch back to the terminal that is reading (and logging) the serial output that comes from the device.

Is there a way to do this in the same terminal? I want to type into my "grepped" terminal and have this characters reach the device.

You can run your tail command in the background:

{ tail --pid=$! -f serial.log | grep <myfilter>; } &

This will tail the log and print matches to your terminal, but you will still be able to type commands in as usual (to get your PS1 prompt back, press return). To stop the command, just run:

kill -9 $!

As long as you have not started any other background tasks! If you have other background tasks to run, you can store the value of $! immediately after starting the tail command, and use it later to kill it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM