I'm currently trying to make a dynamic test with [[]] and evaluated code, but there is a -n that is added automaticaly at the beginning of the conditions. So my conditions are always true...
Here an example with set -x :
myCondition='${queueName} == ${pattern}'
pattern="COMPLETELY_DIFFERENT_PATTERN"
queueName="QM.GCS.SRC.TOTO"
set -x ; [[ $(eval echo $myCondition ) ]] && echo CORRECT; set +x
++ eval echo '${queueName} == ${pattern}'
+++ echo QM.GCS.SRC.TOTO == COMPLETELY_DIFFERENT_PATTERN
+ [[ -n QM.GCS.SRC.TOTO == COMPLETELY_DIFFERENT_PATTERN ]]
+ echo CORRECT
CORRECT
+ set +x
Why the -n is added ? Because there is only one parameter ?
How i can make it work as i want ?
[[
implicitly disables string-splitting, such that an expansion isn't split into multiple arguments.
A single-item test implicitly runs -n
, testing whether that item is the empty string.
Thus, if you want pieces of your condition to be evaluated as separate arguments to [[
, you should have the [[
within the eval
, such that the condition is replaced with the text it contains before [[
's parsing rules are in effect.
eval "[[ $myCondition ]]"
Thus, this would make your code:
myCondition='${queueName} == ${pattern}'
pattern="COMPLETELY_DIFFERENT_PATTERN"
queueName="QM.GCS.SRC.TOTO"
set -x ; eval "[[ $myCondition ]]" && echo CORRECT; set +x
...which emits on stderr the expected log:
+ eval '[[ ${queueName} == ${pattern} ]]'
++ [[ QM.GCS.SRC.TOTO == COMPLETELY_DIFFERENT_PATTERN ]]
+ set +x