Question

Please help, I am stuck here ---

irb> a = "line of text\n  line two\n  line three"
irb> system("cat > test_file << #{a}")
cat: of: No such file or directory
cat: text: No such file or directory
=> false
Was it helpful?

Solution

You need to quote the interpolated parameter:

system("cat > test_file << \"#{a}\"")

And, cat is expecting a filename, not some text to append to test_file, so, this would work as I think you intended:

system("echo \"#{a}\" >> test_file")

If you want to do this in pure Ruby let me know and I'll give you an example.

OTHER TIPS

Write a to a file called "testfile":

File.open("testfile", "w") do |io| io.print a done

Writing to a file directly has already been covered by JesperE. To write to a process (in this case a "cat" process) use popen.

IO.popen("cat > foo", "w") do
    |f|
    f.write("line1\nline2\n")
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top