[Gllug] Unsigned shell arithmetic

Nix nix at esperi.demon.co.uk
Wed Dec 5 23:05:13 UTC 2001


On Wed, 05 Dec 2001, tet at accucard.com yowled:
> 
>>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

Yes, of course they have --- but you specified `Bourne shell' and
explicitly not a bunch of other shells like ksh.  The POSIX shell is of
course a slightly trimmed-down ksh :)

> bit of empirical testings shows this not to be the case (at least for
> Solaris and Tru64).

Solaris keeps its in /usr/xpg4/bin/sh and /bin/ksh; Tru64 just calls it
/bin/ksh.

>>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).

dc? What a cop-out. Greg did it in sed ;)

> 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.

Ah. You could do that bit by bit (rip the netmask into quads). You don't
even need bitwise manipulation for that, just division (which ends up
being the same thing).

>>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 :-)

Unless you're using a Pyramid or something equally antique, it'll be
there.

(Oh, or a Linux box. ;) )

-- 
`The situation is completely under control. All of them were killed.'
     --- Alim Razim, for the Northern Alliance, demonstrating fine
         command of traditional Afghan prisoner control techniques.

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




More information about the GLLUG mailing list