if
)Conditional statement to execute different blocks of code depending on the result of the condition
Conditional control flow
if
can be utilized both as a method as well as a standalone function. As a method, the conditional state is derived from the calling function (eg if the previous function succeeds then the condition is true
).
if
:if { code-block } then {
# true
} else {
# false
}
if
command -> if {
# true
} else {
# false
}
if
:!if { code-block } then {
# false
}
if
:command -> !if {
# false
}
the then
and else
statements are optional. So the first usage could also be written as:
if { code-block } {
# true
} {
# false
}
However the practice of omitting those statements isn’t recommended beyond writing short one liners in the interactive command prompt.
Check if a file exists:
if { g somefile.txt } then {
out "File exists"
}
…or does not exist (both ways are valid):
!if { g somefile.txt } then {
out "File does not exist"
}
if { g somefile.txt } else {
out "File does not exist"
}
The conditional block can contain entire pipelines - even multiple lines of code let alone a single pipeline - as well as solitary commands as demonstrated in the examples above. However the conditional block does not output stdout nor stderr to the rest of the pipeline so you don’t have to worry about redirecting the output streams to null
.
If you require output from the conditional blocks stdout then you will need to use either a Murex named pipe to redirect the output, or test or debug flags (depending on your use case) if you only need to occasionally inspect the conditionals output.
When evaluating a command or code block, if
will treat an exit number less than 0 as true, and one greater than 0 as false. When the exit number is 0, if
will examine the stdout of the command or code block. If there is no output, or the output is one of the following strings, if
will evaluate the command or code block as false. Otherwise, it will be considered true.
0
disabled
fail
failed
false
no
null
off
if
!if
catch
): Handles the exception code raised by try
or trypipe
debug
): Debugging informationfalse
): Returns a false
valueand
): Returns true
or false
depending on whether multiple conditions are metor
): Returns true
or false
depending on whether one code-block out of multiple ones supplied is successful or unsuccessful.!
): Reads the stdin and exit number from previous process and not’s it’s conditiontrypipe
): Checks for non-zero exits of each function in a pipelinetest
): Murex’s test framework - define tests, run tests and debug shell scriptsswitch
): Blocks of cascading conditionalstrue
): Returns a true
valuetry
): Handles non-zero exits inside a block of codeThis document was generated from builtins/core/structs/if_doc.yaml.
This site's content is rebuilt automatically from murex's source code after each merge to the master
branch. Downloadable murex binaries are also built with the website.
Last built on Wed Sep 18 21:18:57 UTC 2024 against commit c037883c03788357164e9846c84d9f777251495d9452a8e.
Current version is 6.3.4225 (develop) which has been verified against tests cases.