Last active
September 15, 2018 04:13
-
-
Save nanpuyue/7357484426658beeff2aa0919c2c831b to your computer and use it in GitHub Desktop.
将子网掩码转换为前缀长度
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
#!/bin/bash | |
# date: 2018-09-15 | |
# license: GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt | |
# author: nanpuyue <[email protected]> https://blog.nanpuyue.com | |
n=0;b=3 | |
for i in ${1//./ };do | |
if ((0 <= i < 256));then | |
let n+=$((2**(b*8) * i)) | |
let b-- | |
else | |
exit 1 | |
fi | |
done | |
[[ $(echo "obase=2;$n"|bc) =~ ^(1+)0*$ ]] &&\ | |
((${#BASH_REMATCH[0]} == 32)) &&\ | |
echo ${#BASH_REMATCH[1]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment