You are not logged in.

#1 2015-06-26 06:45:44

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,390

[SOLVED] commandline calculator

With this in ~/.bashrc

= () { echo "$*" | bc -l ; }

I can simply type:

# = 3/4
.75000000000000000000

...which is handy, but if i want to use parentheses, i must enclose the whole expression by double quotes (or quote parentheses)
Is there a smart way to avoid it?

# = "((3/4)+1)/2"
.87500000000000000000
= ((3/4)+1)/2 
bash: syntax error near unexpected token `('

thanks

Last edited by kokoko3k (2015-06-29 14:40:10)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#2 2015-06-26 15:48:32

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] commandline calculator

backslash quote the parentheses

ewaller@turing ~ 1076 %= \(1 + 2\)/3
1.00000000000000000000
ewaller@turing ~ 1077 %

Or, use dc instead of bc and enter the equation using RPN tongue

Last edited by ewaller (2015-06-26 15:50:36)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2015-06-26 17:38:26

teckk
Member
Registered: 2013-02-21
Posts: 518

Re: [SOLVED] commandline calculator

Also have a look at extra/genius

genius> 3/4
= 3/4
genius> 3/4.0
= 0.75
genius> 3/4 + 1/2
= 1 1/4
genius> 3/4 + 1/2.0
= 1.25
genius> ((3/4)+1)/2
= 7/8
genius> ((3/4)+1.0)/2
= 0.875

Offline

#4 2015-06-26 18:38:03

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,390

Re: [SOLVED] commandline calculator

Thanks, genius is nice, but i'd like to use something lighter and not interactive.
Ewaller, i prefer the double quotes to RPN smile


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#5 2015-06-27 15:42:46

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

Re: [SOLVED] commandline calculator

You can't avoid the escaping because you're asking the shell to interpret your text before it's passed to the function. If you instead:

=() { bc -ql; }

Then you can...

$ =
((3/4)+1)/2
.87500000000000000000
3/4
.75000000000000000000

Requires a few extra keystrokes (including ^D to leave bc), but you can avoid the need for escaping. Perhaps you can go one step further...

=() {
  if (( $# )); then
    echo "$*" | bc -l
  else
    bc -ql
  fi
}

Now you get interactivity if you call '=' without args and accept the pitfalls, or you just type '=' and get an interactive calculator.

Offline

#6 2015-06-29 14:39:57

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,390

Re: [SOLVED] commandline calculator

Yes yes, understood.
Marking as solved, thank you all.


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#7 2015-06-29 14:45:56

esa
Member
Registered: 2011-12-29
Posts: 143
Website

Re: [SOLVED] commandline calculator

I like awk to do math:

echo 15 4 5 | awk '{print ($1 / $2 * $3)}'

Author of: TUI (Text User Interface for scripts), VHS (Video Handler Script, using ffmpeg) and YASSI (Yet Another Simple Script Installer)

Offline

#8 2015-06-30 12:47:53

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] commandline calculator

Check this out, using bash's bind -x (so source it or put it in .bashrc):

bind -x '"=": if [[ "$READLINE_LINE" == =* ]]; then
	echo -e "$(bc <<< "${READLINE_LINE:1}" 2>&1) \e[33m(${READLINE_LINE:1})\e[m"
else
	READLINE_LINE=${READLINE_LINE:: $READLINE_POINT}=${READLINE_LINE: $READLINE_POINT}
	((READLINE_POINT++))
fi'

Type = and then an equation and then =. The equation stays on the prompt so you can edit it further and you can type = anywhere on the line.

72 (9*8)
288 (9*8*2*2)
144 (9*8*2*1)
(standard_in) 1: syntax error (9*8*2*1 4)
█ /1/bin █ =9*8*2*1 4

Last edited by Procyon (2015-07-13 17:30:41)

Offline

#9 2015-07-03 17:24:23

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,390

Re: [SOLVED] commandline calculator

Very nice Procyon, now it is just a matter of taste smile


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#10 2015-07-03 19:06:04

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] commandline calculator

You may also like these two posts.  That is if you want to try interrobang as a calculator (but note the syntax of the config file has changed since then).


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB