-2

I would like to go to the location /var/log/src/ap-kernelmodule-10.001-100

But looks like my code has to deal with ap-kernelmodule-10.002-100, ap-kernelmodule-10.003-101 etc. I would like to reach the location with a split string and execute my command. eg : /var/log/src/ap-kernelmodule-10./

This ruby script for a Linux machine, so I used Mixlib:: ShellOut.

begin  
cmd = Mixlib::ShellOut.new("command run here" , :cwd => 
'/var/cache/acpchef/src/ap-kernelmodule-10xxx')
 cmd.run_command
end
knut
  • 27,320
  • 6
  • 84
  • 112
Anna
  • 1
  • 2
  • MAybe helpfull: https://stackoverflow.com/questions/10147913/ruby-run-shell-command-in-a-specific-directory/10148325#10148325 – knut Oct 15 '18 at 08:10
  • If you want the first 5 characters of a string, you can use the `String#[]` method with a range. `"012345"[0...5] => "01234" ` – user3574603 Oct 15 '18 at 10:07

2 Answers2

0

You can't be in multiple cwds simultaneously. To run the command for each directory that matches the pattern, you can use Dir#glob:

Dir.glob('/var/cache/acpchef/src/ap-kernelmodule-10*').each do |cwd|
  Mixlib::ShellOut.new("command run here", cwd: cwd).run_command
end
Kimmo Lehto
  • 5,910
  • 1
  • 23
  • 32
  • begin cwdkernel = Dir.glob('/var/cache/acpchef/src/ap-kernelmodule-10*') cmd = Mixlib::ShellOut.new("command run here", :cwd => 'cwdkernel') cmd.run_command end------------ how come this is not running the command. – Anna Oct 19 '18 at 04:14
  • There's no "do" – Kimmo Lehto Oct 19 '18 at 07:05
  • begin cwd_kernel = Dir.glob('/var/cache/acpchef/src/ap-kernelmodule-10*') cmd = Mixlib::ShellOut.new("command run here", :cwd => cwd_kernel ) cmd.run_command log 'run' rescue Exception => e end -----------so it works only with do?? else it will not – Anna Oct 19 '18 at 07:12
0

I am not quite get on what you really want to achieve. Question is rather ambiguous.

You know you would like to go to /var/log/src/ap-kernelmodule-10.001-100 and run a command, the most obvious way is you could just use:

execute "command run here" do
 cwd "/var/log/src/ap-kernelmodule-10.001-100"
end

But if you would like to run a command for each directory like you said in ap-kernelmodule-10.002-100, ap-kernelmodule-10.003-101 etc. with /var/log/src/ap-kernelmodule-10.*/ sequentially.

Then you could do with:

Dir.glob("/var/log/src/ap-kernelmodule-10.*").each do |folder|
  execute "command run here" do
   cwd folder
  end
end

Additionally, parallel resources execution is not possible natively in chef (AFAIK). So, the possible workaround is to use bash or ruby_block resource to construct commands to be executed in xargs, parallel or similar tools.

Hope this helps.

zdk
  • 1,528
  • 11
  • 17
  • actually, even if the folder is ap-kernelmodule-10.002-100 or ap-kernelmodule-10.003-101, I need the command to run. Because according to my script the version keeps changing. Sorry for not being very clear. For some machines its one folder and for other machine its the other. – Anna Oct 15 '18 at 11:37