繁体   English   中英

如何重定向几个输入之一?

[英]How to redirect one of several inputs?

在Linux / Unix命令行中,当使用具有多个输入的命令时,如何重定向其中之一?

例如,假设我使用cat连接多个文件,但是我只想要一个文件的最后几行,所以我的输入是testinput1testinput2tail -n 4 testinput3

如何在没有任何临时文件的情况下一行完成此操作?

我尝试了tail -n 4 testinput3 | cat testinput1 testinput2 tail -n 4 testinput3 | cat testinput1 testinput2 ,但这似乎只是接受输入1和2。

对不起,标题不好,我不确定如何准确说出它。

bash不会尝试将tail的输出通过管道传递给cat ,而是提供了进程替换 ,在该进程中,其替换或输入或输出连接到FIFO或/dev/fd的文件(例如您的终端tty),从而运行进程替换。 这使您可以将进程的输出视为文件。

通常情况下,通常将过程替换的输出重定向到一个循环中,例如, while read -r line; do ##stuff; done < <(process) while read -r line; do ##stuff; done < <(process) while read -r line; do ##stuff; done < <(process) 但是,在您的情况下, cat将文件本身作为参数而不是从stdin读取,因此您省略了初始重定向,例如

cat file1 file2 <(tail -n4 file3)

因此,请熟悉两种形式;如果需要将流程重定向为输入,请熟悉< <(process)如果您需要将流程的结果视为文件,则请简单地<(process)

暂无
暂无

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

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