分类:
2009-11-05 17:45:37
#!/bin/bash
#==========================================================
#lcm.sh
#This script can calculate two integers gcd and lcm.
#==========================================================
#Version 1.0\2009-11-06\by zhengyu
#Any questions can be emailed to
#==========================================================
ARGS=2
ERORR_NUM=100
function lcm()
{
#==========================================================
#function gcd() calculates the gcd first.
#==========================================================
function gcd()
{
devidend=$1
devisor=$2
reminder=1
until [[ "$reminder" -eq 0 ]]
do
reminder=`expr "$devidend" % "$devisor"`
devidend=$devisor
devisor=$reminder
done
}
gcd $1 $2
arg1=`expr "$1" / "$devidend"`
arg2=`expr "$2" \* "$arg1"`
}
if [[ "$#" -ne "$ARGS" ]]
then
echo -e "Usage:`basename $0` 1st_int 2nd_int"
exit $ERORR_NUM
else
lcm $1 $2
echo -e "The gcd of $1 and $2 is $devidend."
echo -e "The lcm of $1 and $2 is $arg2."
fi
exit 0
这个脚本的作用就是求出两个整数的最小公倍数和最大公约数。在这个脚本当中也学到一些东西,比如until循环结构,四则运算,乘法“*”需要转义“\*”,另外就是学到了求最大公约数这个算法。