#!/bin/bash # Another version of the "column totaler" script #+ that adds up a specified column (of numbers) in the target file. # This one uses indirect references. ARGS=2 E_WRONGARGS=85 if [ $# -ne "$ARGS" ] # Check for proper number of command-line args. then echo "Usage: `basename $0` filename column-number" exit $E_WRONGARGS fi filename=$1 # Name of file to operate on. column_number=$2 # Which column to total up. #===== Same as original script, up to this point =====# # A multi-line awk script is invoked by # awk " # ... # ... # ... # " # Begin awk script. # ------------------------------------------------- awk " { total += \$${column_number} # Indirect reference } END { print total } " "$filename" # Note that awk doesn't need an eval preceding \$$. # ------------------------------------------------- # End awk script. # Indirect variable reference avoids the hassles #+ of referencing a shell variable within the embedded awk script. # Thanks, Stephane Chazelas. exit $?