LDP/LDP/guide/docbook/abs-guide/max.sh

49 lines
958 B
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
2001-09-04 13:27:31 +00:00
# max.sh: Maximum of two integers.
2001-07-10 14:25:50 +00:00
2006-12-20 21:11:55 +00:00
E_PARAM_ERR=250 # If less than 2 params passed to function.
EQUAL=251 # Return value if both params equal.
2006-03-06 14:21:00 +00:00
# Error values out of range of any
#+ params that might be fed to the function.
2001-07-10 14:25:50 +00:00
2001-09-04 13:27:31 +00:00
max2 () # Returns larger of two numbers.
2011-05-03 15:38:48 +00:00
{ # Note: numbers compared must be less than 250.
2001-07-10 14:25:50 +00:00
if [ -z "$2" ]
then
2001-09-04 13:27:31 +00:00
return $E_PARAM_ERR
2001-07-10 14:25:50 +00:00
fi
if [ "$1" -eq "$2" ]
then
return $EQUAL
else
if [ "$1" -gt "$2" ]
then
return $1
else
return $2
fi
fi
}
max2 33 34
return_val=$?
2001-09-04 13:27:31 +00:00
if [ "$return_val" -eq $E_PARAM_ERR ]
2001-07-10 14:25:50 +00:00
then
echo "Need to pass two parameters to the function."
elif [ "$return_val" -eq $EQUAL ]
then
echo "The two numbers are equal."
else
echo "The larger of the two numbers is $return_val."
fi
exit 0
2002-04-01 16:04:17 +00:00
# Exercise (easy):
# ---------------
# Convert this to an interactive script,
#+ that is, have the script ask for input (two numbers).