Tiny Core Linux
Tiny Core Base => TCB Tips & Tricks => Topic started by: MakodFilu on March 26, 2010, 08:13:10 PM
-
As a one-liner, I thought of it more as a trick rather than a script.
#!/bin/sh
echo '$*'|awk '{print "'$*'="'$*'}'
tc@box:~$ awkcalc 2^10
2^10=1024
tc@box:~$ awkcalc 2**10
2**10=1024
tc@box:~$ awkcalc 2*10
2*10=20
tc@box:~$ awkcalc '(1+1/2^20)^(2^20)'
(1+1/2^20)^(2^20)=2.71828
Note the quotations in that last call.
-
Cool! Yeah awk!
-
tc@box:~$ echo $((1+1))
2
-
sh math :D
#!/bin/sh
echo $@ = $(($@))
http://www.linuxconfig.org/Bash_scripting_Tutorial#17-arithmetic-operations (http://www.linuxconfig.org/Bash_scripting_Tutorial#17-arithmetic-operations)
Note: ash treats ^ as xor, not as exp
-
The way it is done in sh is via ** operator.
Anyway, here is more syntactic sugar:
http://www.grymoire.com/Unix/Awk.html#uh-34
tc@box:~$ awkcalc "sqrt(2)"
sqrt(2)=1.41421
tc@box:~$ awkcalc "sqrt(2)"
sqrt(2)=1.41421
tc@box:~$ awkcalc "2**(1/2)"
2**(1/2)=1.41421
tc@box:~$ awkcalc "2^(1/2)"
2^(1/2)=1.41421
tc@box:~$ awkcalc "20%8"
20%8=4
I just decided galculator.tcz had way too much features that I absolutely didn't need (:
-
BTW, most pages out there use bc. bc is not in Base Core, although dc is.
Anyway, I was just longing the times Tom Oehser (http://www.toms.net/perspage.htm) had not even thought about rewriting awk scripts within TomsRtBt (http://www.toms.net/rb/) as lua 4.0 (http://www.lua.org/versions.html) scripts.