You are not logged in.

#1 2009-04-11 07:29:56

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Creating Basic Calculator Script - Needs help

I'd like to create a basic calculator script that makes doing calculations a bit easier from the command line.  Normally i do:

echo "32*24" | bc

So I built a bash script that I can just type "calc 32*24":

bc << EOF
scale=4
$@
quit
EOF

And it works ok until i try to use parenthesis:

calc (1+49)/5
bash: syntax error near unexpected token `1+49'

if I use parenthesis, directly in the bc command it does ok:

bc
bc 1.06...
(1+49)/5
10

I've tried putting $@ in quotes but not luck.  Looks like bash isn't transferring the parenthesis.  How can I get the bash script to pass theparenthesis?


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#2 2009-04-11 14:04:42

carlocci
Member
From: Padova - Italy
Registered: 2008-02-12
Posts: 368

Re: Creating Basic Calculator Script - Needs help

you have to quote your expression.
AFAIK, bash expands "(stuff)" in a subshell: this means the problem is not in your script, but in the line you invoke your script with.

Offline

#3 2009-04-11 14:08:07

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Creating Basic Calculator Script - Needs help

This is why I always have at least one terminal in an IPython session.

Offline

#4 2009-04-12 04:09:43

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Creating Basic Calculator Script - Needs help

Dang, answer was easier than i thought.  Would be nice to be able to have everything in the script but it's easy just to do quotes from the command line.  Thanks.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#5 2009-04-12 04:17:03

mikesd
Member
From: Australia
Registered: 2008-02-01
Posts: 788
Website

Re: Creating Basic Calculator Script - Needs help

Dusty wrote:

This is why I always have at least one terminal in an IPython session.

+1!

I normally use a standard python instance in the same way but have just installed ipython and am having a look at it now. Looks cool!

Offline

#6 2009-04-12 05:33:36

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Creating Basic Calculator Script - Needs help

Whats ipython got to do with my calc script? What planet r u from?  I thought they spoke English down there.  lol

1+!!

Last edited by Gen2ly (2009-04-12 05:36:07)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

Board footer

Powered by FluxBB