Expand

if

  1. conditionally execute statements

    <if>   = if <expr> { <statements> } <else>
    <else> = ΓΈ
           | else <if>
           | else { <statements> }

    if evaluates the condition and runs the first statements block, if result is true; otherwise it runs the else part. Condition expression must evaluate to a boolean or null, otherwise if panics.

    Syntactically, if is a command and, therefore, can be used in pipelines, redirects and any other places where commands can be used.

    check age
    if $age >= 18 {
        print "adult"
    } else {
        print "not yet adult"
    }
    check if system has been recently started
    # `uptime -r` gives a space-separated list,
    # the second value is uptime in seconds
    if (uptime -r):spaces[1]:seconds < 2:minutes {
        print "just started"
    }
    redirect the whole if to a log file
    if $metric > $threshold {
        print "system state is critical"
        print "metric = $metric"
    } else {
        print "system is okay"
    } >> debug.log
  2. in for, pass only items that match a condition:

    >> put 1 2 3 4 | for $i if $i <= 2
    1
    2

    Check for documentation for the complete syntax.