interger to I P address

Sorry to be continuing this thread, but I find a certain kind of elegance in
bash which isn't actually there, but helps me sleep at night.

bash# iptoint(){ oct1=`echo $1|awk -F\. '{print $1}'`; oct2=`echo $1|awk -F\. '{print $2}'`; oct3=`echo $1|awk -F\. '{print $3}'`; oct4=`echo $1|awk -F\. '{print $4}'`; echo $[($oct1<<24)+($oct2<<16 )+($oct3<<8)+$oct4 ];}
bash# inttoip(){ echo $[$1>>24].$[($1>>16)&255].$[($1>>8)&255].$[$1&255]; }

bash# inttoip 1089055123

BASH? Hahaha. Real Admins use sh. More portable(*).


v=$1;for n in 4 3 2;do
eval o$n=`expr $v % 256`;v=`expr $v / 256`
done; echo $v.$o2.$o3.$o4

Needlessly complex (just to keep up sh's reputation for complexity):

v4=$1;for n in 4 3 2;do
eval o$n=`eval expr \\$v$n % 256`;eval v`expr $n - 1`=`eval expr \\$v$n / 256`
done; echo $v1.$o2.$o3.$o4

$ ipscript 1089055123

(*) Not a claim of actual portability of this code. Just having fun
writing UGLY shell code.

... JG

Somebody's going to bring in Emacs now. Then somebody else will claim VI can do it faster and using less memory....

Argh. :wink: