bourne shell query

Dan Mercer mercer at npdiss1.StPaul.NCR.COM
Sat Sep 1 05:25:34 AEST 1990


In article <8465 at orca.wv.tek.com> jeff at quark.WV.TEK.COM (Jeff Beadles) writes:
:scott at tab00.larc.nasa.gov (Scott Yelich) writes:
:
:>As a side question, does ANYONE have any bourne shell routines which do
:>math... reasonably effeciently?  (For numbers > 1000?)
:
:Well, I use expr.  For example:
:
:a=123456
:b=123
:
:c=`expr $a - $b`
:
:d=`expr $a \* $b`
:
:echo "c = $c"
:echo "d = $d"
:
:Note, that the '*' MUST be quoted, as the shell will expand it.
:
:"Shell qouting --  Know it, live it, be it.  -Jeff"    :-)
:
:	-Jeff
:-- 
:Jeff Beadles				jeff at quark.WV.TEK.COM 
:Utek Engineering, Tektronix Inc.	+1 503 685 2568
:			SPEEA - Just say no.


If your system has named pipes - fifos - and bc then you may find this
useful:


# 
# these instructions illustrate how to set up a background
# engine in a shell script - a simple application is shown only
# for illustration purposes - it numbers lines of standard input

# first allocate fifos

ITMP=/usr/tmp/i.$LOGNAME
OTMP=/usr/tmp/o.$LOGNAME

/bin/rm -f $ITMP $OTMP 2>/dev/null
/etc/mknod $ITMP p  # input fifo for engine
/etc/mknod $OTMP p  # output fifo for engine

# start background engine and open $ITMP as file descriptor 3
# and $OTMP as file descriptor 4

bc <$ITMP >$OTMP &
exec 3>$ITMP
exec 4<$OTMP

# send command to background engine
# if background engine is bc,  counters can be maintained
# echo "x = 1"
# echo "x = x + 1" or echo "++x"
#the second version returns the new value of x

echo "x = 0" >&3

while line=`line`
	do
	echo "x++" >&3
	# read response
	read ans <&4
	echo "$ans: $line"
	done < engine

# terminate background engine and remove fifos

echo "quit" >&3
/bin/rm -f $ITMP $OTMP

exit 0
-- 

Dan Mercer
Reply-To: mercer at npdiss1.StPaul.NCR.COM (Dan Mercer)
"MAN - the only one word oxymoron in the English Language"



More information about the Comp.unix.shell mailing list