gpt4 book ai didi

ruby - "open"命令前的管道符号

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:44 25 4
gpt4 key购买 nike

我无意中发现了下面这行代码

open("|cd lib && /opt/jruby/bin/jruby jasper_pdf.rb") { |input| open("log/jasper_pdf.log", "w") { |f| f.write(input.read) } }

cd 命令前的管道符号是做什么用的?

最佳答案

Kernel#open 的 Ruby 文档说:

If path starts with a pipe character ("|"), a subprocess is created, connected to the caller by a pair of pipes. The returned IO object may be used to write to the standard input and read from the standard output of this subprocess.

在您的情况下,它用于将命令 /opt/jruby/bin/jruby jasper_pdf.rb 生成的进程的输出记录到文件 log/jasper_pdf.log.

它大致相当于使用Open3像这样的模块:

require 'open3'
Open3.popen2e('cd lib && /opt/jruby/bin/jruby jasper_pdf.rb') do |_, output, _|
open('log/jasper_pdf.log', 'w') do |f|
f.write(output.read)
end
end

关于ruby - "open"命令前的管道符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21047413/

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