Appendix R. ASCII Table

In a book of this sort it is traditional to have an ASCII Table appendix. This book does not. Instead, here is a short shell script that generates a complete ASCII table and writes it to the file ASCII.txt.


Example R-1. A script that generates an ASCII table

   1 #!/bin/bash
   2 # ascii.sh
   3 # ver. 0.2, reldate 26 Aug 2008
   4 # Patched by ABS Guide author.
   5 
   6 # Original script by Sebastian Arming.
   7 # Used with permission (thanks!).
   8 
   9 exec >ASCII.txt         #  Save stdout to file,
  10                         #+ as in the example scripts
  11                         #+ reassign-stdout.sh and upperconv.sh.
  12 
  13 MAXNUM=256
  14 COLUMNS=5
  15 OCT=8
  16 OCTSQU=64
  17 LITTLESPACE=-3
  18 BIGSPACE=-5
  19 
  20 i=1 # Decimal counter
  21 o=1 # Octal counter
  22 
  23 while [ "$i" -lt "$MAXNUM" ]; do  # We don't have to count past 400 octal.
  24         paddi="    $i"
  25         echo -n "${paddi: $BIGSPACE}  "       # Column spacing.
  26         paddo="00$o"
  27 #       echo -ne "\\${paddo: $LITTLESPACE}"   # Original.
  28         echo -ne "\\0${paddo: $LITTLESPACE}"  # Fixup.
  29 #                   ^
  30         echo -n "     "
  31         if (( i % $COLUMNS == 0)); then       # New line.
  32            echo
  33         fi
  34         ((i++, o++))
  35         # The octal notation for 8 is 10, and 64 decimal is 100 octal.
  36         (( i % $OCT == 0))    && ((o+=2))
  37         (( i % $OCTSQU == 0)) && ((o+=20))
  38 done
  39 
  40 exit $?
  41 
  42 # Compare this script with the "pr-asc.sh" example.
  43 # This one handles "unprintable" characters.
  44 
  45 # Exercise:
  46 # Rewrite this script to use decimal numbers, rather than octal.