gpt4 book ai didi

ruby - 在没有任何选项的情况下发出 gzip 时,Kernel# 卡住而 Kernel#system 没有卡住

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:06 31 4
gpt4 key购买 nike

长话短说,我一直在做一个项目,当我使用时我注意到:

1.9.3p392 :001 > `gzip`
IRB::Abort: abort then interrupt!
from (irb):1:in `call'
from (irb):1:in ``'
from (irb):1
from /usr/local/rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'

它将无限期地等待,直到我按 CTRL + C。

虽然,当我使用:

1.9.3p392 :047 > system('gzip')
gzip: compressed data not written to a terminal. Use -f to force compression.
For help, type: gzip -h
=> false

它会在我不使用 CTRL + C 的情况下继续

为什么使用反引号会阻止进程继续进行?

最佳答案

反引号运算符隐式重定向生成的子 shell 的标准输出(它这样做是为了捕获子 shell 的输出),而 system 则不会。您可以使用 system 观察相同的挂起,如下所示:

system('gzip > /tmp/foo')

这会明确捕获标准输出并将以相同的方式挂起。

gzip 的输出被重定向时,它将等待输入,直到收到 EOF 或其他信号。如果没有输出重定向,它将发出您提到的错误消息。您可以从常规 bash shell 获得相同的效果:

$ gzip
gzip: compressed data not written to a terminal. Use -f to force compression.
For help, type: gzip -h

和:

$ gzip > /tmp/foo
...

... 表示 gzip 将挂起,直到它收到适当的信号。

关于ruby - 在没有任何选项的情况下发出 gzip 时,Kernel# 卡住而 Kernel#system 没有卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18108882/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com