Linux command in guard


#1

What is the proper syntax of using a shell command and compare its output in guard.

I have a file which contains 1 or 0

How can I write only_if condition using cat and compare the output to 1 or 0

I tried several different ways but every time get skipped due to only_if (for either 0 or 1)

syntax I tried,so far

cat xxx” == “1”
“cat xxx” ==“1”
cat xxx == “1”
all within {} or without, but no help

Appreciate your help

M


#2

When using strings for a guard, the command gets run and the exit status of that command (or pipeline) is compared. Exit 0 triggers the only_if guard. Exit non-0 triggers the not_if guard. Output is irrelevant when using strings for guards. See https://docs.chef.io/resource_common.html#guards.

Arbitrary logic can be applied when using a block as the guard argument, the {} you noted. So if you really wanted to trigger based on the content of a file, your best bet is only_if { IO.read('/my/file').chomp == '1' }. But if you’re just storing a boolean value in a file and performing an action based on that, odds are there are better ways of accomplishing whatever you’re doing.