管尾输出到另一个脚本 [英] pipe tail output into another script
问题描述
我想管尾命令的输出到另一个bash脚本处理:
尾-n +1 -f your_log_file | myscript.sh
然而,当我运行它,$ 1参数(myscript.sh内)永远不会被达到。我在想什么?我如何管输出为脚本的输入参数?
PS - 我想尾巴永远运行,并继续每根线管道到脚本
。 修改
现在myscripts.sh的全部内容是:
回声$ 1;
一般情况下,这里是处理标准输入到一个脚本的一种方式:
#!/斌/庆典而读线;做
回声$线
DONE
这是一个非常粗糙的bash相当于猫
。它确实说明一个关键的事实:在脚本中的每个命令继承了壳它的标准输入,这样你就不会真正需要做什么特别的事情来获得访问在未来数据读
需要由壳体,它(你的情况)从尾
处理,通过管道连接到它获取其输入的输入。
再举一个例子,考虑这个脚本;我们就叫它'mygrep.sh。
#!/斌/庆典grep的$ 1
现在管道
一些文本生产命令| ./mygrep.sh鲍勃
行为相同
一些文本生产命令|鲍勃的grep
$ 1
设置,如果你打电话给你的脚本是这样的:
./ myscript.sh富
然后 $ 1
有foo值。
的位置参数和标准输入是分开的;你可以这样做
尾-n +1 -f your_log_file | myscript.sh富
现在的标准输入还是从尾
工艺,和 $ 1
仍设置为'富'的到来。
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:
#!/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'.
这篇关于管尾输出到另一个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!