Ruby 输出作为系统命令的输入

Ruby output as input for system command

我正在尝试通过 gsutil(Google 云)下载大量文件。您可以传递要下载的 URL 列表:

You can pass a list of URLs (one per line) to copy on stdin instead of as command line arguments by using the -I option. This allows you to use gsutil in a pipeline to upload or download files / objects as generated by a program, such as:

some_program | gsutil -m cp -I gs://my-bucket

如何从 Ruby、 程序中执行此操作?我尝试输出它们,但似乎不起作用。

urls = ["url1", "url2", "url3"]
`echo #{puts urls} | gsutil -m cp -I gs://my-bucket`

有什么想法吗?

一个可能的解决方法是将 URL 保存在一个文件中并使用 cat file | gsutil -m cp -I gs://my-bucket 但这感觉有点矫枉过正。

你能试试吗echo '#{urls.join("\n")}' 如果你把puts它returns nil,而不是你想要的字符串return。同样的原因插值失败。