linux - 简单的 shell 。混合管道和输出

标签 linux shell pipe

让我们考虑一下:

 cat > outout.txt | cat > outout2.txt 

我不知道如何解释这样的事情。第二个命令的输入是什么?

最佳答案

当 cat 没有收到文件名参数时,它会从 STDIN 获取输入并将其发送到 STDOUT,因此命令链第一部分的作用是将键入的任何内容(直到 EOD)放入文件输出中。文本:

cat > outout.txt

如果一切正常,此命令不会产生任何输出,因此命令链的第二部分不会将任何内容放入 outout2.txt 中:

cat > outout2.txt

因此文件 outout2.txt 被完整的命令链清空:

$ cat > outout.txt | cat > outout2.txt

但是请注意,如果第一个“cat”调用生成输出,则 outout2.txt 获得一些输出。例如,如果 outout.txt 无法修改,并且我们将 STDERR 输出发送到 STDOUT:

$ chmod a-w outout.txt
$ cat 2>&1 > outout.txt | cat > outout2.txt

然后 outout.txt 将为空,但以下文本将写入 outout2.txt (消息的确切文本可能取决于您的 shell - 我使用的是 bash 3.2):

-bash: outout.txt: Permission denied

关于linux - 简单的 shell 。混合管道和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32696408/

相关文章:

c - wait() 等待子进程崩溃

unix - 在 UNIX 中使用管道更改目录

linux - 通过写入/dev/input/mice 来控制鼠标

linux - 如何通过sysfs访问设备?

linux - 从 UNIX 中的一行中获取选择性详细信息

linux - curl 帖子问题 : input password became empty

数百万的 Angular 管道格式

linux - 以太网校验和是否通过 AF_PACKET 公开?

linux - 从 Linux 服务器向 Windows 手机发送推送通知

linux - 在 Linux Bash 脚本中调用基于 ip 地址的函数