initial commit
This commit is contained in:
234
chapter13/float_add.ksh
Executable file
234
chapter13/float_add.ksh
Executable file
@@ -0,0 +1,234 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: float_add.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 03/01/2007
|
||||
# REV: 1.1.A
|
||||
#
|
||||
# PURPOSE: This shell script is used to add a list of numbers
|
||||
# together. The numbers can be either integers or floating-
|
||||
# point numbers. For floating-point numbers the user has
|
||||
# the option of specifying a scale of the number of digits to
|
||||
# the right of the decimal point. The scale is set by adding
|
||||
# a -s or -S followed by an integer number.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 ==> This script completed without error
|
||||
# 1 ==> Usage error
|
||||
# 2 ==> This script exited on a trapped signal
|
||||
#
|
||||
# REV. LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to debug without any command execution
|
||||
#
|
||||
########################################################
|
||||
############## DEFINE VARIABLE HERE ####################
|
||||
########################################################
|
||||
|
||||
SCRIPT_NAME=$(basename $0) # The name of this shell script
|
||||
SCALE="0" # Initialize the scale value to zero
|
||||
NUM_LIST= # Initialize the NUM_LIST variable to NULL
|
||||
COUNT=0 # Initialize the counter to zero
|
||||
MAX_COUNT=$# # Set MAX_COUNT to the total number of
|
||||
# command-line arguments.
|
||||
|
||||
########################################################
|
||||
################ FUNCTIONS #############################
|
||||
########################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nPURPOSE: Adds a list of numbers together\n"
|
||||
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2...Nn"
|
||||
echo "\nFor an integer result without any significant decimal places..."
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||
echo "OR for 4 significant decimal places"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 8.09838 2048 65536 42.632"
|
||||
echo "\n\t...EXITING...\n"
|
||||
}
|
||||
|
||||
########################################################
|
||||
|
||||
function exit_trap
|
||||
{
|
||||
echo "\n...EXITING on trapped signal...\n"
|
||||
}
|
||||
|
||||
########################################################
|
||||
################# START OF MAIN ########################
|
||||
########################################################
|
||||
|
||||
###### Set a Trap ######
|
||||
|
||||
trap 'exit_trap; exit 2' 1 2 3 15
|
||||
|
||||
########################################################
|
||||
|
||||
# Check for at least two command-line arguments
|
||||
|
||||
if [ $# -lt 2 ]
|
||||
then
|
||||
echo "\nERROR: Please provide a list of numbers to add"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Parse the command-line arguments to find the scale value, if present.
|
||||
|
||||
while getopts ":s:S:" ARGUMENT
|
||||
do
|
||||
case $ARGUMENT in
|
||||
s|S) SCALE=$OPTARG
|
||||
;;
|
||||
\?) # Because we may have negative numbers we need
|
||||
# to test to see if the ARGUMENT that begins with a
|
||||
# hyphen (-) is a number, and not an invalid switch!!!
|
||||
|
||||
for TST_ARG in $*
|
||||
do
|
||||
if [[ $(echo $TST_ARG | cut -c1) = '-' ]] \
|
||||
&& [ $TST_ARG != '-s' -a $TST_ARG != '-S' ]
|
||||
then
|
||||
case $TST_ARG in
|
||||
+([-0-9])) : # No-op, do nothing
|
||||
;;
|
||||
+([-0-9].[0-9]))
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([-.0-9])) : # No-op, do nothing
|
||||
;;
|
||||
*) echo "\nERROR: Invalid argument \
|
||||
on the command line"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
done
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Parse through the command-line arguments and gather a list
|
||||
# of numbers to add together and test each value.
|
||||
|
||||
while ((COUNT < MAX_COUNT))
|
||||
do
|
||||
((COUNT = COUNT + 1))
|
||||
TOKEN=$1 # Grab a command-line argument on each loop iteration
|
||||
case $TOKEN in # Test each value and look for a scale value.
|
||||
-s|-S) shift 2
|
||||
((COUNT = COUNT + 1))
|
||||
;;
|
||||
-s${SCALE}) shift
|
||||
;;
|
||||
-S${SCALE}) shift
|
||||
;;
|
||||
*) # Add the number ($TOKEN) to the list
|
||||
NUM_LIST="${NUM_LIST} $TOKEN"
|
||||
((COUNT < MAX_COUNT)) && shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Ensure that the scale is an integer value
|
||||
|
||||
case $SCALE in
|
||||
# Test for an integer
|
||||
+([0-9])) : # No-Op - Do Nothing
|
||||
;;
|
||||
*) echo "\nERROR: Invalid scale - $SCALE - \
|
||||
Must be an integer"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
########################################################
|
||||
|
||||
# Check each number supplied to ensure that the "numbers"
|
||||
# are either integers or floating-point numbers.
|
||||
|
||||
for NUM in $NUM_LIST
|
||||
do
|
||||
case $NUM in
|
||||
+([0-9])) # Check for an integer
|
||||
: # No-op, do nothing.
|
||||
;;
|
||||
+([-0-9])) # Check for a negative whole number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([0-9]|[.][0-9]))
|
||||
# Check for a positive floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+(+[0-9][.][0-9]))
|
||||
# Check for a positive floating-point number
|
||||
# with a + prefix
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+(-[0-9][.][0-9]))
|
||||
# Check for a negative floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([-.0-9]))
|
||||
# Check for a negative floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([+.0-9]))
|
||||
# Check for a positive floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
*) echo "\nERROR: $NUM is NOT a valid number"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Build the list of numbers to add
|
||||
|
||||
ADD= # Initialize the ADD variable to NULL
|
||||
PLUS= # Initialize the PLUS variable to NULL
|
||||
|
||||
# Loop through each number and build a math statement that
|
||||
# will add all of the numbers together.
|
||||
|
||||
for X in $NUM_LIST
|
||||
do
|
||||
# If the number has a + prefix, remove it!
|
||||
if [[ $(echo $X | cut -c1) = '+' ]]
|
||||
then
|
||||
X=$(echo $X | cut -c2-)
|
||||
fi
|
||||
ADD="$ADD $PLUS $X"
|
||||
PLUS="+"
|
||||
done
|
||||
|
||||
########################################################
|
||||
# Do the math here by using a here document to supply
|
||||
# input to the bc command. The sum of the numbers is
|
||||
# assigned to the SUM variable.
|
||||
|
||||
echo "\nSCALE is $SCALE\n"
|
||||
|
||||
|
||||
SUM=$(bc <<EOF
|
||||
scale=$SCALE
|
||||
(${ADD})
|
||||
EOF)
|
||||
|
||||
########################################################
|
||||
|
||||
# Present the result of the addition to the user.
|
||||
|
||||
echo "\nThe sum of: $ADD"
|
||||
echo "\nis: ${SUM}\n"
|
||||
131
chapter13/float_average.ksh
Executable file
131
chapter13/float_average.ksh
Executable file
@@ -0,0 +1,131 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: float_aaverage.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 03/01/2007
|
||||
# REV: 1.1.A
|
||||
#
|
||||
# PURPOSE: This script is used to average a list of
|
||||
# floating-point numbers.
|
||||
#
|
||||
# EXIT STATUS:
|
||||
# 0 ==> This script completed without error
|
||||
# 1 ==> Usage error
|
||||
# 2 ==> This script exited on a trapped signal
|
||||
#
|
||||
# REV. LIST:
|
||||
#
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to debug without any command execution
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
ARG1="$1"
|
||||
ARG2="$2"
|
||||
ARG_LIST="$*"
|
||||
TOTAL_TOKENS="$#"
|
||||
SCALE="0"
|
||||
|
||||
########################################################
|
||||
################ FUNCTIONS #############################
|
||||
########################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\n\n"
|
||||
echo "Numbers to average...\n"
|
||||
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2...N#"
|
||||
echo "\nFor an integer result without any significant decimal places..."
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||
echo "OR for 4 significant decimal places"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 8.09838 2048 65536 42.632\n"
|
||||
echo "Try again...EXITING...\n"
|
||||
}
|
||||
########################################################
|
||||
|
||||
function exit_trap
|
||||
{
|
||||
echo "\n...EXITING on trapped signal...\n"
|
||||
}
|
||||
|
||||
########################################################
|
||||
################ START OF MAIN #########################
|
||||
########################################################
|
||||
|
||||
###### Set a Trap ######
|
||||
|
||||
trap 'exit_trap; exit 2' 1 2 3 15
|
||||
|
||||
########################
|
||||
|
||||
if [ $# -lt 2 ]
|
||||
then
|
||||
echo "\nIncorrect number of command auguments...Nothing to average..."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ $ARG1 = "-s" ] || [ $ARG1 = "-S" ]
|
||||
then
|
||||
echo $ARG2 | grep [[:digit:]] >/dev/null 2>&1
|
||||
if [ $? -ne "0" ]
|
||||
then
|
||||
echo "\nERROR: Invalid argument - $ARG2 ... Must be an integer...\n"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
echo $ARG2 | grep "\." >/dev/null 2>&1
|
||||
if [ $? -ne "0" ]
|
||||
then
|
||||
SCALE="$ARG2"
|
||||
NUM_LIST=`echo $ARG_LIST | cut -f 3- -d " "`
|
||||
(( TOTAL_TOKENS = $TOTAL_TOKENS - 2 ))
|
||||
else
|
||||
echo "\nERROR: Invalid scale - $ARG2 ... Scale must be an integer...\n"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
NUM_LIST=$ARG_LIST
|
||||
fi
|
||||
|
||||
for TOKEN in $NUM_LIST
|
||||
do
|
||||
echo $TOKEN | grep [[:digit:]] >/dev/null 2>&1
|
||||
RC=$?
|
||||
case $RC in
|
||||
0) cat /dev/null
|
||||
;;
|
||||
*) echo "\n$TOKEN is not a number...Invalid argument list"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
|
||||
# Build the list of numbers to add for averaging...
|
||||
|
||||
ADD=""
|
||||
PLUS=""
|
||||
for X in $NUM_LIST
|
||||
do
|
||||
ADD="$ADD $PLUS $X"
|
||||
PLUS="+"
|
||||
done
|
||||
|
||||
# Do the math here
|
||||
|
||||
AVERAGE=`bc <<EOF
|
||||
scale=$SCALE
|
||||
(${ADD}) / $TOTAL_TOKENS
|
||||
EOF`
|
||||
|
||||
########################################################
|
||||
|
||||
# Present the result of the average to the user.
|
||||
|
||||
echo "\nThe averaging equation: (${ADD}) / $TOTAL_TOKENS"
|
||||
echo "\nto a scale of $SCALE is: ${AVERAGE}\n"
|
||||
|
||||
227
chapter13/float_divide.ksh
Executable file
227
chapter13/float_divide.ksh
Executable file
@@ -0,0 +1,227 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: float_divide.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 02/23/2007
|
||||
# REV: 1.1.A
|
||||
#
|
||||
# PURPOSE: This shell script is used to divide two numbers.
|
||||
# The numbers can be either integers or floating-point
|
||||
# numbers. For floating-point numbers the user has the
|
||||
# option to specify a scale of the number of digits to
|
||||
# the right of the decimal point. The scale is set by
|
||||
# adding a -s or -S followed by an integer number.
|
||||
#
|
||||
# EXIT STATUS:
|
||||
# 0 ==> This script exited normally
|
||||
# 1 ==> Usage or syntax error
|
||||
# 2 ==> This script exited on a trapped signal
|
||||
#
|
||||
# REV. LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to debug without any command execution
|
||||
#
|
||||
########################################################
|
||||
############## DEFINE VARIABLE HERE ####################
|
||||
########################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
SCALE="0" # Initialize the scale value to zero
|
||||
NUM_LIST= # Initialize the NUM_LIST to NULL
|
||||
COUNT=0 # Initialize the counter to zero
|
||||
MAX_COUNT=$# # Set MAX_COUNT to the total number of
|
||||
# command-line arguments
|
||||
|
||||
########################################################
|
||||
################ FUNCTIONS #############################
|
||||
########################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nPURPOSE: Divides two numbers\n"
|
||||
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2"
|
||||
echo "\nFor an integer result without any significant decimal places..."
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||
echo "OR for 4 significant decimal places"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 2048.221 65536"
|
||||
echo "\n\t...EXITING...\n"
|
||||
}
|
||||
|
||||
########################################################
|
||||
|
||||
function exit_trap
|
||||
{
|
||||
echo "\n...EXITING on trapped signal...\n"
|
||||
}
|
||||
|
||||
########################################################
|
||||
################ START OF MAIN #########################
|
||||
########################################################
|
||||
|
||||
###### Set a Trap ######
|
||||
|
||||
trap 'exit_trap; exit 2' 1 2 3 15
|
||||
|
||||
########################
|
||||
|
||||
# Check for at least two command-line arguments
|
||||
# and not more than four
|
||||
|
||||
if (($# < 2))
|
||||
then
|
||||
echo "\nERROR: Too few command line arguments"
|
||||
usage
|
||||
exit 1
|
||||
elif (($# > 4))
|
||||
then
|
||||
echo "\nERROR: Too many command line arguments"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Parse the command-line arguments to find the scale value, if present.
|
||||
|
||||
while getopts ":s:S:" ARGUMENT
|
||||
do
|
||||
case $ARGUMENT in
|
||||
s|S) SCALE=$OPTARG
|
||||
;;
|
||||
\?) # Because we may have negative numbers we need
|
||||
# to test to see if the ARGUMENT that begins with a
|
||||
# hyphen (-) is a number, and not an invalid switch!!!
|
||||
|
||||
for TST_ARG in $*
|
||||
do
|
||||
if [[ $(echo $TST_ARG | cut -c1) = '-' ]] \
|
||||
&& [ $TST_ARG != '-s' -a $TST_ARG != '-S' ]
|
||||
then
|
||||
case $TST_ARG in
|
||||
+([-0-9])) : # No-op, do nothing
|
||||
;;
|
||||
+([-0-9].[0-9]))
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([-.0-9])) : # No-op, do nothing
|
||||
;;
|
||||
*) echo "\nERROR: $TST_ARG is an invalid argument\n"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
done
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Parse through the command-line arguments and gather a list
|
||||
# of numbers to divide.
|
||||
|
||||
TOTAL_NUMBERS=0
|
||||
|
||||
while ((COUNT < MAX_COUNT))
|
||||
do
|
||||
((COUNT = COUNT + 1))
|
||||
TOKEN=$1
|
||||
case $TOKEN in
|
||||
-s|-S) shift 2
|
||||
((COUNT = COUNT + 1))
|
||||
;;
|
||||
-s${SCALE}) shift
|
||||
;;
|
||||
-S${SCALE}) shift
|
||||
;;
|
||||
*) ((TOTAL_NUMBERS = TOTAL_NUMBERS + 1))
|
||||
if ((TOTAL_NUMBERS == 1))
|
||||
then
|
||||
DIVIDEND=$TOKEN
|
||||
elif ((TOTAL_NUMBERS == 2))
|
||||
then
|
||||
DIVISOR=$TOKEN
|
||||
else
|
||||
echo "ERROR: Too many numbers to divide"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
NUM_LIST="$NUM_LIST $TOKEN"
|
||||
((COUNT < MAX_COUNT)) && shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Ensure that the scale is an integer value
|
||||
|
||||
case $SCALE in
|
||||
+([0-9])) : # No-op - Do Nothing
|
||||
;;
|
||||
*) echo "\nERROR: Invalid scale - $SCALE - Must be an integer"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
########################################################
|
||||
|
||||
# Check each number supplied to ensure that the "numbers"
|
||||
# are either integers or floating-point numbers.
|
||||
|
||||
for NUM in $NUM_LIST
|
||||
do
|
||||
case $NUM in
|
||||
+([0-9])) # Check for an integer
|
||||
: # No-op, do nothing.
|
||||
;;
|
||||
+([-0-9])) # Check for a negative whole number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([0-9]|[.][0-9]))
|
||||
# Check for a positive floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+(+[0-9]|[.][0-9]))
|
||||
# Check for a positive floating-point number
|
||||
# with a + prefix
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([-0-9]|.[0-9]))
|
||||
# Check for a negative floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+(-.[0-9]))
|
||||
# Check for a negative floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([+.0-9]))
|
||||
# Check for a positive floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
*) echo "\nERROR: $NUM is NOT a valid number"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Do the math here by using a here document to supply
|
||||
# input to the bc command. The quotient of the division is
|
||||
# assigned to the QUOTIENT variable.
|
||||
|
||||
QUOTIENT=$(bc <<EOF
|
||||
scale=$SCALE
|
||||
$DIVIDEND / $DIVISOR
|
||||
EOF)
|
||||
|
||||
########################################################
|
||||
|
||||
# Present the result of the division to the user.
|
||||
|
||||
echo "\nThe quotient of: $DIVIDEND / $DIVISOR"
|
||||
echo "\nto a scale of $SCALE is ${QUOTIENT}\n"
|
||||
233
chapter13/float_multiply.ksh
Executable file
233
chapter13/float_multiply.ksh
Executable file
@@ -0,0 +1,233 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: float_multiply.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 02/23/2007
|
||||
# REV: 1.1.P
|
||||
#
|
||||
# PURPOSE: This shell script is used to multiply a list of numbers
|
||||
# together. The numbers can be either integers or floating-
|
||||
# point numbers. For floating-point numbers the user has
|
||||
# the option of specifying a scale of the number of digits to
|
||||
# the right of the decimal point. The scale is set by adding
|
||||
# a -s or -S followed by an integer number.
|
||||
#
|
||||
# EXIT STATUS:
|
||||
# 0 ==> This script/function exited normally
|
||||
# 1 ==> Usage or syntax error
|
||||
# 2 ==> This script/function exited on a trapped signal
|
||||
#
|
||||
# REV. LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to debug without any command execution
|
||||
#
|
||||
########################################################
|
||||
############## DEFINE VARIABLE HERE ####################
|
||||
########################################################
|
||||
|
||||
SCRIPT_NAME=$(basename $0) # The name of this shell script
|
||||
SCALE="0" # Initialize the scale value to zero
|
||||
NUM_LIST= # Initialize the NUM_LIST to NULL
|
||||
COUNT=0 # Initialize the counter to zero
|
||||
MAX_COUNT=$# # Set MAX_COUNT to the total number of
|
||||
# command-line arguments
|
||||
|
||||
########################################################
|
||||
################ FUNCTIONS #############################
|
||||
########################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nPURPOSE: Multiplies a list of numbers together\n"
|
||||
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2...Nn"
|
||||
echo "\nFor an integer result without any significant decimal places..."
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||
echo "OR for 4 significant decimal places"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 8.09838 2048 65536 42.632"
|
||||
echo "\n\t...EXITING...\n"
|
||||
}
|
||||
|
||||
########################################################
|
||||
|
||||
function exit_trap
|
||||
{
|
||||
echo "\n...EXITING on trapped signal...\n"
|
||||
}
|
||||
|
||||
########################################################
|
||||
################# START OF MAIN ########################
|
||||
########################################################
|
||||
|
||||
###### Set a Trap ######
|
||||
|
||||
trap 'exit_trap; exit 2' 1 2 3 15
|
||||
|
||||
########################################################
|
||||
|
||||
# Check for at least two command-line arguments
|
||||
|
||||
if (($# < 2))
|
||||
then
|
||||
echo "\nERROR: Please provide a list of numbers to multiply"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
########################################################
|
||||
|
||||
# Parse the command-line arguments to find the scale value, if present.
|
||||
|
||||
while getopts ":s:S:" ARGUMENT
|
||||
do
|
||||
case $ARGUMENT in
|
||||
s|S) SCALE=$OPTARG
|
||||
;;
|
||||
\?) # Because we may have negative numbers we need
|
||||
# to test to see if the ARGUMENT that begins with a
|
||||
# hyphen (-) is a number, and not an invalid switch!!!
|
||||
|
||||
for TST_ARG in $*
|
||||
do
|
||||
if [[ $(echo $TST_ARG | cut -c1) = '-' ]] \
|
||||
&& [ $TST_ARG != '-s' -a $TST_ARG != '-S' ]
|
||||
then
|
||||
case $TST_ARG in
|
||||
+([-0-9])) : # No-op, do nothing
|
||||
;;
|
||||
+([-0-9].[0-9]))
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([-.0-9])) : # No-op, do nothing
|
||||
;;
|
||||
*) echo "\nERROR: $TST_ARG is an invalid argument\n"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
done
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Parse through the command-line arguments and gather a list
|
||||
# of numbers to multiply together.
|
||||
|
||||
while ((COUNT < MAX_COUNT))
|
||||
do
|
||||
((COUNT = COUNT + 1))
|
||||
TOKEN=$1
|
||||
case $TOKEN in
|
||||
-s|-S) shift 2
|
||||
((COUNT = COUNT + 1))
|
||||
;;
|
||||
-s${SCALE}) shift
|
||||
;;
|
||||
-S${SCALE}) shift
|
||||
;;
|
||||
*) NUM_LIST="${NUM_LIST} $TOKEN"
|
||||
((COUNT < MAX_COUNT)) && shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Ensure that the scale is an integer value
|
||||
|
||||
case $SCALE in
|
||||
+([0-9])) : # No-Op - Do Nothing
|
||||
;;
|
||||
*) echo "\nERROR: Invalid scale - $SCALE - Must be an integer"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
########################################################
|
||||
|
||||
# Check each number supplied to ensure that the "numbers"
|
||||
# are either integers or floating-point numbers.
|
||||
|
||||
for NUM in $NUM_LIST
|
||||
do
|
||||
case $NUM in
|
||||
+([0-9])) # Check for an integer
|
||||
: # No-op, do nothing.
|
||||
;;
|
||||
+([-0-9])) # Check for a negative whole number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([0-9]|[.][0-9]))
|
||||
# Check for a positive floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+(+[0-9]|[.][0-9]))
|
||||
# Check for a positive floating-point number
|
||||
# with a + prefix
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([-0-9]|.[0-9]))
|
||||
# Check for a negative floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+(-.[0-9]))
|
||||
# Check for a negative floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([+.0-9]))
|
||||
# Check for a positive floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
*) echo "\nERROR: $NUM is NOT a valid number"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Build the list of numbers to multiply
|
||||
|
||||
MULTIPLY= # Initialize the MULTIPLY variable to NULL
|
||||
TIMES= # Initialize the TIMES variable to NULL
|
||||
|
||||
# Loop through each number and build a math statement that
|
||||
# will multiply all of the numbers together.
|
||||
|
||||
for X in $NUM_LIST
|
||||
do
|
||||
# If the number has a + prefix, remove it!
|
||||
if [[ $(echo $X | cut -c1) = '+' ]]
|
||||
then
|
||||
X=$(echo $X | cut -c2-)
|
||||
fi
|
||||
MULTIPLY="$MULTIPLY $TIMES $X"
|
||||
TIMES='*'
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Do the math here by using a here document to supply
|
||||
# input to the bc command. The product of the multiplication
|
||||
# of the numbers is assigned to the PRODUCT variable.
|
||||
|
||||
echo "\nSCALE is $SCALE\n"
|
||||
|
||||
|
||||
PRODUCT=$(bc <<EOF
|
||||
scale=$SCALE
|
||||
$MULTIPLY
|
||||
EOF)
|
||||
|
||||
########################################################
|
||||
|
||||
# Present the result of the multiplication to the user.
|
||||
|
||||
echo "\nThe product of: $MULTIPLY"
|
||||
echo "\nto a scale of $SCALE is ${PRODUCT}\n"
|
||||
226
chapter13/float_subtract.ksh
Executable file
226
chapter13/float_subtract.ksh
Executable file
@@ -0,0 +1,226 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: float_subtract.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 02/23/2007
|
||||
# REV: 1.1.A
|
||||
#
|
||||
# PURPOSE: This shell script is used to subtract a list of numbers.
|
||||
# The numbers can be either integers or floating-point
|
||||
# numbers. For floating-point numbers the user has the
|
||||
# option to specify a scale of the number of digits to
|
||||
# the right of the decimal point. The scale is set by
|
||||
# adding a -s or -S followed by an integer number.
|
||||
#
|
||||
# EXIT STATUS:
|
||||
# 0 ==> This script completed without error
|
||||
# 1 ==> Usage error
|
||||
# 2 ==> This script exited on a trapped signal
|
||||
#
|
||||
# REV. LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to debug without any command execution
|
||||
#
|
||||
########################################################
|
||||
############## DEFINE VARIABLE HERE ####################
|
||||
########################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0` # The name of this shell script
|
||||
SCALE="0" # Initialize the scale value to zero
|
||||
NUM_LIST= # Initialize the NUM_LIST to NULL
|
||||
COUNT=0 # Initialize the counter to zero
|
||||
MAX_COUNT=$# # Set MAX_COUNT to the total number of
|
||||
# command-line arguments
|
||||
|
||||
########################################################
|
||||
################ FUNCTIONS #############################
|
||||
########################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nPURPOSE: Subtracts a list of numbers\n"
|
||||
echo "USAGE: $SCRIPT_NAME [-s scale_value] N1 N2...Nn"
|
||||
echo "\nFor an integer result without any significant decimal places..."
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 2048.221 65536 \n"
|
||||
echo "OR for 4 significant decimal places"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME -s 4 8.09838 2048 65536 42.632"
|
||||
echo "\n\t...EXITING...\n"
|
||||
}
|
||||
|
||||
########################################################
|
||||
|
||||
function exit_trap
|
||||
{
|
||||
echo "\n...EXITING on trapped signal...\n"
|
||||
}
|
||||
########################################################
|
||||
################ START OF MAIN #########################
|
||||
########################################################
|
||||
|
||||
###### Set a Trap ######
|
||||
|
||||
trap 'exit_trap; exit 2' 1 2 3 15
|
||||
|
||||
########################
|
||||
|
||||
# Check for at least two command-line arguments
|
||||
|
||||
if (($# < 2))
|
||||
then
|
||||
echo "\nERROR: Please provide a list of numbers to subtract"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
# Parse the command-line arguments to find the scale value, if present.
|
||||
|
||||
while getopts ":s:S:" ARGUMENT
|
||||
do
|
||||
case $ARGUMENT in
|
||||
s|S) SCALE=$OPTARG
|
||||
;;
|
||||
\?) # Because we may have negative numbers we need
|
||||
# to test to see if the ARGUMENT that begins with a
|
||||
# hyphen (-) is a number, and not an invalid switch!!!
|
||||
|
||||
for TST_ARG in $*
|
||||
do
|
||||
if [[ $(echo $TST_ARG | cut -c1) = '-' ]] \
|
||||
&& [ $TST_ARG != '-s' -a $TST_ARG != '-S' ]
|
||||
then
|
||||
case $TST_ARG in
|
||||
+([-0-9])) : # No-op, do nothing
|
||||
;;
|
||||
+([-0-9].[0-9]))
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([-.0-9])) : # No-op, do nothing
|
||||
;;
|
||||
*) echo "\nERROR: Invalid argument on the command line"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
done
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Parse through the command-line arguments and gather a list
|
||||
# of numbers to subtract.
|
||||
|
||||
while ((COUNT < MAX_COUNT))
|
||||
do
|
||||
((COUNT = COUNT + 1))
|
||||
TOKEN=$1
|
||||
case $TOKEN in
|
||||
-s|-S) shift 2
|
||||
((COUNT = COUNT + 1))
|
||||
;;
|
||||
-s${SCALE}) shift
|
||||
;;
|
||||
-S${SCALE}) shift
|
||||
;;
|
||||
*) NUM_LIST="${NUM_LIST} $TOKEN"
|
||||
((COUNT < MAX_COUNT)) && shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Ensure that the scale is an integer value
|
||||
|
||||
case $SCALE in
|
||||
+([0-9])) : # No-Op - Do Nothing
|
||||
;;
|
||||
*) echo "\nERROR: Invalid scale - $SCALE - Must be an integer"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
########################################################
|
||||
|
||||
# Check each number supplied to ensure that the "numbers"
|
||||
# are either integers or floating-point numbers.
|
||||
|
||||
for NUM in $NUM_LIST
|
||||
do
|
||||
case $NUM in
|
||||
+([0-9])) # Check for an integer
|
||||
: # No-op, do nothing.
|
||||
;;
|
||||
+([-0-9])) # Check for a negative whole number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([0-9]|[.][0-9]))
|
||||
# Check for a positive floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+(+[0-9]|[.][0-9]))
|
||||
# Check for a positive floating-point number
|
||||
# with a + prefix
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([-0-9]|.[0-9]))
|
||||
# Check for a negative floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+(-[.][0-9]))
|
||||
# Check for a negative floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
+([+.0-9]))
|
||||
# Check for a positive floating-point number
|
||||
: # No-op, do nothing
|
||||
;;
|
||||
*) echo "\nERROR: $NUM is NOT a valid number"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Build the list of numbers to subtract
|
||||
|
||||
SUBTRACT= # Initialize the SUBTRACT variable to NULL
|
||||
MINUS= # Initialize the MINUS variable to NULL
|
||||
|
||||
# Loop through each number and build a math statement that
|
||||
# will subtract the numbers in the list.
|
||||
|
||||
for X in $NUM_LIST
|
||||
do
|
||||
# If the number has a + prefix, remove it!
|
||||
if [[ $(echo $X | cut -c1) = '+' ]]
|
||||
then
|
||||
X=$(echo $X | cut -c2-)
|
||||
fi
|
||||
SUBTRACT="$SUBTRACT $MINUS $X"
|
||||
MINUS='-'
|
||||
done
|
||||
|
||||
########################################################
|
||||
|
||||
# Do the math here by using a here document to supply
|
||||
# input to the bc command. The difference of the numbers is
|
||||
# assigned to the DIFFERENCE variable.
|
||||
|
||||
DIFFERENCE=$(bc <<EOF
|
||||
scale=$SCALE
|
||||
(${SUBTRACT})
|
||||
EOF)
|
||||
|
||||
########################################################
|
||||
|
||||
# Present the result of the subtraction to the user.
|
||||
|
||||
echo "\nThe difference of: $SUBTRACT"
|
||||
echo "\nis: ${DIFFERENCE}\n"
|
||||
Reference in New Issue
Block a user