Reputation: 354
mysqldump = "mysqldump"
`#{mysqldump} > backup_file.sql`
I'm supposed to append several of those mysqldump Strings (I simplified it for this example; normally line 2 would have the username and password as options) into the SQL file.
The problem is line 2, when I try to call the Bash operator '>' to append the String. Instead of appending, the script ends up calling the mysqldump command itself.
What can I do to store the String "mysqldump" into the file backup_file.sql? I want to do it the same way as line 2: automatically appending through the Bash.
Upvotes: 0
Views: 93
Reputation: 7679
if you are trying to append "like" you said and not overwrite the target file use >>
instead of >
. Here is a working version of your script:
za$ emacs ./foo.rb
#!/usr/bin/env ruby
target_dir = "/Users/za/ruby-practice/backup_file.sql"
mysqldump = "mysqldump"
`echo #{mysqldump} >> "#{target_dir}"`
You can also do something like : system %Q{echo "#{mysqldump}" >> "#{target_dir}"}
. Personally , I would say use IO#puts instead of making system calls inside your script , if you want a pure ruby solution/system independent solution.
Upvotes: 2
Reputation: 299
Why don't you use pure ruby to do it? Like:
File.open("backup_file.sql", "w") do |f|
dump_lines.each do |line|
f.puts line
end
end
assuming that you have the dump in an array..
Upvotes: 0