maafk
maafk

Reputation: 6876

output of unix command in chef recipe

I have an execute command in a chef recipe, and I'd like to set the cwd property as the output of a unix command.

execute 'run dynamically generated install file' do
    command 'make install'
    cwd '' # would like the output of `ls -Adrt /tmp/unixODBC.* | tail -n 1`
end

Is this possible?

Upvotes: 2

Views: 327

Answers (3)

coderanger
coderanger

Reputation: 54181

Okay, so finally at a keyboard and can write this out in full.

The literal translation of what you have there would be:

execute 'run dynamically generated install file' do
    command 'make install'
    cwd lazy { shell_out!('ls -Adrt /tmp/unixODBC.* | tail -n 1').stdout.strip }
end

However that is going to be much slower than needed and more failure prone so I would recommend writing it in Ruby instead:

execute 'run dynamically generated install file' do
    command 'make install'
    cwd lazy { Dir['/tmp/unixODBC.*'].first }
end

This avoids having to spawn a bunch of processes and instead just does the same (I think) logic directly.

Upvotes: 4

Jakir00
Jakir00

Reputation: 2073

That seems like it's out of the scope of an execute block.

Maybe just use a ruby_block?

ruby_block 'run dynamically generated install file' do
    require 'mixlib/shellout'
    block do
        cmd = Mixlib::ShellOut.new('make install')
        cmd.run_command
        cwd = cmd.stdout
        # Do more stuff with cwd...
    end
end

Upvotes: 0

eiko
eiko

Reputation: 5335

You should be able to do it just like this:

execute 'run dynamically generated install file' do
    command 'make install'
    cwd `ls -Adrt /tmp/unixODBC.* | tail -n 1`
end

Upvotes: 0

Related Questions