用 zsh 修复损坏的管道
fix broken pipe with zsh
按照此处 https://www.howtogeek.com/howto/30184/10-ways-to-generate-a-random-password-from-the-command-line/ 的建议,我正在尝试使用以下命令生成随机密码:
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;
这在 bash shell 中运行顺利,但在 zsh[= 上给出以下 warning/error 消息22=]:
a8vuFvMzDcV4E-vbbkvfgi1Gf3KYtYiC[1] 40491 broken pipe tr -dc
_A-Z-a-z-0-9 < /dev/urandom |
40492 done head -c${1:-32}
此消息的来源是什么?我们如何调整它以使其在 zsh 上运行?
很难说,因为它在我的两个工作站上 bash 和 zsh 上对我来说完美无缺。
第一个进程 (urandom...) 产生写入第二个进程的无限输出的问题可能不是什么大问题:“head -c${1:-32};echo;”。 =12=]
在第二个进程结束的那一刻,第一个进程仍在继续写入管道并因此给出错误(因为管道被第二个进程关闭)。
这里解释得更好:
https://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error
在同一个 url 中也有一个解决方案,例如:
< /dev/urandom tr -dc _A-Z-a-z-0-9 | tail -n +1 | head -c${1:-32};echo;
但我无法检查它,因为它对我来说已经很好用了。
你能试试吗?
一种解决方法是使用 head
来读取 /dev/urandom 而不是 tr
,就像这样:
< /dev/urandom head -c 1000 | tr -dc _A-Z-a-z-0-9 | head -c 32; echo;
但不能保证我会得到一个 32 个字符的随机序列。将 1000 增加到 100000 会再次导致管道破裂。
如 zsh 邮件列表 https://www.zsh.org/mla/workers/2021/msg01171.html 中所述,这是使用选项 setopt PRINT_EXIT_VALUE
.
时的预期行为
保持该选项设置但不是针对该单个命令的解决方案是 运行 在子 shell 中将其设置为 :
( < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c 32; echo )
按照此处 https://www.howtogeek.com/howto/30184/10-ways-to-generate-a-random-password-from-the-command-line/ 的建议,我正在尝试使用以下命令生成随机密码:
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;
这在 bash shell 中运行顺利,但在 zsh[= 上给出以下 warning/error 消息22=]:
a8vuFvMzDcV4E-vbbkvfgi1Gf3KYtYiC[1] 40491 broken pipe tr -dc _A-Z-a-z-0-9 < /dev/urandom | 40492 done head -c${1:-32}
此消息的来源是什么?我们如何调整它以使其在 zsh 上运行?
很难说,因为它在我的两个工作站上 bash 和 zsh 上对我来说完美无缺。
第一个进程 (urandom...) 产生写入第二个进程的无限输出的问题可能不是什么大问题:“head -c${1:-32};echo;”。 =12=]
在第二个进程结束的那一刻,第一个进程仍在继续写入管道并因此给出错误(因为管道被第二个进程关闭)。
这里解释得更好:
https://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error
在同一个 url 中也有一个解决方案,例如:
< /dev/urandom tr -dc _A-Z-a-z-0-9 | tail -n +1 | head -c${1:-32};echo;
但我无法检查它,因为它对我来说已经很好用了。 你能试试吗?
一种解决方法是使用 head
来读取 /dev/urandom 而不是 tr
,就像这样:
< /dev/urandom head -c 1000 | tr -dc _A-Z-a-z-0-9 | head -c 32; echo;
但不能保证我会得到一个 32 个字符的随机序列。将 1000 增加到 100000 会再次导致管道破裂。
如 zsh 邮件列表 https://www.zsh.org/mla/workers/2021/msg01171.html 中所述,这是使用选项 setopt PRINT_EXIT_VALUE
.
保持该选项设置但不是针对该单个命令的解决方案是 运行 在子 shell 中将其设置为 :
( < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c 32; echo )