Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate

Advertising

Unnamed
Tuesday, May 8th, 2007 at 9:41:25am UTC 

  1. #!/bin/sh
  2. #
  3. # simple mathematics calculator for the GNU Linux system.  uses "bc"
  4. # all code is GNU-GPL free software.
  5. #
  6. VERSION=0.2.3
  7. BN="$(basename $0)"
  8. DN="$(dirname $0)"
  9. [ -f /bin/echo ] && RE="/bin/echo -e" || RE="echo -e" # kludge
  10. #
  11. # if nothing is specified on the command line then we read stdin
  12. if [ -z "$1" ]
  13.     then JOE="`while read P ;do $RE $P ;done`"
  14.         $0 $JOE
  15.         exit $?
  16. fi
  17. #
  18. # if the user asked for help we display a help page of sorts.
  19. dohelp() {
  20.   case $BN in
  21.   math*)
  22.   $RE "
  23.     syntax; math <math_expression>
  24.  
  25.  
  26.         if called as any one of the many base2base functions, then simply
  27.         convert the number from the input base to the output base.
  28.         otherwise, a normal math computation of 'string = ?' is done.
  29.         and the following switch applies.
  30.  
  31. -s     silent mode regular calculations only the answer is outputted.
  32.         only for the normal \"math\" function.
  33.  
  34.  
  35. -h     this help message and of curse the help page is only one page
  36.         for all functions.
  37.  
  38. --install  lets you use the version suffixed script as an installer
  39.             for the symlinks that it will then use as function calls.
  40.  
  41.             NOTE: you should put $0 in $HOME/bin  for a
  42.             user installation, or system wide installs should be done by
  43.             root, and put in /usr/local/bin!  simply copy this script to
  44.             the correct dir and run:  math-$VERSION --install from there.
  45.   "
  46.   ;;
  47.   #
  48.   *)
  49.   $RE "
  50.    syntax; $BN <number>
  51.  
  52.         example: $BN 111
  53.           yields:$($0 111)
  54.  
  55.  
  56.          simply converts the number from the input base to the output base.
  57.          available conversions are; hex2dec hex2oct hex2bin dec2hex dec2oct
  58.          dec2bin oct2hex oct2dec oct2bin bin2hex bin2dec bin2oct
  59.  
  60. -h     show this help message.
  61.  
  62.   "
  63.   ;;
  64.   esac
  65.   exit 0
  66. }
  67. #
  68. case $1 in
  69.     -h)
  70.     dohelp
  71.     ;;
  72.     --help)
  73.     dohelp
  74.     ;;
  75. esac
  76. #
  77. # from this point on, unless you do 'bash' shell scripting, you probably
  78. # wont be able to follow much of what is happening,  or at lease why it is.
  79. # but you are more than welcome to try   :)
  80. #
  81. # the normal math function for both silent and noisy modes
  82. math() {
  83.   if [ "-s" = "$1" ]
  84.     then shift
  85.       $RE "$*" | bc -l 2> /dev/null
  86.       exit 0
  87.   fi
  88.   ANSWER="$* = $($RE "$*" | bc -l 2>/dev/null)"
  89. }
  90. #
  91. # the install function for a first time setup or reinstall.
  92. INSTALL() {
  93.     MESS='      installing in $D,
  94.     and moving original script to $D/.scripts '
  95.     LST='hex2dec hex2oct hex2bin dec2hex dec2oct dec2bin oct2hex oct2dec  oct2bin bin2hex bin2dec bin2oct'
  96.   WORK() {
  97.     mkdir -p "$D/.scripts" || { $RE "E: permission denied!" ;exit 1 ; }
  98.     cp $0 "$D/.scripts" || { $RE "E: permission denied!" ;exit 1 ; }
  99.     $RE "copied \"$0\" to \"$D/.scripts/$BN\" "
  100.     ln -s ".scripts/$BN" "$D/math" || { $RE "E: permission denied!" ;exit 1 ; }
  101.     $RE "created. \"$D/math\"  symlink"
  102.     for Q in $LST
  103.         do ln -s math "$D/$Q" && $RE "created. \"$D/$Q\"  symlink"
  104.     done
  105.     [ -h "$D/math" ] &&\
  106.      [ ! "`find "$D/" -maxdepth 1 -name $BN`" = "`find "$DN/" -maxdepth 1 -name
  107. $BN`" ] &&\
  108.       CLEANUP="rm $0" || { $RE "E: can not rm \"$0\" !" ;exit 1 ; }
  109.     $RE "$BN installed."
  110.     $RE "$PATH" | grep -v "$D" &&\
  111.      $RE "\"$D\" not in your path. you should fix that."
  112.   }
  113.     $0 --help
  114.     default="$( [ "$UID" = "0" ] && $RE "/usr/local/bin" || $RE "$HOME/bin" )"
  115.     $RE "Please provide the path to install into,\c"
  116.     read -p " [$default]: " TOM
  117.     $RE "\n"
  118.     D=${TOM:=$default}
  119.     $RE "$MESS"
  120.     WORK &&\
  121.     read -n 1 -p "is it ok to remove the origenal file  [Y,n] ?  " X
  122.     $RE "\n"
  123.     [ ! "$X" = "y" -a ! "$X" = "Y" ] && exit 0 || $CLEANUP
  124.     exit 0
  125. }
  126. #
  127. # the install switch lets you use the version suffixed script as an
  128. # installer for the symlinks that it will then use as function calls
  129. if [ "x--install" = "x$1" ]
  130.     then INSTALL
  131. fi
  132. #
  133. # the conversion function which allows all conversion methods
  134. funct() {
  135. if [ "$1" = "-s" ] >/dev/null 2>&1
  136.     then shift
  137.         for Q in $*
  138.             do Q=`$RE "$Q" | tr "[a-f]" "[A-F]"`
  139.                 ANSWER="$($RE "obase=$TT ;ibase=$FF ;$Q" | bc -l )"
  140.         done
  141.     else for Q in [email protected]
  142.             do Q=`$RE "$Q" | tr "[a-f]" "[A-F]"`
  143.                 ANSWER="$ANSWER \n $Q$F = $($RE "obase=$TT ;ibase=$FF ;$Q" | bc-l )$T"
  144.         done
  145. fi
  146. }
  147. #
  148. # the rest of these functions only set the formating for the 'funct' call
  149. hex2dec() {
  150.     T=''
  151.     TT='10'
  152.     F='_16'
  153.     FF='16'
  154. }
  155. #
  156. hex2oct() {
  157.     T='_8'
  158.     TT='8'
  159.     F='_16'
  160.     FF='16'
  161. }
  162. #
  163. hex2bin() {
  164.     T='_2'
  165.     TT='2'
  166.     F='_16'
  167.     FF='16'
  168. }
  169. #
  170. dec2hex() {
  171.     T='_16'
  172.     TT='16'
  173.     F=''
  174.     FF='10'
  175. }
  176. #
  177. dec2oct() {
  178.     T='_8'
  179.     TT='8'
  180.     F=''
  181.     FF='10'
  182. }
  183. #
  184. dec2bin() {
  185.     T='_2'
  186.     TT='2'
  187.     F=''
  188.     FF='10'
  189. }
  190. #
  191. oct2hex() {
  192.     T='_16'
  193.     TT='16'
  194.     F='_8'
  195.     FF='8'
  196. }
  197. #
  198. oct2dec() {
  199.     T=''
  200.     TT='10'
  201.     F='_8'
  202.     FF='8'
  203. }
  204. #
  205. oct2bin() {
  206.     T='_2'
  207.     TT='2'
  208.     F='_8'
  209.     FF='8'
  210. }
  211. #
  212. bin2hex() {
  213.     T='_16'
  214.     TT='16'
  215.     F='_2'
  216.     FF='2'
  217. }
  218. #
  219. bin2dec() {
  220.     T=''
  221.     TT='10'
  222.     F='_2'
  223.     FF='2'
  224. }
  225. #
  226. bin2oct() {
  227.     T='_8'
  228.     TT='8'
  229.     F='_2'
  230.     FF='2'
  231. }
  232. #
  233. # this loop checks to see what the user was asking for by checking the
  234. # symlink name and acting accordingly.
  235. case $BN in
  236.                     math)
  237.                     math $*
  238.                     ;;
  239.                     hex2dec)
  240.                     hex2dec
  241.                     funct $*
  242.                     ;;
  243.                     hex2oct)
  244.                     hex2oct
  245.                     funct $*
  246.                     ;;
  247.                     hex2bin)
  248.                     hex2bin
  249.                     funct $*
  250.                     ;;
  251.                     bin2oct)
  252.                     bin2oct
  253.                     funct $*
  254.                     ;;
  255.                     bin2dec)
  256.                     bin2dec
  257.                     funct $*
  258.                     ;;
  259.                     bin2hex)
  260.                     bin2hex
  261.                     funct $*
  262.                     ;;
  263.                     dec2hex)
  264.                     dec2hex
  265.                     funct $*
  266.                     ;;
  267.                     dec2oct)
  268.                     dec2oct
  269.                     funct $*
  270.                     ;;
  271.                     dec2bin)
  272.                     dec2bin
  273.                     funct $*
  274.                     ;;
  275.                     oct2hex)
  276.                     oct2hex
  277.                     funct $*
  278.                     ;;
  279.                     oct2dec)
  280.                     oct2dec
  281.                     funct $*
  282.                     ;;
  283.                     oct2bin)
  284.                     oct2bin
  285.                     funct $*
  286.                     ;;
  287. esac
  288. #
  289. # now we display the formated calculated equation or conversion as applies.
  290. $RE "$ANSWER"

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will not expire by default. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right worth-right