为什么此外壳管道退出? [英] Why does this shell pipeline exit?

查看:54
本文介绍了为什么此外壳管道退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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会使整个管道退出呢?我假设catbase64head是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆