bash - how to use shell script in not_if condition -
i want use shell script output validate resource in guard condition.i using below code still not working.
bash "stampdisksuccess" code <<-eoh whoami eoh user 'root' not_if {"`sudo sh /home/scripts/getstampeddisk.sh test`" == ''} end
whether script(getstampeddisk.sh) returns blank message or data, bash resource getting executed. expecting should not run when script returns blank data.
correct me if missing.
let's dissect it:
not_if {"`sudo sh /home/scripts/getstampeddisk.sh test`" == ''}
- not_if ... quite obvious, it's guard
{
block opening"
string opening- backticks opening
- command
- closing backticks , quote
==
operator''
constant empty string}
closing block
that's lot of operations test have empty string. output backticks not guaranteed empty (it can have newline example) , compare arbitrary empty string (not nil, it's string nothing). there's many way can turn bad , it's quite hard debug non printing characters.
however shells have operator this, it's -z
usual bash.
quoting bash documentation:
-z string true if length of string zero.
another helper $()
evaluate command inside script
last 1 [[ ]]
construction tell we're using operator , not command.
all end command execution guard when expressed string (documentation on guards here)
not_if '[[ -z $(sudo sh /home/scripts/getstampeddisk.sh test) ]]'
quote guard documentation:
a guard attribute accepts either string value or ruby block value:
- a string executed shell command. if command returns 0, guard applied. if command returns other value, guard attribute not applied.
- a block executed ruby code must return either true or false. if block returns true, guard attribute applied. if block returns false, guard attribute not applied.
if don't use ruby in guard, don't use block, you're adding layers witch in trouble, when try compare non printable or empty strings , it's harder debug, try stick standard shell commands if need call command or script, can test in console , sure not problem later.
Comments
Post a Comment