FOR

Execute a series of commands in a loop

FOR

Run command lines once for each value of a counter.

for <var>=<start>,<stop>[,<step>[,<fmt>]] '<command>' …

<step> defaults to 1 and may be negative; a step of 0 runs nothing. <fmt> is a printf-style format for the value (default %d); keep it simple, because it is used as given. Inside an rcload script the loop variable is removed when the loop ends. At the drivshell> prompt it is not: it stays set as a global variable holding its last value (after the second example below, $i is 0x30). Every further argument is one complete command line. Put it in single quotes so that $var is expanded on each pass, not once before the loop starts.

The loop stops at the first failing command while the LoopError option is on (the default, see set). Nesting of for, loop and rcload is limited (rcload: recursion too deep in for loop). A malformed first argument prints Invalid loop format. A running loop cannot be interrupted at the drivshell> prompt. Whether it is safe depends on the commands.

Hardware-validated

drivshell> for port=0,23 'echo port=$port'
port=0
port=1
port=2
port=3
port=4
port=5
port=6
port=7
port=8
port=9
port=10
port=11
port=12
port=13
port=14
port=15
port=16
port=17
port=18
port=19
port=20
port=21
port=22
port=23

Hardware-validated

drivshell> for i=0,0x30,0x10,0x%02x 'echo $i'
0x00
0x10
0x20
0x30

Built-in help

Parameters: <var>=<start>,<stop>[,<step>[,<fmt>]] 'command' ...
    Iterate a series of commands, each time setting <var> to the
    loop value.  Each argument is a complete command, so if it contains
    more than one word, it must be placed in quotes.  For example:

        for port=0,23 'echo port=$port'

    Note the use of single quotes to avoid expanding the $port variable
    before executing the loop.  <fmt> defaults to %d (decimal), but can
    be any other standard printf(1)-style format string.

    If the series of commands fails, the loop will stop only if the
    "LoopError" option is true (see the "set" command).

Further reading

SDK-6.5.20 HSDK Getting Started Guide (GS-SDK6520-R), plus SDK-6.5.20/21/22 Release Notes (RN-SDK65xx-R), BCM_SDK命令.