bash 管道尾部输出到另一个脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12079626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pipe tail output into another script
提问by slthomason
I am trying to pipe the output of a tail command into another bash script to process:
我正在尝试将 tail 命令的输出通过管道传输到另一个 bash 脚本中以进行处理:
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?
但是,当我运行它时,永远不会到达 $1 参数(在 myscript.sh 中)。我错过了什么?如何通过管道将输出作为脚本的输入参数?
PS - I want tail to run forever and continue piping each individual line into the script.
PS - 我希望 tail 永远运行并继续将每一行管道输送到脚本中。
EditFor now the entire contents of myscripts.sh are:
编辑现在 myscripts.sh 的全部内容是:
echo ;
回答by chepner
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. readtakes its input from the shell, which (in your case) is getting its input from the tailprocess connected to it via the pipe.
这是一个非常粗略的 bash 相当于cat. 它确实展示了一个关键事实:脚本中的每个命令都从 shell 继承其标准输入,因此您实际上不需要做任何特殊的事情来访问传入的数据。read从 shell 获取其输入(在您的情况)正在从tail通过管道连接到它的进程中获取输入。
As another example, consider this script; we'll call it 'mygrep.sh'.
作为另一个例子,考虑这个脚本;我们将其称为“mygrep.sh”。
#!/bin/bash
grep ""
Now the pipeline
现在管道
some-text-producing-command | ./mygrep.sh bob
behaves identically to
行为相同
some-text-producing-command | grep bob
$1is set if you call your script like this:
$1如果您像这样调用脚本,则会设置:
./myscript.sh foo
Then $1has the value "foo".
然后$1具有值“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 tailprocess, and $1is still set to 'foo'.
现在标准输入仍然来自tail进程,并且$1仍然设置为'foo'。
回答by Henk Langeveld
Perhaps your were confused with awk?
也许您与awk?
tail -n +1 -f your_log_file | awk '{
print
}'
would print the first column from the output of the tail command.
将从 tail 命令的输出中打印第一列。
In the shell, a similar effect can be achieved with:
在 shell 中,可以通过以下方式实现类似的效果:
tail -n +1 -f your_log_file | while read first junk; do
echo "$first"
done
Alternatively, you could put the whole while ... doneloop inside myscript.sh
或者,您可以将整个while ... done循环放在里面myscript.sh
回答by Russ
Piping connects the output (stdout) of one process to the input (stdin) of another process. stdinis not the same thing as the arguments sent to a process when it starts.
管道将stdout一个进程的输出 ( )连接到另一个进程的输入 ( stdin)。stdin与进程启动时发送给进程的参数不同。
What you want to do is convert the lines in the output of your first process into arguments for the the second process. This is exactly what the xargscommand is for.
您想要做的是将第一个进程的输出中的行转换为第二个进程的参数。这正是xargs命令的用途。
All you need to do is pipe an xargsin between the initial command and it will work:
您需要做的就是xargs在初始命令之间插入一个管道,它将起作用:
tail -n +1 -f your_log_file | xargs | myscript.sh
tail -n +1 -f your_log_file | xargs | myscript.sh

