Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting
Prev |  | Next

# Appendix T. ASCII Table

Traditionally, a book of this sort has an ASCII Table appendix. This book does not. Instead, here are several short scripts, each of which generates a complete ASCII table.

Example T-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.

Example T-2. Another ASCII table script

       1 #!/bin/bash
       2 # Script author: Joseph Steinhauser
       3 # Lightly edited by ABS Guide author, but not commented.
       4 # Used in ABS Guide with permission.
       5
       6 #-------------------------------------------------------------------------
       7 #-- File:  ascii.sh    Print ASCII chart, base 10/8/16         (JETS-2012)
       8 #-------------------------------------------------------------------------
       9 #-- Usage: ascii [oct|dec|hex|help|8|10|16]
      10 #--
      11 #-- This script prints out a summary of ASCII char codes from Zero to 127.
      12 #-- Numeric values may be printed in Base10, Octal, or Hex.
      13 #--
      14 #-- Format Based on: /usr/share/lib/pub/ascii with base-10 as default.
      15 #-- For more detail, man ascii . . .
      16 #-------------------------------------------------------------------------
      17
      18 [ -n "$BASH_VERSION" ] && shopt -s extglob
      19
      20 case "$1" in
      21    oct|[Oo]?([Cc][Tt])|8)       Obase=Octal;  Numy=3o;;
      22    hex|[Hh]?([Ee][Xx])|16|[Xx]) Obase=Hex;    Numy=2X;;
      23    help|?(-)[h?])        sed -n '2,/^[ ]*$/p' $0;exit;;
      24    code|[Cc][Oo][Dd][Ee])sed -n '/case/,$p'   $0;exit;;
      25    *) Obase=Decimal
      26 esac # CODE is actually shorter than the chart!
      27
      28 printf "\t\t## $Obase ASCII Chart ##\n\n"; FM1="|%0${Numy:-3d}"; LD=-1
      29
      30 AB="nul soh stx etx eot enq ack bel bs tab nl vt np cr so si dle"
      31 AD="dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us sp"
      32
      33 for TOK in $AB $AD; do ABR[$((LD+=1))]=$TOK; done;
      34 ABR[127]=del
      35
      36 IDX=0
      37 while [ $IDX -le 127 ] && CHR="${ABR[$IDX]}"
      38    do ((${#CHR}))&& FM2='%-3s'|| FM2=`printf '\\\\%o  ' $IDX`
      39       printf "$FM1 $FM2" "$IDX" $CHR; (( (IDX+=1)%8))||echo '|'
      40    done
      41
      42 exit $?

Example T-3. A third ASCII table script, using awk

       1 #!/bin/bash
       2 # ASCII table script, using awk.
       3 # Author: Joseph Steinhauser
       4 # Used in ABS Guide with permission.
       5
       6
       7 #-------------------------------------------------------------------------
       8 #-- File:  ascii     Print ASCII chart, base 10/8/16         (JETS-2010)
       9 #-------------------------------------------------------------------------
      10 #-- Usage: ascii [oct|dec|hex|help|8|10|16]
      11 #--
      12 #-- This script prints a summary of ASCII char codes from Zero to 127.
      13 #-- Numeric values may be printed in Base10, Octal, or Hex (Base16).
      14 #--
      15 #-- Format Based on: /usr/share/lib/pub/ascii with base-10 as default.
      16 #-- For more detail, man ascii
      17 #-------------------------------------------------------------------------
      18
      19 [ -n "$BASH_VERSION" ] && shopt -s extglob
      20
      21 case "$1" in
      22    oct|[Oo]?([Cc][Tt])|8)       Obase=Octal;  Numy=3o;;
      23    hex|[Hh]?([Ee][Xx])|16|[Xx]) Obase=Hex;    Numy=2X;;
      24    help|?(-)[h?])        sed -n '2,/^[ ]*$/p' $0;exit;;
      25    code|[Cc][Oo][Dd][Ee])sed -n '/case/,$p'   $0;exit;;
      26    *) Obase=Decimal
      27 esac
      28 export Obase   # CODE is actually shorter than the chart!
      29
      30 awk 'BEGIN{print "\n\t\t## "ENVIRON["Obase"]" ASCII Chart ##\n"
      31            ab="soh,stx,etx,eot,enq,ack,bel,bs,tab,nl,vt,np,cr,so,si,dle,"
      32            ad="dc1,dc2,dc3,dc4,nak,syn,etb,can,em,sub,esc,fs,gs,rs,us,sp"
      33            split(ab ad,abr,",");abr[0]="nul";abr[127]="del";
      34            fm1="|%0'"${Numy:- 4d}"' %-3s"
      35            for(idx=0;idx<128;idx++){fmt=fm1 (++colz%8?"":"|\n")
      36            printf(fmt,idx,(idx in abr)?abr[idx]:sprintf("%c",idx))} }'
      37
      38 exit $?

Prev | Home | Next
Copyright |  | Index