| 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 $? |