Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arithmetic with shell using bc or octave 1

Status
Not open for further replies.

liondari

Technical User
Oct 4, 2004
7
0
0
GR
Hello programmers,

I would like to use the calculator bc or octave in shell scripts. I wrote a short shell script "calc" with this
content:

echo "$1" | octave -q | awk '{print $3}'

It's nice, you can do the following, e.g.:

./calc "sin(23),1/3,4*4"

The output is:

-0.84622
0.33333
16

Is it possible to get rid of the double quotes and the
commata in the input? So, I would like to use it like this:

./calc sin(23) 1/3 4*4

Thank you all. Hope its not too difficult.


 
Hi

liondari said:
Is it possible to get rid of the double quotes and the commata in the input?
Maybe. Maybe.

Maybe you can get rid of the quotes ( " ). It depends on your shell and expression. But your example may cause problem in most shells because they will try to perform file name expansion on the asterisk ( * ). The parenthesis ( () ) will also cause problem in many shells because they usually are control operators. However, alternative exists :
Code:
./calc sin[red]\[/red](23[red]\[/red]),1/3,4[red]\[/red]*4
Maybe you can get rid of the commas ( , ). It depends on the [tt]octave[/tt]'s syntax, which I do not know. However, alternative exists :
Code:
echo "$[red]*[/red]" | [red]tr ' ' ',' |[/red] octave -q | awk '{print $3}'


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top