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;
Generally, here is one way to handle standard input to a script:
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.readtakes its input from the shell, which (in your case) is getting its input from thetailprocess connected to it via the pipe.As another example, consider this script; we’ll call it ‘mygrep.sh’.
Now the pipeline
behaves identically to
$1is set if you call your script like this:Then
$1has the value “foo”.The positional parameters and standard input are separate; you could do this
Now standard input is still coming from the
tailprocess, and$1is still set to ‘foo’.