Catherine Hampton wrote,
| I want to write a recipe which checks to see if a variable is set, and if
| it isn't, sets it to a default value. Will this recipe work?
|
| :0:
| * ! VARIABLE ?? .*
| VARIABLE=defaultvalue
|
Rik Kabel and W. Wesley Groleau have pretty much answered this; I just want
to gather what they've said into one place.
First, it is critical whether Catherine means "set" literally (and a null
value is acceptable if the variable is set but null) or she really means
"non-null" (if the variable is set but null, that's unacceptable and the
default value should be used).
As Wesley said, it can be done without a recipe:
VARIABLE=${VARIABLE-defaultvalue} # if set but null is acceptable
VARIABLE=${VARIABLE:-defaultvalue} # if set but null is unacceptable
If you really want to put it into a recipe,
# if set but null is acceptable
:0
* $ ${VARIABLE+!}
{ VARIABLE=defaultvalue }
# if set but null is unacceptable
:0
* $ ${VARIABLE:+!}
{ VARIABLE=defaultvalue }
# another way if set but null is unacceptable
:0
* VARIABLE ?? ^^^^
{ VARIABLE=defaultvalue }