if
conditionally execute statements
<if> = if <expr> { <statements> } <else> <else> = ΓΈ | else <if> | else { <statements> }ifevaluates the condition and runs the first statements block, if result is true; otherwise it runs theelsepart. Condition expression must evaluate to a boolean or null, otherwiseifpanics.Syntactically,
ifis 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 ifto a log fileif $metric > $threshold { print "system state is critical" print "metric = $metric" } else { print "system is okay" } >> debug.login
for, pass only items that match a condition:>> put 1 2 3 4 | for $i if $i <= 2 1 2Check
fordocumentation for the complete syntax.