Advertising
- Unnamed
- Tuesday, May 8th, 2007 at 9:41:25am UTC
- #!/bin/sh
- #
- # simple mathematics calculator for the GNU Linux system. uses "bc"
- # all code is GNU-GPL free software.
- #
- VERSION=0.2.3
- BN="$(basename $0)"
- DN="$(dirname $0)"
- [ -f /bin/echo ] && RE="/bin/echo -e" || RE="echo -e" # kludge
- #
- # if nothing is specified on the command line then we read stdin
- if [ -z "$1" ]
- then JOE="`while read P ;do $RE $P ;done`"
- $0 $JOE
- exit $?
- fi
- #
- # if the user asked for help we display a help page of sorts.
- dohelp() {
- case $BN in
- math*)
- $RE "
- syntax; math <math_expression>
- if called as any one of the many base2base functions, then simply
- convert the number from the input base to the output base.
- otherwise, a normal math computation of 'string = ?' is done.
- and the following switch applies.
- -s silent mode regular calculations only the answer is outputted.
- only for the normal \"math\" function.
- -h this help message and of curse the help page is only one page
- for all functions.
- --install lets you use the version suffixed script as an installer
- for the symlinks that it will then use as function calls.
- NOTE: you should put $0 in $HOME/bin for a
- user installation, or system wide installs should be done by
- root, and put in /usr/local/bin! simply copy this script to
- the correct dir and run: math-$VERSION --install from there.
- "
- ;;
- #
- *)
- $RE "
- syntax; $BN <number>
- example: $BN 111
- yields:$($0 111)
- simply converts the number from the input base to the output base.
- available conversions are; hex2dec hex2oct hex2bin dec2hex dec2oct
- dec2bin oct2hex oct2dec oct2bin bin2hex bin2dec bin2oct
- -h show this help message.
- "
- ;;
- esac
- exit 0
- }
- #
- case $1 in
- -h)
- dohelp
- ;;
- --help)
- dohelp
- ;;
- esac
- #
- # from this point on, unless you do 'bash' shell scripting, you probably
- # wont be able to follow much of what is happening, or at lease why it is.
- # but you are more than welcome to try :)
- #
- # the normal math function for both silent and noisy modes
- math() {
- if [ "-s" = "$1" ]
- then shift
- $RE "$*" | bc -l 2> /dev/null
- exit 0
- fi
- ANSWER="$* = $($RE "$*" | bc -l 2>/dev/null)"
- }
- #
- # the install function for a first time setup or reinstall.
- INSTALL() {
- MESS=' installing in $D,
- and moving original script to $D/.scripts '
- LST='hex2dec hex2oct hex2bin dec2hex dec2oct dec2bin oct2hex oct2dec oct2bin bin2hex bin2dec bin2oct'
- WORK() {
- mkdir -p "$D/.scripts" || { $RE "E: permission denied!" ;exit 1 ; }
- cp $0 "$D/.scripts" || { $RE "E: permission denied!" ;exit 1 ; }
- $RE "copied \"$0\" to \"$D/.scripts/$BN\" "
- ln -s ".scripts/$BN" "$D/math" || { $RE "E: permission denied!" ;exit 1 ; }
- $RE "created. \"$D/math\" symlink"
- for Q in $LST
- do ln -s math "$D/$Q" && $RE "created. \"$D/$Q\" symlink"
- done
- [ -h "$D/math" ] &&\
- [ ! "`find "$D/" -maxdepth 1 -name $BN`" = "`find "$DN/" -maxdepth 1 -name
- $BN`" ] &&\
- CLEANUP="rm $0" || { $RE "E: can not rm \"$0\" !" ;exit 1 ; }
- $RE "$BN installed."
- $RE "$PATH" | grep -v "$D" &&\
- $RE "\"$D\" not in your path. you should fix that."
- }
- $0 --help
- default="$( [ "$UID" = "0" ] && $RE "/usr/local/bin" || $RE "$HOME/bin" )"
- $RE "Please provide the path to install into,\c"
- read -p " [$default]: " TOM
- $RE "\n"
- D=${TOM:=$default}
- $RE "$MESS"
- WORK &&\
- read -n 1 -p "is it ok to remove the origenal file [Y,n] ? " X
- $RE "\n"
- [ ! "$X" = "y" -a ! "$X" = "Y" ] && exit 0 || $CLEANUP
- exit 0
- }
- #
- # the install switch lets you use the version suffixed script as an
- # installer for the symlinks that it will then use as function calls
- if [ "x--install" = "x$1" ]
- then INSTALL
- fi
- #
- # the conversion function which allows all conversion methods
- funct() {
- if [ "$1" = "-s" ] >/dev/null 2>&1
- then shift
- for Q in $*
- do Q=`$RE "$Q" | tr "[a-f]" "[A-F]"`
- ANSWER="$($RE "obase=$TT ;ibase=$FF ;$Q" | bc -l )"
- done
- do Q=`$RE "$Q" | tr "[a-f]" "[A-F]"`
- ANSWER="$ANSWER \n $Q$F = $($RE "obase=$TT ;ibase=$FF ;$Q" | bc-l )$T"
- done
- fi
- }
- #
- # the rest of these functions only set the formating for the 'funct' call
- hex2dec() {
- T=''
- TT='10'
- F='_16'
- FF='16'
- }
- #
- hex2oct() {
- T='_8'
- TT='8'
- F='_16'
- FF='16'
- }
- #
- hex2bin() {
- T='_2'
- TT='2'
- F='_16'
- FF='16'
- }
- #
- dec2hex() {
- T='_16'
- TT='16'
- F=''
- FF='10'
- }
- #
- dec2oct() {
- T='_8'
- TT='8'
- F=''
- FF='10'
- }
- #
- dec2bin() {
- T='_2'
- TT='2'
- F=''
- FF='10'
- }
- #
- oct2hex() {
- T='_16'
- TT='16'
- F='_8'
- FF='8'
- }
- #
- oct2dec() {
- T=''
- TT='10'
- F='_8'
- FF='8'
- }
- #
- oct2bin() {
- T='_2'
- TT='2'
- F='_8'
- FF='8'
- }
- #
- bin2hex() {
- T='_16'
- TT='16'
- F='_2'
- FF='2'
- }
- #
- bin2dec() {
- T=''
- TT='10'
- F='_2'
- FF='2'
- }
- #
- bin2oct() {
- T='_8'
- TT='8'
- F='_2'
- FF='2'
- }
- #
- # this loop checks to see what the user was asking for by checking the
- # symlink name and acting accordingly.
- case $BN in
- math)
- math $*
- ;;
- hex2dec)
- hex2dec
- funct $*
- ;;
- hex2oct)
- hex2oct
- funct $*
- ;;
- hex2bin)
- hex2bin
- funct $*
- ;;
- bin2oct)
- bin2oct
- funct $*
- ;;
- bin2dec)
- bin2dec
- funct $*
- ;;
- bin2hex)
- bin2hex
- funct $*
- ;;
- dec2hex)
- dec2hex
- funct $*
- ;;
- dec2oct)
- dec2oct
- funct $*
- ;;
- dec2bin)
- dec2bin
- funct $*
- ;;
- oct2hex)
- oct2hex
- funct $*
- ;;
- oct2dec)
- oct2dec
- funct $*
- ;;
- oct2bin)
- oct2bin
- funct $*
- ;;
- esac
- #
- # now we display the formated calculated equation or conversion as applies.
- $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.
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.