[Gllug] Unsigned shell arithmetic

tet at accucard.com tet at accucard.com
Wed Dec 5 09:16:38 UTC 2001


>Not a chance. The Bourne shell isn't a POSIX shell and hence doesn't
>have shell arithmetic at all.

So it seems. I was sure all the major vendors had been through an updated
their shells to make them mostly POSIX compliant a few years ago, but a
bit of empirical testings shows this not to be the case (at least for
Solaris and Tru64).

>You'll have to either write C or implement bitwise arithmetic in dc or
>sed in terms of decimal manipulations. This is definitely possible ---
>see the seders mailing list archives for an implementation by (who else)
>Greg Ubben --- but it's, er, hardly readable.

Actually, I was on the verge of doing it myself (using dc to convert
to binary, and then parsing the result bit by bit and comparing to
the other number). But then I realised I could do it in Korn shell,
and it worked for the major platform I'm interested in, so I went
with that instead...

FWIW, the particular application I have for this is trying to calculate
a network address given an IP address and a netmask. I'm converting the
dotted quads to a single 32-bit integer (using cut and dc), then ANDing
them together, before breaking the result back out into dotted quads.

>I think you'll need /bin/ksh, but that's always there, isn't it?

Yes, and that's what I've ended up using. And if it's not there, then
they're out of luck :-)

Tet

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list