Control Flow
Control flow statements direct execution based on conditions and iteration.
Source: src/scripting/control_flow.f90, src/ast/evaluator_simple_real.f90
Overview
fortsh supports all standard shell control flow constructs:
- Conditionals -
if/elif/else/fiandcase/esac - Loops -
for,while,until - Functions - Named command groups with local scope
Quick Reference
Conditionals
if [[ $x -gt 0 ]]; then
echo "positive"
elif [[ $x -lt 0 ]]; then
echo "negative"
else
echo "zero"
fi
Case Statements
case $input in
y|yes) echo "Confirmed" ;;
n|no) echo "Cancelled" ;;
*) echo "Unknown" ;;
esac
Loops
# Word list
for file in *.txt; do
echo "$file"
done
# C-style arithmetic
for ((i=0; i<10; i++)); do
echo $i
done
# While
while read line; do
echo "$line"
done < file.txt
Functions
greet() {
local name="$1"
echo "Hello, $name"
}
greet "World"
Exit Status
Control flow keywords (if, while, done, etc.) reset the exit status to 0 after condition evaluation, per POSIX specification.
The exit status of:
if/elif/else/fi- status of last executed commandcase/esac- status of last executed command, or 0 if no pattern matchedfor/while/until- status of last executed command, or 0 if body never executed- Functions - status of last command or explicit
returnvalue
Operators
| Operator | Description |
|---|---|
&& | Execute next if previous succeeded |
|| | Execute next if previous failed |
! | Negate exit status |
; | Sequential execution |
& | Background execution |