dot

packages and services management
Log | Files | Refs | README

commit 697c3fd42bcdcbf8322010d7e4ac01d7f6eab7ff
parent 5c0b39d01cf8e3777bd1d68e627458f59565228d
Author: Josuah Demangeon <mail@josuah.net>
Date:   Fri,  1 Dec 2017 14:47:02 +0100

simpler and more efficient script

Diffstat:
Mbin/ipconv | 23++++++++---------------
1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/bin/ipconv b/bin/ipconv @@ -3,31 +3,24 @@ if [ -z "$1" ] || [ $# -ne 1 ] then exit 1 - elif [ -z "$(printf %s "$1" | tr -d 0-9)" ] then int=$1 ip= - - while [ "$int" -gt 0 ] + for i in 1 2 3 4 do - byte=$((int & 255)) - int=$((int / 256)) - [ -z "$ip" ] && ip="$byte" || ip="$byte.$ip" + ip=.$((int & 255))$ip + int=$((int >> 8)); done - - printf '%s\n' "$ip" - + printf '%s\n' "${ip#?}" elif [ -z "$(printf %s "$1" | tr -d 0-9.)" ] then - ip=$1 int=0 IFS=. - - for byte in $ip + int=0 IFS=. + for byte in $1 do - int=$((int * 256 + byte)) + int=$(((int << 8) + byte)) done - printf '%s\n' "$int" - else + printf 'unknown address format\n' exit 1 fi