Skip to main content

๐Ÿ”„ Special Loops

Special loops to simplify common iterative processes.

warning

Special loops cannot be nested. Starting a new special loop inside another will cause unexpected results.


loopโ€‹

Performs iterations based on numerical sequences.

ArgumentDescription
N1Start integer (if alone, iterates 1 to N1)
N2End integer (optional, iterates N1 to N2)
note

The $INDEX variable reflects the current iteration number.

# From 1 to 100
loop 100; do
ui_print "Iteration: $INDEX"
done

# From 5 to 10
loop 5 10; do
ui_print "At: $INDEX"
done

loop_fileโ€‹

Iterates over each line in a file.

ArgumentDescription
FILEFile to read
note
  • $INDEX: Current line number.
  • $LINE: Content of the current line.
loop_file "/sdcard/file.txt"; do
ui_print "LINE NUMBER: $INDEX"
ui_print "LINE CONTENT: $LINE"
done

loop_stringโ€‹

Breaks text into segments based on delimiters and iterates over them.

ArgumentDescription
DELIMITERSCharacters to split on (empty "" splits by char)
TEXTText to split
note
  • $INDEX: Current segment number.
  • $SECTION: Content of the current segment.
# Single delimiter
loop_string ";" "First; Second; Third"; do
ui_print "SEGMENT NUMBER: $INDEX"
ui_print "SEGMENT CONTENT: $SECTION"
done

# Multiple delimiters
loop_string ";$:" "First; Second$ Third: Four"; do
ui_print "SEGMENT NUMBER: $INDEX"
ui_print "SEGMENT CONTENT: $SECTION"
done

# Character by character
loop_string "" "Hello"; do
ui_print "CHAR NUMBER: $INDEX"
ui_print "CHAR: $SECTION"
done

loop_arrayโ€‹

Iterates over elements in an array.

ArgumentDescription
ARRAY NAMEName of the array
note
  • $KEY: Access key of the current element.
  • $VALUE: Value of the current element.
declare -A SOMETHING=(
["key1"]="value1"
["key2"]="value2"
["key3"]="value3"
)
loop_array SOMETHING; do
ui_print "KEY: $KEY"
ui_print "VALUE: $VALUE"
done