1

I am using linux commands to generate a file "a.txt" . Now I need to read the first word of file "a.txt" and update an existing file called "b.txt". I will search a word called "/fill " in b.txt and replace it with the word read from a.txt Below is the code

bash 'example' do

 code <<-EOH
 cat ex.txt >> a.txt 
  EOH
end

test = /#{'cat /a.txt'}/
file_names = ['/b.txt']

file_names.each do |file_name|
  text = File.read(file_name)
  new_contents = text.gsub(/fill, test)
  puts new_contents
  File.open(file_name, "w") {|file| file.puts new_contents }
  1. With the help of linux command "cat ex.txt >> a.txt " I am putting the content of ex.txt to a.txt.
  2. After this I want to read the file a.txt with test = /#{'cat /a.txt'}/. Example a.txt contains "azure" word
  3. Now in b.txt I want to search for a word "/fill" and replace with content read in step 2 from b.txt file ie azure
  4. The problem is instead of replacing /fill with azure, /fill is getting replaced with cat /a.txt. Hope its clear now

Can you please help here

Tensibai
  • 15,557
  • 1
  • 37
  • 57
Dave
  • 379
  • 1
  • 5
  • 14
  • No we can't help as it sounds absolutely counter productive, why doesn't a `template` and `file` resource works in your case ? relying to bash code should be the last resort when usual idempotent primitive can't fit. This sounds like a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), describe what you're really after and we may help with approaches. – Tensibai Feb 15 '17 at 12:52
  • @Tensibai its because ex.txt is generated dynamically and I need to read the contents. The goal is to read the content of dynamically generated file called ex.txt and replace the content of the b.txt file based on certain word search. I hope I made my point – Dave Feb 15 '17 at 13:31
  • Not you didn't, you don't say why a template doesn't fit for b.txt, nor why you have to rely on this ex.txt, nor defined what you're doing as a whole. – Tensibai Feb 15 '17 at 13:34
  • Ok I quit, if you can't describe your overall problem and say why you script can't be maanged by chef, and why the ohai information doesn't fit your need, I can't help. – Tensibai Feb 15 '17 at 13:43
  • You're just repeating what you said above. As said, I quit there, nevermind. – Tensibai Feb 15 '17 at 13:49

1 Answers1

1

It is a bit hard to follow, what you actually want to achieve. Your code has a few issues. General advice:

  • put your ruby code inside a ruby_block resource so that is executed during Chef's convergence phase
  • Use Chef::Util::FileEdit, if you want to edit files that are not entirely managed by Chef (see this question for more inspiration)).
  • In case you really want to write the complete file using Chef, use a file resource and specify the content based on what you've read using File.read.
  • As said, ruby code outside of ruby_block is executed in the compile phase (which precedes the convergence phase). If you this is too early (because the source file isn't there yet, you can use a lazy block for lazy evaluation:

    file "b.txt" do
      content lazy { File.read .. }
    end
    
Community
  • 1
  • 1
StephenKing
  • 36,187
  • 11
  • 83
  • 112
  • thanks for your response. I have reworded the requirements. please help here – Dave Feb 15 '17 at 12:21
  • Very hard. As @Tenisbai said it would make sense to describe what you actually want to do without having a solution in mind which does not feel "chef native" (at least to me). – StephenKing Feb 15 '17 at 19:45