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
3 个解决方案
#1
2
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:
并且,cat期望一个文件名,而不是一些文本附加到test_file,所以,这将按照我的意思运行:
system("echo \"#{a}\" >> test_file")
If you want to do this in pure Ruby let me know and I'll give you an example.
如果你想在纯Ruby中这样做,请告诉我,我会给你一个例子。
#2
3
Write a to a file called "testfile":
写一个名为“testfile”的文件:
File.open("testfile", "w") do |io| io.print a done
#3
0
Writing to a file directly has already been covered by JesperE. To write to a process (in this case a "cat" process) use popen.
JesperE已经涵盖了直接写入文件。要写入进程(在本例中为“cat”进程),请使用popen。
IO.popen("cat > foo", "w") do
|f|
f.write("line1\nline2\n")
end
#1
2
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:
并且,cat期望一个文件名,而不是一些文本附加到test_file,所以,这将按照我的意思运行:
system("echo \"#{a}\" >> test_file")
If you want to do this in pure Ruby let me know and I'll give you an example.
如果你想在纯Ruby中这样做,请告诉我,我会给你一个例子。
#2
3
Write a to a file called "testfile":
写一个名为“testfile”的文件:
File.open("testfile", "w") do |io| io.print a done
#3
0
Writing to a file directly has already been covered by JesperE. To write to a process (in this case a "cat" process) use popen.
JesperE已经涵盖了直接写入文件。要写入进程(在本例中为“cat”进程),请使用popen。
IO.popen("cat > foo", "w") do
|f|
f.write("line1\nline2\n")
end