#!/bin/dash # written by andrewt@unsw.edu.au for COMP(2041|9044) # print print numbers < 1000 # note use of local Shell builtin to scope a variable # without the local declaration # the variable i in the function would be global # and would break the bottom while loop # local is not (yet) POSIX but is widely supported is_prime() { local n i n=$1 i=2 while test $i -lt $n do test $((n % i)) -eq 0 && return 1 i=$((i + 1)) done return 0 } i=0 while test $i -lt 1000 do is_prime $i && echo $i i=$((i + 1)) done