我可以从 Ruby 中的系统调用获得连续输出吗?
当您在 Ruby 脚本中使用系统调用时,您可以获得该命令的输出,如下所示:
output = `ls`
puts output
这就是 这个问题是关于。
但是有没有办法显示系统调用的连续输出?例如,如果您运行此安全复制命令,通过 SSH 从服务器获取文件:
scp user@someserver:remoteFile /some/local/folder/
... 它会随着下载进度显示连续输出。但这:
output = `scp user@someserver:remoteFile /some/local/folder/`
puts output
...并没有捕获该输出。
如何从 Ruby 脚本内部显示下载的持续进度?
When you use a system call in a Ruby script, you can get the output of that command like this:
output = `ls`
puts output
That's what this question was about.
But is there a way to show the continuous output of a system call? For example, if you run this secure copy command, to get a file from a server over SSH:
scp user@someserver:remoteFile /some/local/folder/
... it shows continuous output with the progress of the download. But this:
output = `scp user@someserver:remoteFile /some/local/folder/`
puts output
... doesn't capture that output.
How can I show the ongoing progress of the download from inside my Ruby script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试:
Try:
我认为使用 ruby 标准库来处理 SCP(而不是分叉 shell 进程)会更好。 Net::SCP 库(以及整个 Net::* 库)功能齐全,可与 Capistrano 一起使用来处理远程命令。
查看 http://net-ssh.rubyforge.org/ 了解可用内容的概要。
I think you would have better luck using the ruby standard library to handle SCP (as opposed to forking a shell process). The Net::SCP library (as well as the entire Net::* libraries) are full featured and used with Capistrano to handle remote commands.
Checkout http://net-ssh.rubyforge.org/ for a rundown of what is available.
托克兰回答了我提出的问题,但我最终使用了亚当的方法。这是我完成的脚本,它确实显示下载的字节数,以及完成的百分比。
Tokland answered the question as I asked it, but Adam's approach was what I ended up using. Here was my completed script, which does show a running count of bytes downloaded, and also a percentage complete.
你试过 IO.popen 吗?
您应该能够在进程仍在运行时读取输出并相应地解析它。
have you tried with IO.popen ?
you should be able to read the output while the process is still running and parse it accordingly.
将 stderr 重定向到 stdout 可能对您有用:
这应该捕获 stderr 和 stdout。您只能通过丢弃 stdout 来捕获 stderr:
然后您可以使用 IO.popen。
Redirecting stderr to stdout may work for you:
That should capture both stderr and stdout. You can capture stderr only by throwing away stdout:
You can then use
IO.popen
.