linux shell实现判断输入的数字是否为合理的浮点数
这个shell是来判断输入的数字是否为合理的浮点数
实现代码如下:
#!/bin/sh
#validfloat--Testswhetheranumberisavalidfloating-pointvalue.
#Notethatthisscriptcannotacceptscientific(1.304e5)notation.
#Totestwhetheranenteredvalueisavalidfloating-pointnumber,we
#needtosplitthevalueatthedecimalpoint.Wethentestthefirstpart
#toseeifit'savalidinteger,thentestthesecondparttoseeifit'sa
#valid>=0integer,so-30.5isvalid,but-30.-8isn't.
.validint#Bourneshellnotationtosourcethevalidintfunction
validfloat()
{
fvalue="$1"
if[!-z$(echo$fvalue|sed's/[^.]//g')];then
decimalPart="$(echo$fvalue|cut-d.-f1)"
fractionalPart="$(echo$fvalue|cut-d.-f2)"
if[!-z$decimalPart];then
if!validint"$decimalPart""""";then
return1
fi
fi
if["${fractionalPart%${fractionalPart#?}}"="-"];then
echo"Invalidfloating-pointnumber:'-'notallowed\
afterdecimalpoint">&2
return1
fi
if["$fractionalPart"!=""];then
if!validint"$fractionalPart""0""";then
return1
fi
fi
if["$decimalPart"="-"-o-z"$decimalPart"];then
if[-z$fractionalPart];then
echo"Invalidfloating-pointformat.">&2;return1
fi
fi
else
if["$fvalue"="-"];then
echo"Invalidfloating-pointformat.">&2;return1
fi
if!validint"$fvalue""""";then
return1
fi
fi
return0
}
notice:
1):if[!-z$(echo$fvalue|sed's/[^.]//g')]将输入,以.分成整数和小数部分。
2):if["${fractionalPart%${fractionalPart#?}}"="-"]判断小数点后面如果接‘-'号,这输出字符不合法
3)接着的一些if语句就是判断小数及整数部分合不合法
4)由于valiint函数没给出,脚本不能完全执行,valiint函数是判断字符串是否全为数字.
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下