WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: awkcalc.sh  (Read 6836 times)

Offline MakodFilu

  • Newbie
  • *
  • Posts: 46
awkcalc.sh
« on: March 26, 2010, 08:13:10 PM »
As a one-liner, I thought of it more as a trick rather than a script.

Code: [Select]
#!/bin/sh

echo '$*'|awk '{print "'$*'="'$*'}'

Code: [Select]
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.
« Last Edit: March 26, 2010, 08:17:52 PM by MakodFilu »

Offline roberts

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 7361
  • Founder Emeritus
Re: awkcalc.sh
« Reply #1 on: March 27, 2010, 01:01:53 AM »
Cool! Yeah awk!
10+ Years Contributing to Linux Open Source Projects.

Offline jls

  • Hero Member
  • *****
  • Posts: 2135
Re: awkcalc.sh
« Reply #2 on: March 27, 2010, 12:57:49 PM »
Code: [Select]
tc@box:~$ echo $((1+1))
2
dCore user

Offline robc

  • Sr. Member
  • ****
  • Posts: 447
Re: awkcalc.sh
« Reply #3 on: March 27, 2010, 01:41:02 PM »
sh math  :D
Code: [Select]
#!/bin/sh
echo $@ = $(($@))
http://www.linuxconfig.org/Bash_scripting_Tutorial#17-arithmetic-operations

Note: ash treats ^ as xor, not as exp
"Never give up! Never surrender!" - Commander Peter Quincy Taggart

"Make it so." - Captain Picard

Offline MakodFilu

  • Newbie
  • *
  • Posts: 46
Re: awkcalc.sh
« Reply #4 on: March 27, 2010, 02:05:24 PM »
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

Code: [Select]
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 (:

Offline MakodFilu

  • Newbie
  • *
  • Posts: 46
Re: awkcalc.sh
« Reply #5 on: March 27, 2010, 02:19:02 PM »
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 had not even thought about rewriting awk scripts within TomsRtBt as lua 4.0 scripts.
« Last Edit: March 27, 2010, 02:25:20 PM by MakodFilu »