# Chapter 31Defined Macros ## 31.6 Macro Conditionals: \$?, \$|, and \$.

Occasionally, it is necessary to test a macro to see whether a value has been assigned to it. To perform such a test, a special prefix and two operators are used. The general form is

```  if       else       endif \$?x text1 \$| text2 \$. if x is defined   if x is not defined```

This expression yields one of two possible values: `text1` if the macro named `x` has a value, `text2` if it doesn't. The entire above expression, starting with the `\$?` and ending with the `\$.`, yields a single value, which may contain multiple tokens.

The following, for example, includes the configuration-file version in the SMTP greeting message but does so only if that version (in `\$v`; see Section 31.10.38) is defined:

```O SmtpGreetingMessage=\$j Sendmail (\$v/`\$?Z\$Z\$|generic\$.`) ready at \$b note```

Here the parenthetical version information is expressed one way if `Z` has a value (like `1.4`):

`(\$v/\$Z)`

but is expressed differently if `Z` lacks a value:

`(\$v/generic)`

The else part (`\$|`) of this conditional expression is optional. If it is omitted, the result is the same as if the `text2` were omitted:

```\$?xtext1\$|\$.
\$?xtext1\$.```

Both of the preceding yield the same result. If `x` has a value, then `text1` becomes the value of the entire expression. If `x` lacks a value, then the entire expression lacks a value (produces no tokens).

Note that it is not advisable to use the `\$?` conditional expression in rules. It may not have the intended effect, because macro conditionals are expanded when the configuration file is read.

### 31.6.1 Conditionals May Nest

V8 sendmail allows conditionals to nest. To illustrate, consider the following expression:

`\$?x \$?y both \$| xonly \$. \$| \$?y yonly \$| none \$. \$.`

This is just like the example in the previous section:

`\$?x text1 \$| text2 \$.`

except that `text1` and `text2` are both conditionals:

```text1 = \$?y both \$| xonly \$.
text2 = \$?y yonly \$| none \$.```

The grouping when conditionals nest is from the outside in. In the following example, parentheses have been inserted to show the groupings (they are not a part of either expression):

````(`\$?x `(`text1`)` \$| `(`text2`)` \$.`)`
`(`\$?x `(`\$?y both \$| xonly \$.`)` \$| `(`\$?y yonly \$| none \$.`)` \$.`)````

Interpretation is from left to right. The logic of the second line above is therefore this: If both `\$x` and `\$y` have values, the result is `both`. If `\$x` has a value but `\$y` lacks one, the result is `xonly`. If `\$x` lacks a value but `\$y` has one, the result is `yonly`. And if both lack values, the result is `none`.

The sendmail program does not enforce or check for balance in nested conditionals. Each `\$?` should have a corresponding `\$.` to balance it. If they do not balance, sendmail will not detect the problem. Instead, it may interpret the expression in a way that you did not intend.

The depth to which conditionals may be nested is limited only by our ability to easily comprehend the result. More than two deep is not recommended, and more than three deep is vigorously discouraged. 31.5 Macro Expansion: \$ and \$& 31.7 Categories of Macros
{"ft_lang_label":"__label__en","ft_lang_prob":0.8545416,"math_prob":0.8997439,"size":2835,"snap":"2023-40-2023-50","text_gpt3_token_len":756,"char_repetition_ratio":0.13034263,"word_repetition_ratio":0.01183432,"special_character_ratio":0.25996473,"punctuation_ratio":0.15743944,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9599037,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26],"im_url_duplicate_count":[null,null,null,5,null,5,null,3,null,3,null,3,null,3,null,3,null,3,null,5,null,2,null,5,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-11-29T03:21:09Z\",\"WARC-Record-ID\":\"<urn:uuid:1751da98-b796-4e63-b323-5c00c9522aeb>\",\"Content-Length\":\"11256\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bc2eb871-06ca-49fe-9fc6-45a86a287a3c>\",\"WARC-Concurrent-To\":\"<urn:uuid:44d60976-cbde-4149-a7d4-496493f33924>\",\"WARC-IP-Address\":\"199.201.145.78\",\"WARC-Target-URI\":\"http://docs.smoe.org/oreilly/netwrkcd/sendmail/ch31_06.htm\",\"WARC-Payload-Digest\":\"sha1:KD7FFC77NP5OEQ46FNPE676KCOU4M7RO\",\"WARC-Block-Digest\":\"sha1:XPWUW6ZXHYLNZ7TTSY4BOSBZBSUFXHCO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100047.66_warc_CC-MAIN-20231129010302-20231129040302-00224.warc.gz\"}"}