简体   繁体   English

如何在自定义 pipe 命令脚本中使用 bash tail 命令?

[英]How to use bash tail command inside a custom pipe command script?

I want to use tail in my custom pipe command.我想在我的自定义 pipe 命令中使用 tail 。

For example, I want to execute this command:例如,我想执行这个命令:

>ls -1 | tail -n 1 | awk '{print "last file is "$1}'
>last file is test.txt

And I want to make it short by making my own custom script.我想通过制作我自己的自定义脚本来缩短它。 It looks like this:它看起来像这样:

>ls -1 | myscript
>last file is test.txt

I know myscript can get input from "ls -1" by this code:我知道 myscript 可以通过以下代码从“ls -1”获取输入:

while read line; do
    echo last file is $line
done

But I don't know how to use "tail -n 1" in the custom pipe command code above.但我不知道如何在上面的自定义 pipe 命令代码中使用“tail -n 1”。

Is there a way to use a pipe command in another pipe command script?有没有办法在另一个 pipe 命令脚本中使用 pipe 命令?

Or do I have to implement the code which does the same process as "tail -n 1" myself?还是我必须自己实现与“tail -n 1”执行相同过程的代码?

I hope bash has some solution for this.我希望 bash 对此有一些解决方案。

Try putting just this in myscript试着把这个放在myscript

tail -n 1 | awk '{print "last file is "$1}'

This works as the first command (tail) consumes the stdin of your script.这可以作为第一个命令(tail)使用脚本的标准输入。 In general, scripts work as though you typed their contest as-is to the terminal.通常,脚本的工作方式就像您在终端上按原样键入他们的比赛一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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