> > I have (to practice awk programming) to create a simple calculator
> > with awk
> > here is my code:
> > command : echo "34 * 2" | bc-l.sh
> > script :
> > read a
> > echo $a | awk '
> > {
> > { if($2=="+") { sum = $1+=$3; print sum;} }
> > { if($2=="-") { sum = $1-=$3; print sum;} }
> > { if($2=="/") { sum = $1/=$3; print sum;} }
> > { if($2=="*") { sum = $1*=$3; print sum;} }
> > }'
> > It works well except with multiplication. The * character seems to be
> > interpreted like "joker" not "multiplication" How could I fix it?
> Quote the variable so * isn't expanded.
> Ben
I quoted the $a instead and it works.
------- Sample Output -------
Script started on Mon May 6 12:48:53 2002
[ /students/btam01/tmp ]
cat calc
read a
echo "$a" | awk '
{
{ if ( $2 == "+" ) { sum = $1 + $3; print sum;} }
{ if ( $2 == "-" ) { sum = $1 - $3; print sum;} }
{ if ( $2 == "/" ) { sum = $1 / $3; print sum;} }
{ if ( $2 == "*" ) { sum = $1 * $3; print sum;} }
Quote:}'
[ /students/btam01/tmp ]
echo "3 * 3" | calc
9
[ /students/btam01/tmp ]
script done on Mon May 6 12:49:09 2002
-Bill