-if … -elif … -else … -endif

Figure 1: Perhaps a few more -elif's might help

An-if … -elif … -else  … -endif series executes at most one block of commands when its corresponding condition proves to be true. If no condition proves to be true then commands within an-else … -endif block, if defined, execute. Lacking this default course, and without any condition prevailing, no action occurs.

The format of the command is:

-if <condition> … -elif <condition> … -else  … -endif

At least one block of zero or more commands resides between the -if <condition> … -endif delimiters; one or more -elif <condition> commands within this block sub-divides it into a series of blocks. In this case, -if <condition> marks the beginning of the first block, while one or more following -elif <condition> commands mark the second and subsequent blocks. The G'MIC interpreter tests the conditions corresponding to each block in order of appearance until a <condition> is found to be true, and the commands in the corresponding block executes, or all the blocks are exhausted. If present, a final -else … -endif block establishes a default course of action to take when none of the other conditions are true. In any case, if a block of commands executes, subsequent blocks if any, are ignored and the G'MIC interpreter resumes with the commands following the -endif block.

In this final -else … -endif block, it is illegal to insert -if <condition> … -elif <condition> without matching -endif commands. That is, within the -else … -endif block, one can only nest a new series of conditional actions, not extend the existing series.

The <condition> is an argument reducing to either a numeral or a string. If the numeral is non-zero or the string resolves to a existing file (boolean true), the commands, if any, of the following block execute. The G'MIC interpreter then resumes with the commands following -endif. A zero condition or a file that cannot be found (boolean false) bars execution of the following command block and the G'MIC interpreter goes on to test the condition of the next -elif command, or, in its absence, execute the final -else … -endif block, or, in the absence of that, resumes execution of commands following -endif.

Examples

CodeDiscussion
gmic \
-if 'myimage.png' \
   -echo[] 'Have the image.' \
-endif \
A basic conditional with one command block. This pipeline echoes a report only if a file called myimage.png can be found in the shell's current directory.
gmic \
-if 'turbofig-3.svg' \
   -if 'turbofig-4.svg' \
      -echo[] 'Got them both!' \
   -endif \
-endif
Two basic conditionals, one nested in the other. Echoes a report only if two particular files exist in the current directory.
gmic \
-if 'primary.jpg' \
   -input 'primary.jpg' \
-elif 'secondary.jpg' \
   -input 'secondary.jpg' \
-else \
-echo[]'Neither primary nor secondary found. \
-endif \
A conditional with two command blocks and a default action. The script tries to input primary.jpg first, otherwise secondary.jpg; otherwise it raises a warning.
gmic \
-if 'myimage.png' \
   -input 'myimage.png' \
   -if '{iM==im}' \
      -echo[]'Image lacks details.' \
   -else \
      -echo[]'Image has details.' \
   -endif \
-else \
   -echo 'Image does not exist.' \
-endif
Another example of a nested -if … -endif block. This pipeline first checks the existence of myimage.png and, should it exist, the nested conditional block reports if it has 'details' of some sort, assuming that a difference between the maximum and minimum intensities of images indicates details; otherwise the nested block reports a 'lack of details'. The outer conditional reports that myimage.png does not exist if the file is not in the shell's current directory.