Ask Your Question

Why the exec is not run with unless

asked 2014-01-27 08:46:37 -0500

jaksky gravatar image

Hi I have following piece of code: Exec with "condition"

   exec { "extract java ${fullversion}": 
  command => "rpm -i ${path}/${jdkfile}", 
  unless =>"rpm -qa|grep jdk-1.7.0_45|wc -l"

rpm -qa|grep jdk-1.7.0_45|wc -l - returns 0 if absent and 1 if present. I tried unless and onlyif but I cannot make it work correctly. Can someone give a hand, pls?

edit retag flag offensive close merge delete


I know this is an old post, but just to clarify -- the returned string value of `wc -l` is 1 or 0, but that's not what `unless` is checking. `unless` checks the *error code* returned from the last command run, and in this case, `wc` always returns '0' -- which is true

poolpog gravatar imagepoolpog ( 2016-06-08 12:32:15 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-01-27 09:42:59 -0500

frederik gravatar image

updated 2014-01-27 10:36:20 -0500

spuder gravatar image

You must check the return value, you can do it like this:

   exec { "extract java ${fullversion}": 
  command => "rpm -i ${path}/${jdkfile}", 
  unless =>"rpm -qi grep jdk-1.7.0_45 >/dev/null 2>&1"

The command will be executed if the return value of the "unless" part is not zero.

But i think what you are trying to do is more correctly handled using a yum repo and a package resource, seems more right.

Hope it helps.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-01-27 08:46:37 -0500

Seen: 5,088 times

Last updated: Jan 27 '14