You are not logged in.

#1 2010-07-21 23:07:18

kmason
Member
From: Tempe, Arizona, USA
Registered: 2010-03-23
Posts: 256
Website

CLiC - A very simple command line calculator

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

#2 2010-07-22 03:59:32

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: CLiC - A very simple command line calculator

I use this in my .bashrc

calc () { awk "BEGIN { print $* }" }

Offline

#3 2010-07-22 04:38:33

kmason
Member
From: Tempe, Arizona, USA
Registered: 2010-03-23
Posts: 256
Website

Re: CLiC - A very simple command line calculator

steve___ wrote:

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

#4 2010-07-22 06:32:51

Husio
Member
From: Europe
Registered: 2005-12-04
Posts: 359
Website

Re: CLiC - A very simple command line calculator

# calc() { echo $(( $@ )) }
# calc 2 + 3
5
# calc 6 / 3
2
# calc '6 * 3'
18

Offline

#5 2010-07-22 12:24:10

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: CLiC - A very simple command line calculator

@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

#6 2010-07-22 13:34:06

Stalafin
Member
From: Berlin, Germany
Registered: 2007-10-26
Posts: 617

Re: CLiC - A very simple command line calculator

calc() { echo "$@" | bc -l ; }

Awesome, too...

Offline

#7 2010-07-22 13:57:01

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: CLiC - A very simple command line calculator

#!/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

#8 2010-07-22 14:05:41

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: CLiC - A very simple command line calculator

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

Offline

#9 2010-07-31 15:27:36

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: CLiC - A very simple command line calculator

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

#10 2010-08-02 07:30:46

quigybo
Member
Registered: 2009-01-15
Posts: 223

Re: CLiC - A very simple command line calculator

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 wink. And I already have readline completion in my $PYTHONSTARTUP so I'm a happy camper.

Offline

#11 2010-08-02 07:40:21

Ari'osika
Member
From: Your computer, okay?
Registered: 2010-06-22
Posts: 175

Re: CLiC - A very simple command line calculator

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

smile

Last edited by Ari'osika (2010-08-02 07:56:11)


If you're reading this; you're awesome.

Offline

#12 2010-08-02 07:54:03

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

Re: CLiC - A very simple command line calculator

"The official calculator thread" ?
wink

Offline

#13 2010-08-02 08:31:24

Stalafin
Member
From: Berlin, Germany
Registered: 2007-10-26
Posts: 617

Re: CLiC - A very simple command line calculator

brisbin33 wrote:
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

#14 2010-08-02 09:21:29

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: CLiC - A very simple command line calculator

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

smile

perl -E 'say eval while<>'

Offline

#15 2010-08-02 09:46:26

Ari'osika
Member
From: Your computer, okay?
Registered: 2010-06-22
Posts: 175

Re: CLiC - A very simple command line calculator

dmz wrote:
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

smile

perl -E 'say eval while<>'

Touche, I forgot about the -E switch. big_smile


If you're reading this; you're awesome.

Offline

#16 2010-08-02 11:12:42

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: CLiC - A very simple command line calculator

No reverse polish? I'll chip in.

Offline

#17 2010-08-03 03:08:48

kmason
Member
From: Tempe, Arizona, USA
Registered: 2010-03-23
Posts: 256
Website

Re: CLiC - A very simple command line calculator

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

Board footer

Powered by FluxBB