Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以从Ruby中的系统调用中获得连续输出吗?_Ruby_Bash_Scripting_Scp - Fatal编程技术网

我可以从Ruby中的系统调用中获得连续输出吗?

我可以从Ruby中的系统调用中获得连续输出吗?,ruby,bash,scripting,scp,Ruby,Bash,Scripting,Scp,在Ruby脚本中使用系统调用时,可以获得该命令的输出,如下所示: output = `ls` puts output 这就是为什么 但是,有没有一种方法可以显示系统调用的连续输出?例如,如果运行此secure copy命令,要通过SSH从服务器获取文件: scp user@someserver:remoteFile /some/local/folder/ 。。。它显示下载进度的连续输出。但这是: output = `scp user@someserver:remoteFile /some/l

在Ruby脚本中使用系统调用时,可以获得该命令的输出,如下所示:

output = `ls`
puts output
这就是为什么

但是,有没有一种方法可以显示系统调用的连续输出?例如,如果运行此secure copy命令,要通过SSH从服务器获取文件:

scp user@someserver:remoteFile /some/local/folder/
。。。它显示下载进度的连续输出。但这是:

output = `scp user@someserver:remoteFile /some/local/folder/`
puts output
。。。不会捕获该输出


如何在我的Ruby脚本中显示下载的持续进度?您尝试过IO.popen吗? 您应该能够在进程仍在运行时读取输出,并相应地对其进行分析。

尝试:

IO.popen("scp -v user@server:remoteFile /local/folder/").each do |fd|
  puts(fd.readline)
end

我认为使用ruby标准库来处理SCP(而不是分叉一个shell进程)会更好。Net::SCP库(以及整个Net::*库)功能齐全,可与Capistrano一起用于处理远程命令


签出可用内容的详细信息。

将stderr重定向到stdout可能适合您:

output = `scp user@someserver:remoteFile /some/local/folder/ 2>&1`
puts output
这将捕获stderr和stdout。只有扔掉stdout才能捕获stderr:

output = `scp user@someserver:remoteFile /some/local/folder/ 2>&1 >/dev/null`
puts output

然后你可以使用IO.popen

Tokland回答了我提出的问题,但Adam的方法就是我最终使用的方法。这是我完成的脚本,显示下载的字节数,以及完成的百分比

require 'rubygems'
require 'net/scp'
puts "Fetching file"

# Establish the SSH session
ssh = Net::SSH.start("IP Address", "username on server", :password => "user's password on server", :port => 12345)

# Use that session to generate an SCP object
scp = ssh.scp

# Download the file and run the code block each time a new chuck of data is received
scp.download!("path/to/file/on/server/fileName", "/Users/me/Desktop/") do |ch, name, received, total|

  # Calculate percentage complete and format as a two-digit percentage
  percentage = format('%.2f', received.to_f / total.to_f * 100) + '%'

  # Print on top of (replace) the same line in the terminal
  # - Pad with spaces to make sure nothing remains from the previous output
  # - Add a carriage return without a line feed so the line doesn't move down
  print "Saving to #{name}: Received #{received} of #{total} bytes" + " (#{percentage})               \r"

  # Print the output immediately - don't wait until the buffer fills up
  STDOUT.flush
end

puts "Fetch complete!"

这里有两个正交问题,因为默认情况下scp只输出到终端,所以需要scp-v@tokland-这使它能够输出调试消息,但不能输出我仅运行scp时看到的传输进度。我认为这些信息不能发送到标准输出,而且我看不到scp将它们发送到标准输出的选项。显然,scp将进度信息发送到“交互式终端”?不知道如何捕获它…这对于获取标准输出消息是有效的,但scp似乎并不是在发送传输进度。所以我想我现在的问题是scp。这回答了我提出的问题-事实证明scp不做正常输出是一个意外的细节。请参阅下面我的答案,了解我是如何从Ruby标准库获得输出的。这看起来非常有希望!net scp文档在这里:此外,本教程还展示了如何在传输过程中输出进度:这就是我最后要做的。通过向scp对象传递一个块,我能够显示下载进度的运行计数器。请看我的完整脚本的答案。是的,这是最好的方法,因为使用好的语言只能依靠它们的库。