更新说明
:优化最后输出的结果信息的格式
使用说明
:将如下的内容保存到文件后source文件,然后即可输入 sfn.isIPv4 来使用了
sfn.isIPv4() {
_doVerify() {
_etip() { printf '> NO : [ \e[31;1m%s\e[0m ], for %s \n' ${ia} "$*"; }
#-------------------------------------------------------------
local ia=$1
#check charset
local ecs=${ia//[0-9.]/}
if [ "X${ecs}" != "X" ]; then
local et="$(echo $ecs | sed -r 's#(.)#\1 #g' | xargs -n1 | sort | uniq | xargs)"
_etip "has invalid chars: { ${et// /,} } !!!" && return 1
fi
#-------------------------------------------------------------
#segments
local ca=(${ia//\./ }) && sn=${#ca[@]}
[ ${sn} -ne 4 ] && _etip "it divids by dot(.) into < $sn > parts !!!" && return 2
#-------------------------------------------------------------
#ranges: SEG1: 1~255, SEG2,3,4: 0~255
[ ${ca} -eq 0 ] && [ "X$1" != "X0.0.0.0" ] && _etip "its' 1st part ZERO(0) !!!" && return 3
for i in 0 1 2 3; do
y=${ca[$i]}
[ $y -gt 255 ] && _etip "its' $((++i))st part greater than 255 !!!" && return 4
if [ "X${STRICT}" == "X1" ] && [ $y -ne 0 ] && [ "X${y:0:1}" == "X0" ]; then
_etip "in STRICT mode, its' $((++i))st part has ZERO-PREFIX (0-i)" && return 5
fi
done
#-------------------------------------------------------------
echo -e '> YES: [ \e[32;1m'$ia'\e[0m ]'
}
local STRICT= usgStr='Usage: '${FUNCNAME}' [-s,--strict-mode] <LIST_OF_IPV4>'
case $1 in
"" | -h | --help) echo && echo $usgStr && echo && return ;;
-s) shift && STRICT=1 ;;
esac
local t= && for t in $@; do _doVerify $t; done
}