Debian Codegraph @

col-totaler2.sh

raw symbols SCIP
1 #!/bin/bash
2
3 # Another version of the "column totaler" script
4 #+ that adds up a specified column (of numbers) in the target file.
5 # This one uses indirect references.
6
7 ARGS=2
8 E_WRONGARGS=85
9
10 if [ $# -ne "$ARGS" ] # Check for proper number of command-line args.
11 then
12 echo "Usage: `basename $0` filename column-number"
13 exit $E_WRONGARGS
14 fi
15
16 filename=$1 # Name of file to operate on.
17 column_number=$2 # Which column to total up.
18
19 #===== Same as original script, up to this point =====#
20
21
22 # A multi-line awk script is invoked by
23 # awk "
24 # ...
25 # ...
26 # ...
27 # "
28
29
30 # Begin awk script.
31 # -------------------------------------------------
32 awk "
33
34 { total += \$${column_number} # Indirect reference
35 }
36 END {
37 print total
38 }
39
40 " "$filename"
41 # Note that awk doesn't need an eval preceding \$$.
42 # -------------------------------------------------
43 # End awk script.
44
45 # Indirect variable reference avoids the hassles
46 #+ of referencing a shell variable within the embedded awk script.
47 # Thanks, Stephane Chazelas.
48
49
50 exit $?