Debian Codegraph @

letter-count.sh

raw symbols SCIP
1 #!/bin/bash
2 # letter-count.sh: Counting letter occurrences in a text file.
3 # Written by Stefano Palmeri.
4 # Used in ABS Guide with permission.
5 # Slightly modified by document author.
6
7 MINARGS=2 # Script requires at least two arguments.
8 E_BADARGS=65
9 FILE=$1
10
11 let LETTERS=$#-1 # How many letters specified (as command-line args).
12 # (Subtract 1 from number of command-line args.)
13
14
15 show_help(){
16 echo
17 echo Usage: `basename $0` file letters
18 echo Note: `basename $0` arguments are case sensitive.
19 echo Example: `basename $0` foobar.txt G n U L i N U x.
20 echo
21 }
22
23 # Checks number of arguments.
24 if [ $# -lt $MINARGS ]; then
25 echo
26 echo "Not enough arguments."
27 echo
28 show_help
29 exit $E_BADARGS
30 fi
31
32
33 # Checks if file exists.
34 if [ ! -f $FILE ]; then
35 echo "File \"$FILE\" does not exist."
36 exit $E_BADARGS
37 fi
38
39
40
41 # Counts letter occurrences .
42 for n in `seq $LETTERS`; do
43 shift
44 if [[ `echo -n "$1" | wc -c` -eq 1 ]]; then # Checks arg.
45 echo "$1" -\> `cat $FILE | tr -cd "$1" | wc -c` # Counting.
46 else
47 echo "$1 is not a single char."
48 fi
49 done
50
51 exit $?
52
53 # This script has exactly the same functionality as letter-count2.sh,
54 #+ but executes faster.
55 # Why?