Created
February 12, 2016 02:47
-
-
Save kei-sato/006cc6f069e648c077af to your computer and use it in GitHub Desktop.
example: 0xffffff00 => 24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# read from argument or stdin | |
mask="$1"; [[ -z "$mask" ]] && read -r mask < /dev/stdin | |
# remove 0x | |
mask="${mask##0x}" | |
count=0 | |
while [ "$mask" != "" ]; do | |
digit="${mask:0:1}" | |
mask="${mask:1}" | |
case "$digit" in | |
[fF]) count=$((count+4));; | |
[eE]) count=$((count+3));; | |
[cC]) count=$((count+2));; | |
8) count=$((count+1));; | |
0) ;; | |
*) echo 1>&2 "error: illegal digit $digit in netmask"; exit 1;; | |
esac | |
done | |
echo $count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment