While Loops

KSH while loop constructs

Basic while loop

KSH while loop with integer arithmetic

#!/bin/ksh
integer counter=1
while (( counter <= 5 )); do
    print "Count: $counter"
    (( counter++ ))
done
KSH while with integer arithmetic

KSH while loop with integer arithmetic and increment

#!/bin/ksh
integer counter=1
while (( counter <= 10 )); do
    print "Count: $counter"
    (( counter++ ))
done
KSH while with array processing

KSH while loop processing array elements

#!/bin/ksh
set -A numbers 1 2 3 4 5
integer i=0
while (( i < ${#numbers[@]} )); do
    print "Number: ${numbers[$i]}"
    (( i++ ))
done
KSH while with file reading

KSH while loop reading file line by line

#!/bin/ksh
while IFS= read -r line; do
    print "Processing: $line"
done < /etc/passwd
KSH while with user input

KSH while loop with interactive user input

#!/bin/ksh
while true; do
    print "Enter a number (0 to quit): "
    read num
    if (( num == 0 )); then
        break
    fi
    print "You entered: $num"
done
KSH while with timeout

KSH while loop with timeout mechanism

#!/bin/ksh
integer timeout=30
integer start_time=$(date +%s)
while (( $(date +%s) - start_time < timeout )); do
    if some_condition; then
        print "Condition met!"
        break
    fi
    sleep 1
done
KSH while with command-based condition

KSH while loop using command exit status

#!/bin/ksh
while command; do
    print "Command is still running"
    sleep 1
done
KSH while with test command

KSH while loop using test command

#!/bin/ksh
counter=1
while test $counter -le 5; do
    print "Count: $counter"
    counter=$((counter + 1))
done