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 
   4 # Script by Sebastian Arming.
   5 # Lightly modified by ABS Guide author.
   6 # Used with permission (thanks!).
   7 
   8 exec >ASCII.txt         #  Save stdout to file,
   9                         #+ as in the example scripts
  10                         #+ reassign-stdout.sh and upperconv.sh.
  11 
  12 MAXNUM=256
  13 COLUMNS=5
  14 OCT=8
  15 OCTSQU=64
  16 LITTLESPACE=-3
  17 BIGSPACE=-5
  18 
  19 i=1 # Decimal counter
  20 o=1 # Octal counter
  21 
  22 while [ "$i" -lt "$MAXNUM" ]; do
  23         paddi="    $i"
  24         echo -n "${paddi: $BIGSPACE}  "    # Column spacing.
  25         paddo="00$o"
  26         echo -ne "\\${paddo: $LITTLESPACE}"
  27         echo -n "     "
  28         if (( i % $COLUMNS == 0)); then    # New line.
  29            echo
  30         fi
  31         ((i++, o++))
  32         # The octal notation for 8 is 10 and 80 -> 100.
  33         (( i % $OCT == 0))    && ((o+=2))
  34         (( i % $OCTSQU == 0)) && ((o+=20))
  35         # We don't have to count past 0777.
  36 done
  37 
  38 exit 0

    Place for this material granted by linux and technology portal www.net4me.ru