我有一个文件,其中包含一系列文件编号(从0开始)。
以下代码与上面的代码有何不同?
显然与管道有关联发生
更新
操作系统:Ubuntu 18.04.1 LTS
Bash: 版本 4.4.19(1)-release (x86_64-pc-linux-gnu)
第二次更新
作为 @Barmar 给出的绝妙答案的补充信息,更多 stdio 缓冲区内容请查看 此链接。
$ cat in.del
0
1
2
....
请问有人能够解释一下这里发生了什么以及除了管道以外还在哪里进行缓冲? 据我所知,head
的标准输入文件描述符(fileno)必须直接指向管道的读取端。
$ cat in.del | ( head -n1 ; head -n1 )
0
60
以下代码与上面的代码有何不同?
$ cat in.del | ( head -n10 ; head -n10 )
0
1
...
8
9
60
1861 # O_o
1862
1863
...
1868
1869
这个工作按预期运行,证明head
本身不会读取比其向stdout
写入的更多字节:
$ ( head -n10 ; head -n10 ) < ./in.del
0
1
...
9
10
11
...
18
19
显然与管道有关联发生
更新
操作系统:Ubuntu 18.04.1 LTS
Bash: 版本 4.4.19(1)-release (x86_64-pc-linux-gnu)
第二次更新
作为 @Barmar 给出的绝妙答案的补充信息,更多 stdio 缓冲区内容请查看 此链接。
variable=$(head -n 1)
来支持我的脚本。我找到的解决方法是改用read -r variable
。这似乎也适用于上面的例子cat in.del | ( read -r line; echo $line ; read -r line ; echo $line )
,以防其他人(或将来的我)仍需要在他们的脚本中使用管道支持。 - bigbear3001