Search code examples
rubyposix

Ruby output as input for system command


I am trying download a ton of files via gsutil (Google Cloud). You can pass a list of URLs to download:

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

How can I do this from Ruby, from within the program I mean? I tried to output them but that doesn't seem to work.

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

Any idea?

A potential workaround would be to save the URLs in a file and use cat file | gsutil -m cp -I gs://my-bucket but that feels like overkill.


Solution

  • Can you try echo '#{urls.join("\n")}' If you put puts it returns nil, rather than the string you want to return. The interpolation fails due to the same reason.