JCShell - Control flow statements

Within JCShell script files, you can use the following expressions to control the execution of script flow:

if / elseif / else statement

The if statement represents a conditional expression statement. It is used to conditionally execute sections within a script.

Expression Example
if expression
...
elseif expression
...
else
...
end
if ${str} == "abc"
   /echo "String is abc"
elseif ${str} == "cde"
   /echo "String is cde"
else
   /echo "String is neither abc nor cde"
   /echo "Sorry..."
end

while statement

The while statement is used to construct loops. The label is optional and can be used as a reference in break and continue statements. The expression is evaluated to a boolean value.

Expression Example
 [label:] while expression
 ...
 end
while ${loop} < 2
  /echo ${loop}
end

for statement

The for statement is used as looping construct. The label is optional and can be used as a reference in break and continue statements. The expression is evaluated to a boolean value.

Expression Example
[label:] for varname words
...
end
arr[*]=v1 v2 v3 v4 v5
for D ${arr[*]}
   /echo ${D}
end

for loop 1 2 14 15
   /echo ${loop} 
end

myloop: for loop 1 2 14 15
   if ${loop} == 2 
      continue
   end
   /echo ${loop} 
end

break / continue statements

The break and continuestatements are used to transfer the control flow within loops. break is used to stop processing the loop and continue to continue with the next loop value. The label is optional and, if present, must refer to label in front of a while or for statement. If the label is omitted, the statement refers to the innermost while or for loop.

Expression Example
break [label]

continue [label]
myloop: for loop 1 2 14 15
   if ${loop} == 2 
      continue
   end
   /echo ${loop} 
end

goto statements

Use the goto statement to continue execution at a specified label. The destination should be in the same block and the same block nesting level. You should not leave or enter an if, while, for, or try block.

Expression Example
goto label
loop=2
goto mylabel
loop=3
mylabel: /echo ${loop} 

try / catch / throw statements

If a command between the try and the first catch terminates abnormally, execution is resumed at the first catch statement whose expression evaluates to true. You can signal your own exceptions with the throw statement.

Expression Example
try
...
catch expression
...
catch expression
...
end
try
   upload ${script.dir}/build/mycap.cap
catch "${last.error}" =~ "*problems*"
   # Terminal closed 
   /echo "Houston, we have a problem"

catch 1
   throw "Err: ${last.errorlog}"
end

Related information