#!/usr/bin/env bash # Greatest Common Divisor in POSIX shell using Euclid's algorithm. On # return, variable gcd_value is set and is the gcd of parameters $1 # and $2. The GCD of a negative number is the same as the GCD of its # absolute value, since a negative number is -1 times its positive # value. Negative numbers are set when there is an error; -1 is set # when the wrong number of parameters are given. proc gcd { typeset -i a=$1 typeset -i b=$2 if sh-expr ' a > b )' { setglobal a = $b setglobal b = $1 } if sh-expr ' a == 1 || (b-a) == 0)' { setglobal gcd_value = $a return 0 } typeset -i c sh-expr 'c=b-a' gcd $c $a } gcd $1 $2 echo $gcd_value