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

29 lines
807 B
Bash

#!/bin/bash
# hypotenuse.sh: Returns the "hypotenuse" of a right triangle.
# (square root of sum of squares of the "legs")
ARGS=2 # Script needs sides of triangle passed.
E_BADARGS=85 # Wrong number of arguments.
if [ $# -ne "$ARGS" ] # Test number of arguments to script.
then
echo "Usage: `basename $0` side_1 side_2"
exit $E_BADARGS
fi
AWKSCRIPT=' { printf( "%3.7f\n", sqrt($1*$1 + $2*$2) ) } '
# command(s) / parameters passed to awk
# Now, pipe the parameters to awk.
echo -n "Hypotenuse of $1 and $2 = "
echo $1 $2 | awk "$AWKSCRIPT"
# ^^^^^^^^^^^^
# An echo-and-pipe is an easy way of passing shell parameters to awk.
exit
# Exercise: Rewrite this script using 'bc' rather than awk.
# Which method is more intuitive?