I am pretty often missing the calculator. Of course, there are xcalc and kcalc for graphical interface, but that’s just too slow for something that I should actually do in my head. There is bc(1) for console, but I never liked it for quick stuff. expr(1) is pretty close, but it needs a rather complex syntax like “expr 1 ‘+’ 2”.
So here I am with a need for simple console calculator. Here is what I do – write a small perl script:
#!/usr/bin/perl -w
use strict;
my $expr = shift;
my $verbose = $expr eq '-v' ? 1 : 0;
$expr = shift if ($verbose);
do {
my $result = eval($expr);
print $expr . ' = ' if ($verbose);
print eval($expr) . "
";
} while ( $expr = shift);
This can be than placed into any directory in the path and used as following:
[leonid@home tmp]$ calc.pl "2+3"
5
The beauty of it is that it can do several equations at once and not only equations:
[leonid@home tmp]$ KOEF=`calc.pl '10 + 5'`
[leonid@home tmp]$ calc.pl -v "$KOEF * 2" "sqrt(256)" "localtime(time)"
15 * 2 = 30
sqrt(256) = 16
localtime(time) = Thu Feb 26 04:28:25 2004
This ‘-v’ option causes calc.pl to print out the equation as well. Now I can totally forget arithmetics. :)