Instantly share code, notes, and snippets.

Embed
What would you like to do?
将子网掩码转换为前缀长度
#!/bin/bash
# date: 2018-09-15
# license: GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt
# author: nanpuyue <nanpuyue@gmail.com> 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