This shell is to determine if the number entered is a reasonable float
The implementation code is as follows:
#!/bin/sh # Validfloat-Tests Whether a number is a valid floating-point value.
# This script cannot accept scientific (1.304E5) notation. # to test whether ' a entered value is a valid floating-point number, we ' need to split the ' value at the ' decimal point. We then test the "I" # to "if it" s a valid integer, then test the second part to the if it's a # valid >=0 in
Teger, so-30.5 is valid, but-30.-8 isn ' t. . Validint # Bourne Shell notation to source the Validint function validfloat () {fvalue= ' $ ' if [!-Z $ (echo $fvalue | Sed ' s/[^.] G ')]; Then decimalpart= "$ (echo $fvalue | cut-d.-f1)" fractionalpart= "$ (echo $fvalue | cut-d.-f2)" If [!-Z $decimal Part]; Then if! Validint "$decimalPart" "" "" "; Then return 1 fi fi if ["${fractionalpart%${fractionalpart#?}}" = "-"]; Then echo "Invalid floating-point number: '-' not allowed \ on decimal point ' >&2 return 1 fi if [ "$fractionalPart" "!="]; Then if! Validint "$fractionalPart" "0" ""; Then return 1 fi fi if ["$decimalPart" = "-"-o-z "$decimalPart"]; Then if [-Z $fractionalPart]; Then echo "Invalid floating-point format." >&2; Return 1 fi fi else if ["$fvalue" = "-"]; Then echo "Invalid floating-point format." >&2; Return 1 fi if! Validint "$fvalue" "" "" "; Then return 1 fi fi return 0}
Notice:
1: if [!-Z $ (echo $fvalue | sed ' s/[^.] G ')] will be entered to. divide into integers and decimal parts.
2): If ["${fractionalpart%${fractionalpart#?}}" = "-"] to determine if the "-" number after the decimal point, the output character is not valid
3) Then some if statement is to judge the decimal and integer parts of the law
4) Because the Valiint function is not given, the script cannot be fully executed, and the Valiint function is to determine whether the string is all numeric.