为什么此外壳管道退出? [英] Why does this shell pipeline exit?
问题描述
我有一个Shell管道可以随机生成10个字符的密码:
I have a shell pipeline for generating 10 characters password at random:
cat /dev/urandom | base64 | head -c 10
我的问题是cat /dev/urandom | base64
是无限的输出流,它不会自行退出.但是,为什么附加head -c 10
会使整个管道退出呢?我假设cat
,base64
和head
是3个独立的进程,head
如何使cat
进程退出?
My question is cat /dev/urandom | base64
is an infinite output stream which will not exit by itself. But why does appending head -c 10
make the whole pipeline exit? I assume cat
, base64
and head
are 3 separated processes, how can head
make the cat
process exit?
推荐答案
base64
输出10个字节后,head
获得足够的输入和退出.当前者尝试输出更多字节时,它将收到 SIGPIPE信号,因此也退出.出于同样的原因,cat
将依次退出.
After base64
outputs 10 bytes, head
gets enough inputs and exits. When the former attempts to output more bytes, it will receive SIGPIPE signal and hence exit too。For the same reason, cat
will exit in turn.
这篇关于为什么此外壳管道退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!