You are not logged in.

#1 2009-11-11 14:20:42

virtuemood
Member
Registered: 2009-04-04
Posts: 33

share : enable bash to do some simple calculattions

just add this line into  ~/.bashrc

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

ex:
$ calc "(1*2)+4-(5-6)"
7

or
$ calc 128*3
384

Offline

#2 2009-11-11 14:41:39

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: share : enable bash to do some simple calculattions

Or using just bash:

calc(){ echo $(( $1 )); };

archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#3 2009-11-11 15:51:45

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

Re: share : enable bash to do some simple calculattions

there's also bc for decimal math.

calc() { echo "scale=3; $*" | bc ; }
//blue/0/~/ calc 5/3
1.666

or even

//blue/0/~/ calc 'r=5; pi=3.14; pi*r^2'
78.50

Offline

#4 2009-11-11 15:58:46

apaige
Member
Registered: 2008-06-15
Posts: 96

Re: share : enable bash to do some simple calculattions

Being integer only, bash truncates results from divisions. For example, 16 divided by 9 (1.777…) results in 1. If you want to get rounded results, multiply the left operand by ten, add 5 to the result and divide that by 10.

[user@localhost ~]# function divide() { echo $(( ($1 * 10 / $2 + 5) / 10 )); }
[user@localhost ~]# echo $(( 16 / 9 ))
1
[user@localhost ~]# divide 16 9
2
[user@localhost ~]# echo $(( 4 / 3 ))
1
[user@localhost ~]# divide 4 3
1

Last edited by apaige (2009-11-11 18:00:38)

Offline

#5 2009-11-11 17:23:39

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 834
Website

Re: share : enable bash to do some simple calculattions

Another bc liner

% alias calc='bc -l <<<'
% calc 'r=5; pi=3.14; pi*r^2'
78.50

You need to install an RTFM interface.

Offline

Board footer

Powered by FluxBB