#!/bin/sh
#
# Copyright (c) 2008 by Bruce Blinn
#
# NAME
#	addcolumn - add a column of numbers
#
# SYNOPSIS
#	addcolumn [column]
#
# DESCRIPTION
#	This command will read its standard input and add the
#	numbers in the specified column.  If column is omitted, the
#	numbers in the first column are added.
#
# RETURN VALUE
#	0	Successful completion.
#	1	Error - see the message written to standard error.
#
####################################################################
CMDNAME=$(basename $0)
USAGE="Usage: $CMDNAME [column]"

if [ $# -gt 1 ]; then
	echo "$USAGE" 1>&2
	exit 1
fi

COLUMN=${1:-1}

expr "$COLUMN" + 1 >/dev/null 2>&1
if [ $? -ge 2 ]; then
	echo "Usage: $CMDNAME argument is not numeric." 1>&2
	exit 1
fi

awk '{total+=$'$COLUMN'} END {print total}'
