github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/structs/if_doc.yaml (about) 1 - DocumentID: if 2 Title: >+ 3 `if` 4 CategoryID: commands 5 Summary: >- 6 Conditional statement to execute different blocks of code depending on the 7 result of the condition 8 Description: |- 9 Conditional control flow 10 11 `if` can be utilized both as a method as well as a standalone function. As a 12 method, the conditional state is derived from the calling function (eg if the 13 previous function succeeds then the condition is `true`). 14 Usage: |- 15 ### Function `if`: 16 17 ``` 18 if { code-block } then { 19 # true 20 } else { 21 # false 22 } 23 ``` 24 25 ### Method `if`: 26 27 ``` 28 command -> if { 29 # true 30 } else { 31 # false 32 } 33 ``` 34 35 ### Negative Function `if`: 36 37 ``` 38 !if { code-block } then { 39 # false 40 } 41 ``` 42 43 ### Negative Method `if`: 44 45 ``` 46 command -> !if { 47 # false 48 } 49 ``` 50 51 ### Please Note: 52 the `then` and `else` statements are optional. So the first usage could 53 also be written as: 54 55 ``` 56 if { code-block } { 57 # true 58 } { 59 # false 60 } 61 ``` 62 63 However the practice of omitting those statements isn't recommended beyond 64 writing short one liners in the interactive command prompt. 65 Examples: |- 66 Check if a file exists: 67 68 ``` 69 if { g somefile.txt } then { 70 out "File exists" 71 } 72 ``` 73 74 ...or does not exist (both ways are valid): 75 76 ``` 77 !if { g somefile.txt } then { 78 out "File does not exist" 79 } 80 ``` 81 82 ``` 83 if { g somefile.txt } else { 84 out "File does not exist" 85 } 86 ``` 87 Flags: 88 Detail: |- 89 ### Pipelines and Output 90 91 The conditional block can contain entire pipelines - even multiple lines of code 92 let alone a single pipeline - as well as solitary commands as demonstrated in 93 the examples above. However the conditional block does not output STDOUT nor 94 STDERR to the rest of the pipeline so you don't have to worry about redirecting 95 the output streams to `null`. 96 97 If you require output from the conditional blocks STDOUT then you will need to 98 use either a Murex named pipe to redirect the output, or test or debug flags 99 (depending on your use case) if you only need to occasionally inspect the 100 conditionals output. 101 102 ### Exit Numbers 103 104 When evaluating a command or code block, `if` will treat an exit number less 105 than 0 as true, and one greater than 0 as false. When the exit number is 0, `if` 106 will examine the stdout of the command or code block. If there is no output, or 107 the output is one of the following strings, `if` will evaluate the command or 108 code block as false. Otherwise, it will be considered true. 109 110 * `0` 111 * `disabled` 112 * `fail` 113 * `failed` 114 * `false` 115 * `no` 116 * `null` 117 * `off` 118 119 Synonyms: 120 - if 121 - "!if" 122 Related: 123 - trypipe 124 - try 125 - catch 126 - "true" 127 - "false" 128 - not-func 129 - and 130 - or 131 - test 132 - debug 133 - switch