SUMMARY: Arithmetic expressions in /bin/sh

From: Ian Roddis <roddis_at_nortelnetworks.com>
Date: Tue Jun 19 2001 - 14:47:37 EDT
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C0F8F0.4F689F70
Content-Type: text/plain;
	charset="iso-8859-1"

Hi again,

  The overwhelming response was to go with ksh instead of sh ( with quite a
bit of poking at other OSs for non-standard sh implementations :) ). Just
for fun, though, I persevered. Taking advantage of the fact that I was
dealing with known quantities ( IP addresses ), I came up with the
following.

_NETWORK=`echo $_IP_ADDRESS $_NETMASK | sed -e 's/\./ /g' | awk '{
        network = ( $1  - ( $1 % ( 255 - $5 + 1 ) ) )
        for ( i = 2 ; i <= 4 ; i++ ) network = network "." ( $i - ( $i % (
255 - $( i + 4 ) + 1 ) ) )
        print network
}'`

  This works for all netmasks when applied to each octet. ( Even odd ones
like 255.255.236.0 ).
  This solution seems cleaner ( at least easier to read ) than an equivalent
solution in sh or ksh. I didn't try using arrays in ksh, so it might be
easier.

Thanks muchly to all who responded.

  -Ian


Original Question:

Hi,

  I need to do bitwise operations within a shell script to determine
netmasks for a range of IPs ( not interfaces on the current box ). I can't
use perl for this ( this will be run on 1500+ nodes, not all configured the
same way ). In every other implementation of /bin/sh I've used you could do
something like this:
	echo $(( numberX & numberY ))
  This doesn't work in Solaris. Is there any way to do this without
downloading extra tools?

Thanks,
  Ian

------_=_NextPart_001_01C0F8F0.4F689F70
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2654.59">
<TITLE>SUMMARY: Arithmetic expressions in /bin/sh</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>Hi again,</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; The overwhelming response was to go with ksh =
instead of sh ( with quite a bit of poking at other OSs for =
non-standard sh implementations :) ). Just for fun, though, I =
persevered. Taking advantage of the fact that I was dealing with known =
quantities ( IP addresses ), I came up with the following.</FONT></P>

<P><FONT SIZE=3D2>_NETWORK=3D`echo $_IP_ADDRESS $_NETMASK | sed -e =
's/\./ /g' | awk '{</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; network =
=3D ( $1&nbsp; - ( $1 % ( 255 - $5 + 1 ) ) )</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( i =
=3D 2 ; i &lt;=3D 4 ; i++ ) network =3D network &quot;.&quot; ( $i - ( =
$i % ( 255 - $( i + 4 ) + 1 ) ) )</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print =
network</FONT>
<BR><FONT SIZE=3D2>}'`</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; This works for all netmasks when applied to =
each octet. ( Even odd ones like 255.255.236.0 ).</FONT>
<BR><FONT SIZE=3D2>&nbsp; This solution seems cleaner ( at least easier =
to read ) than an equivalent solution in sh or ksh. I didn't try using =
arrays in ksh, so it might be easier.</FONT></P>

<P><FONT SIZE=3D2>Thanks muchly to all who responded.</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; -Ian</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>Original Question:</FONT>
</P>

<P><FONT SIZE=3D2>Hi,</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; I need to do bitwise operations within a shell =
script to determine netmasks for a range of IPs ( not interfaces on the =
current box ). I can't use perl for this ( this will be run on 1500+ =
nodes, not all configured the same way ). In every other implementation =
of /bin/sh I've used you could do something like this:</FONT></P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>echo $(( =
numberX &amp; numberY ))</FONT>
<BR><FONT SIZE=3D2>&nbsp; This doesn't work in Solaris. Is there any =
way to do this without downloading extra tools?</FONT>
</P>

<P><FONT SIZE=3D2>Thanks,</FONT>
<BR><FONT SIZE=3D2>&nbsp; Ian</FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C0F8F0.4F689F70--
Received on Tue Jun 19 19:47:37 2001

This archive was generated by hypermail 2.1.8 : Wed Mar 23 2016 - 16:24:57 EDT