You are not logged in.
CLiC, or Command Line Calculator
Okay, I know, there's probably a ton of command line calculators out there, but I figured I'd post the one I just made anyways, even if it is done very amateurishly IMO.
Its 51 SLOC, so its probably pretty light, even though it is just a calculator. It actually uses AWK for the calculations, which I thought was pretty interesting.
Last edited by kmason (2010-07-21 23:15:41)
Offline
I use this in my .bashrc
calc () { awk "BEGIN { print $* }" }
Offline
I use this in my .bashrc
calc () { awk "BEGIN { print $* }" }
Oh geesh... That's amazing how something I made so complex can be so simple...
Thanks.
Offline
# calc() { echo $(( $@ )) }
# calc 2 + 3
5
# calc 6 / 3
2
# calc '6 * 3'
18
Offline
@Husio
# booga() { echo $(( $@ )) ; }
$ booga 1+2
3
# booga 1.2+2.3
bash: 1.2+2.3 : syntax error: invalid arithmetic operator (error token is ".2+2.3 ")
Bash does not support floating point.
Offline
calc() { echo "$@" | bc -l ; }
Awesome, too...
Offline
#!/usr/bin/perl
use strict;
# calc
print "Expression?\n";
while (1) {
print '> ';
chomp(my $c = <STDIN>);
exit(0) if(($c =~ /^\s*:q\s*/));
my $i = eval($c);
printf("%d 0x%x 0%o %b %f %s \n", $i, $i, $i, $i, $i, $i);
}
Offline
calc() { echo "$@" | bc -l ; }
Awesome, too...
throw a "scale=3; " in front of the $@ to do decimals too.
bc is a full fledge math language so you can get pretty crazy with your little calc if you know what you're doing.
//blue/0/~/ pi=$(calc '4*a(1)')
//blue/0/~/ echo $pi
3.140
//github/
Offline
I like this R-based calculator, even though it might be slightly slower than bc.
# Calc based on R
# http://www.compbiome.com/2010/06/r-command-line-calculator-using-rscript.html
alias Calc='Rscript -e "cat( file=stdout(), eval( parse( text=paste( commandArgs(TRUE), collapse=\"\"))),\"\n\")"'
Arch x64 on Thinkpad X200s/W530
Offline
I also use awk's print $* for simple stuff, for anything more complicated I just run a python shell with from math import *. Variable assignment, functions, loops, what more could you need . And I already have readline completion in my $PYTHONSTARTUP so I'm a happy camper.
Offline
perl -e 'print 30+12,"\n"';
42
perl -e 'print 50-8,"\n"';
42
perl -e 'print 21*2,"\n"';
42
perl -e 'print 84/2,"\n"';
42
Last edited by Ari'osika (2010-08-02 07:56:11)
If you're reading this; you're awesome.
Offline
Offline
Stalafin wrote:calc() { echo "$@" | bc -l ; }
Awesome, too...
throw a "scale=3; " in front of the $@ to do decimals too.
bc is a full fledge math language so you can get pretty crazy with your little calc if you know what you're doing.
//blue/0/~/ pi=$(calc '4*a(1)') //blue/0/~/ echo $pi 3.140
Indeed; I just assumed one would call calc with scale=xx...
Just checked it - if one calls bc like so
echo "scale=3;scale=5; 4*a(a)" | bc -l
The last scale= setting will override the previous one.
Offline
perl -e 'print 30+12,"\n"'; 42 perl -e 'print 50-8,"\n"'; 42 perl -e 'print 21*2,"\n"'; 42 perl -e 'print 84/2,"\n"'; 42
perl -E 'say eval while<>'
Offline
Ari'osika wrote:perl -e 'print 30+12,"\n"'; 42 perl -e 'print 50-8,"\n"'; 42 perl -e 'print 21*2,"\n"'; 42 perl -e 'print 84/2,"\n"'; 42
perl -E 'say eval while<>'
Touche, I forgot about the -E switch.
If you're reading this; you're awesome.
Offline
Thanks to all of you guys' suggestions, I modified my script, into 10 SLOC (including comments and the header):
#!/bin/bash
# CLIC - Command Line Interactive Calculator
while true; do
read -p "Expression?: "
if [[ "$REPLY" = "" ]]; then
exit
else
scale=3 echo $REPLY | bc -l ;
fi
done
A minor detail you might have figured out: I replaced the lower case "i" in the name with an uppercase, for Interactive.
Last edited by kmason (2010-08-03 03:09:29)
Offline