Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipe tail output into another script

I am trying to pipe the output of a tail command into another bash script to process:

tail -n +1 -f your_log_file | myscript.sh

However, when I run it, the $1 parameter (inside the myscript.sh) never gets reached. What am I missing? How do I pipe the output to be the input parameter of the script?

PS - I want tail to run forever and continue piping each individual line into the script.

Edit For now the entire contents of myscripts.sh are:

echo $1;
like image 770
slthomason Avatar asked Aug 22 '12 18:08

slthomason


People also ask

How do you pipe the output of a command to another command?

The | command is called a pipe. It is used to pipe, or transfer, the standard output from the command on its left into the standard input of the command on its right. # First, echo "Hello World" will send Hello World to the standard output.

Can you use pipe in shell script?

Pipe may be the most useful tool in your shell scripting toolbox. It is one of the most used, but also, one of the most misunderstood. As a result, it is often overused or misused. This should help you use a pipe correctly and hopefully make your shell scripts much faster and more efficient.

What does pipe to bash do?

A pipe in Bash takes the standard output of one process and passes it as standard input into another process. Bash scripts support positional arguments that can be passed in at the command line.


2 Answers

Generally, here is one way to handle standard input to a script:

#!/bin/bash

while read line; do
    echo $line
done

That is a very rough bash equivalent to cat. It does demonstrate a key fact: each command inside the script inherits its standard input from the shell, so you don't really need to do anything special to get access to the data coming in. read takes its input from the shell, which (in your case) is getting its input from the tail process connected to it via the pipe.

As another example, consider this script; we'll call it 'mygrep.sh'.

#!/bin/bash

grep "$1"

Now the pipeline

some-text-producing-command | ./mygrep.sh bob

behaves identically to

some-text-producing-command | grep bob

$1 is set if you call your script like this:

./myscript.sh foo

Then $1 has the value "foo".

The positional parameters and standard input are separate; you could do this

tail -n +1 -f your_log_file | myscript.sh foo

Now standard input is still coming from the tail process, and $1 is still set to 'foo'.

like image 176
chepner Avatar answered Oct 04 '22 21:10

chepner


Perhaps your were confused with awk?

tail -n +1 -f your_log_file | awk '{
    print $1
}'

would print the first column from the output of the tail command.

In the shell, a similar effect can be achieved with:

tail -n +1 -f your_log_file | while read first junk; do
    echo "$first"
done

Alternatively, you could put the whole while ... done loop inside myscript.sh

like image 29
Henk Langeveld Avatar answered Oct 04 '22 21:10

Henk Langeveld