| 1 | #!/bin/bash |
| 2 | ########################################################################### |
| 3 | # Shellscript: base.sh - print number to different bases (Bourne Shell) |
| 4 | # Author : Heiner Steven ([email protected]) |
| 5 | # Date : 07-03-95 |
| 6 | # Category : Desktop |
| 7 | # $Id: base.sh,v 1.2 2000/02/06 19:55:35 heiner Exp $ |
| 8 | # ==> Above line is RCS ID info. |
| 9 | ########################################################################### |
| 10 | # Description |
| 11 | # |
| 12 | # Changes |
| 13 | # 21-03-95 stv fixed error occuring with 0xb as input (0.2) |
| 14 | ########################################################################### |
| 15 | |
| 16 | # ==> Used in ABS Guide with the script author's permission. |
| 17 | # ==> Comments added by ABS Guide author. |
| 18 | |
| 19 | NOARGS=85 |
| 20 | PN=`basename "$0"` # Program name |
| 21 | VER=`echo '$Revision: 1.2 $' | cut -d' ' -f2` # ==> VER=1.2 |
| 22 | |
| 23 | Usage () { |
| 24 | echo "$PN - print number to different bases, $VER (stv '95) |
| 25 | usage: $PN [number ...] |
| 26 | |
| 27 | If no number is given, the numbers are read from standard input. |
| 28 | A number may be |
| 29 | binary (base 2) starting with 0b (i.e. 0b1100) |
| 30 | octal (base 8) starting with 0 (i.e. 014) |
| 31 | hexadecimal (base 16) starting with 0x (i.e. 0xc) |
| 32 | decimal otherwise (i.e. 12)" >&2 |
| 33 | exit $NOARGS |
| 34 | } # ==> Prints usage message. |
| 35 | |
| 36 | Msg () { |
| 37 | for i # ==> in [list] missing. Why? |
| 38 | do echo "$PN: $i" >&2 |
| 39 | done |
| 40 | } |
| 41 | |
| 42 | Fatal () { Msg "$@"; exit 66; } |
| 43 | |
| 44 | PrintBases () { |
| 45 | # Determine base of the number |
| 46 | for i # ==> in [list] missing... |
| 47 | do # ==> so operates on command-line arg(s). |
| 48 | case "$i" in |
| 49 | 0b*) ibase=2;; # binary |
| 50 | 0x*|[a-f]*|[A-F]*) ibase=16;; # hexadecimal |
| 51 | 0*) ibase=8;; # octal |
| 52 | [1-9]*) ibase=10;; # decimal |
| 53 | *) |
| 54 | Msg "illegal number $i - ignored" |
| 55 | continue;; |
| 56 | esac |
| 57 | |
| 58 | # Remove prefix, convert hex digits to uppercase (bc needs this). |
| 59 | number=`echo "$i" | sed -e 's:^0[bBxX]::' | tr '[a-f]' '[A-F]'` |
| 60 | # ==> Uses ":" as sed separator, rather than "/". |
| 61 | |
| 62 | # Convert number to decimal |
| 63 | dec=`echo "ibase=$ibase; $number" | bc` # ==> 'bc' is calculator utility. |
| 64 | case "$dec" in |
| 65 | [0-9]*) ;; # number ok |
| 66 | *) continue;; # error: ignore |
| 67 | esac |
| 68 | |
| 69 | # Print all conversions in one line. |
| 70 | # ==> 'here document' feeds command list to 'bc'. |
| 71 | echo `bc <<! |
| 72 | obase=16; "hex="; $dec |
| 73 | obase=10; "dec="; $dec |
| 74 | obase=8; "oct="; $dec |
| 75 | obase=2; "bin="; $dec |
| 76 | ! |
| 77 | ` | sed -e 's: : :g' |
| 78 | |
| 79 | done |
| 80 | } |
| 81 | |
| 82 | while [ $# -gt 0 ] |
| 83 | # ==> Is a "while loop" really necessary here, |
| 84 | # ==>+ since all the cases either break out of the loop |
| 85 | # ==>+ or terminate the script. |
| 86 | # ==> (Above comment by Paulo Marcel Coelho Aragao.) |
| 87 | do |
| 88 | case "$1" in |
| 89 | --) shift; break;; |
| 90 | -h) Usage;; # ==> Help message. |
| 91 | -*) Usage;; |
| 92 | *) break;; # First number |
| 93 | esac # ==> Error checking for illegal input might be appropriate. |
| 94 | shift |
| 95 | done |
| 96 | |
| 97 | if [ $# -gt 0 ] |
| 98 | then |
| 99 | PrintBases "$@" |
| 100 | else # Read from stdin. |
| 101 | while read line |
| 102 | do |
| 103 | PrintBases $line |
| 104 | done |
| 105 | fi |
| 106 | |
| 107 | |
| 108 | exit |