#!/bin/bash #.Y / 70 83 / / / / 105 / / / / / / ;A4_QUER.Y #.YT 3 2 0 0 3 3 1 / / / #.H!a!0:linux//math/THIS!0.!c # # copyright : (C) 2009 by Thomas Bruecker =: tbrue # email : public0x05bf@bluewin.ch # # tbrue 12jun2006 # # math THIS : math utilities #+++++++++++++++++++++++++++ # # MyHash("linux//math/THIS") = 0x0921 # # * for every function: # * if returned result is not commented, the function returns 0 on no-error. # # temp: SCRIPTS=root/bin . /$SCRIPTS/io/tape_/md5sumS mathCalc() # * $* : expression # print result to stdout; # * returns # 0 if ok, { local I let "I = $*"; echo "$* = $I" } # mathMD5BlockSums() # * input: stdin; output: stdout . # { # } mathToUnit() # * convert $3 (in bytes) to unit; print result to stdout; # * returns # 0 if ok, # 1 if wrong parameter for $1, # 2 if wrong parameter for $2. # * $1 in { b,... } : unit; $2 in { D, U }, D: round down, U: round up; # $3: #bytes. { local Divisor0x0921 Result0x0921 Round0x0921 case "$2" in D) Round0x0921=0 ;; U) Round0x0921=1 ;; *) return 1 esac case "$1" in b) Divisor0x0921=1 ;; K) Divisor0x0921=1024 # in 1024 byte units. ;; kB) Divisor0x0921=1000 # in 1000 byte units. ;; M) let "Divisor0x0921 = 1024 * 1024" # in 1024 * 1024 byte units. ;; MB) Divisor0x0921=1000000 # in 1,000,000 byte units. ;; *) return 2 esac # * rounding down: Round0x0921 = 0; # * rounding up: Round0x0921 = Divisor0x0921 - 1 . let "Round0x0921 = Round0x0921 * (Divisor0x0921 - 1)" # rounding up: ( 1 + (Divisor0x0921 - 1) ) = Divisor0x0921 . let "Result0x0921 = ($3 + Round0x0921) / Divisor0x0921" echo $Result0x0921 return } math_toUnitError() # print "Usage: $1 $2 ... $n\n ..." as an error-message to stdout. # * $1 : name of program concerned by the error, # * if $i; i in {2,... ,n} is an empty string, $i is replaced by "UNIT" . { local I0x0921 # echo usage: echo -n "Usage: $1 " for (( I0x0921 = $# - 1; I0x0921--; )) do shift 1 if [ -z $1 ]; then echo -n "UNIT " else echo -n "$1 " fi done echo ";" echo " UNIT in {\"b\", \"K\", \"kB\", \"M\", \"MB\"} :" echo " b: bytes," echo " K: 1,024 byte units," echo " kB: 1,000 byte units," echo " M: 1024 * 1024 = 1,048,576 byte units," echo " MB: 1,000,000 byte units." return 0 }