You are not logged in.
Pages: 1
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
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
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
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
Thanks, genius is nice, but i'd like to use something lighter and not interactive.
Ewaller, i prefer the double quotes to RPN
Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !
Offline
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
Yes yes, understood.
Marking as solved, thank you all.
Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !
Offline
Offline
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
Very nice Procyon, now it is just a matter of taste
Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !
Offline
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
Pages: 1