Legalizing floating-point number input in shell scripts

Source: Internet
Author: User

In shell scripts, the process of legalizing a floating-point value in a shell script seems a little frustrating, however, floating point numbers are just two integers separated by decimal points. Contact the 5th script validint. sh, and you will find that the test of floating point legalization is short and surprising. Code: 01 #! /Bin/sh02 03 # validfloat. sh -- test whether a value is a valid floating point 04 # note that this script does not accept numbers in the scientific notation form 05 06 # to test whether the value is valid, we need to split the number at the decimal point. 07 # test the first part to see if it is a valid integer 08 # test the second part to see if it is an integer greater than or equal to 0. 09 # So-30.5 is valid,-30.-8 is invalid. 10 11 source validint. sh12 13 validfloat () 14 {15 fvalue = "$1" 16 17 if [! -Z $ (echo $ fvalue | sed's/[^.] // G')]; then18 19 decimalPart = "$ (echo $ fvalue | cut-d. -f1) "20 fractionalPart =" $ (echo $ fvalue | cut-d. -f2) "21 22 if [! -Z $ decimalPart]; then23 if! Validint "$ decimalPart"; then24 return 125 fi26 fi27 28 if ["$ {fractionalPart % $ {fractionalPart #?}} "="-"]; Then29 echo" Invalid floating-point number: '-' not allowed \ 30 after decimal point "> & 231 return 132 fi33 34 if [" $ fractionalPart "! = ""]; Then35 if! Validint "$ fractionalPart" "0"; then36 return 137 fi38 fi39 40 if ["$ decimalPart" = "-"-o-z "$ decimalPart"]; then41 if [-z $ fractionalPart]; then42 echo "Invalid floating-point format. "> & 243 fi44 fi45 46 else47 if [" $ fvalue "="-"]; then48 echo" Invalid floating-point format. "> & 249 return 150 fi51 52 if! Validint "$ fvalue"; then53 return 154 fi55 56 fi57 58 return 059} 60 61 if validfloat $1; then62 echo "$1 is a valid floating-point value" 63 fi run script: If no error message is returned when a function is called, the return code is 0, the given number is a valid floating point number. Run the following lines in the script: 1if validfloat $1; then2 echo "$1 is a valid floating-point value" 3fi: 01. /validfloat. sh 1234.5602 1234.56 is a valid floating-point value03. /validfloat. sh-1234.5604-1234.56 is a valid floating-point value05. /validfloat. sh -. 7506 -. 75 is a valid floating-point value07. /validfloat. sh-11. -1208 Invalid floating-point number: '-' not allowed after decimal point09. /validfloat. Sh 1.0344e2210 incorrect digit format! There are only numbers and no comma or space. In the above script, use source to introduce validint. sh, remember to comment out the last few test scripts in validint; otherwise, an error will be reported. For details about the usage of source, refer to the manual page. Analysis Script: The best extension to the above script is to make it support scientific notation. The content of the last test. This is not too difficult. You need to test whether there are 'E' or 'E', and divide the result into three parts: the part before the decimal point, with only one digit; the fractional part; and the exponential part. Make sure that each part is an integer. I thought about it for a while and wrote one. You are welcome to correct it: 01 # test science note 02 validfloat () 03 {04 fvalue = "$1" 05 delimiter = "$ (echo $ fvalue | sed's/[^ e | ^ E] // G ') "06 07 if [! -Z $ delimiter]; then # Check whether e or E08 09 if [$ {# delimiter}-ne 1] exists. then # e or E cannot have multiple 10 echo "only one e or E. "11 return 112 fi13 14 decimalPart =" $ (echo $ fvalue | cut-d. -f1) "#15 part =" $ (echo $ fvalue | cut-d. -f2-) "# After decimal point, note that there is a small horizontal after f2, the purpose is to output the remaining 16 fractionalPart = "$ (echo $ part | cut"-d $ delimiter "-f1)" # after the decimal point, e part 17 exponent = "$ (echo $ part | cut"-d $ delimiter "-f2)" # e part 18 19 if! Validint "$ decimalPart" "-9" "9"; then # test whether the part before the decimal point is a number, that is, 20 echo "scientific notation's decimal part abs must in [0-9] in the 0-9 range. "21 return 122 fi23 24 if [" $ {fractionalPart % $ {fractionalPart #?}} "= '-']; Then # test whether the first symbol in the fractional part is a minus sign 25 echo" scientific notation's fractional portion cannot be negative. "26 return 127 fi28 29 if [" $ fractionalPart "="]; then # test whether the fractional part is null 30 echo "scientific notation's fractional portion is empty. "31 return 132 else33 if! Validint "$ fractionalPart"; then # test whether the fractional part is an integer 34 echo "scientific notation's fractional portion is not integer." 35 return 136 fi37 fi38 39 if! Validint "$ exponent"; then40 echo "scientific notation's exponent not integer." 41 return 142 fi43 44 elif [! -Z $ (echo $ fvalue | sed's/[^.] // G')]; The Code below then is the same as that above. Then test: 1. /validfloat2.sh 1.0EEe22 2 only one e or E.3. /validfloat2.sh 1. -3E224 scientific notation's fractional portion cannot be negative.5/validfloat2.sh 1.34E-22.356 error number format! Only numbers are allowed. 7 scientific notation's exponent not integer.8. /validfloat2.sh 1.013E229 1.013E22 is a valid floating-point value. Finally, sort out the contents in the book and write them as follows: 01 #! /Bin/sh02 03 source validint. sh04 05 validfloat () 06 {07 fvalue = "$1" 08 delimiter = "$ (echo $ fvalue | sed's/[^ e | ^ E] // G ') "09 10 if [! -Z $ delimiter]; then # Check whether e or E11 12 if [$ {# delimiter}-ne 1] exists. then # e or E cannot have multiple 13 echo "only one e or E. "14 return 115 fi16 17 decimalPart =" $ (echo $ fvalue | cut-d. -f1) "#18 part =" $ (echo $ fvalue | cut-d. -f2-) "# After decimal point, note that there is a small horizontal after f2, the purpose is to output the remaining 19 fractionalPart = "$ (echo $ part | cut"-d $ delimiter "-f1)" # after the decimal point, e first part 20 exponent = "$ (echo $ part | cut"-d $ delimiter "-f2)" # e later part 21 22 if! Validint "$ decimalPart" "-9" "9"; then # test whether the part before the decimal point is a number, that is, 23 echo "scientific notation's decimal part abs must in [0-9] in the 0-9 range. "24 return 125 fi26 27 if [" $ {fractionalPart % $ {fractionalPart #?}} "= '-']; Then # test whether the first symbol in the fractional part is negative 28 echo" scientific notation's fractional portion cannot be negative. "29 return 130 fi31 32 if [" $ fractionalPart "="]; then # test whether the fractional part is null 33 echo "scientific notation's fractional portion is empty. "34 return 135 else36 if! Validint "$ fractionalPart"; then # test whether the fractional part is an integer 37 echo "scientific notation's fractional portion is not integer." 38 return 139 fi40 fi41 42 if! Validint "$ exponent"; then43 echo "scientific notation's exponent not integer." 44 return 145 fi46 47 elif [! -Z $ (echo $ fvalue | sed's/[^.] // G')]; then48 49 decimalPart = "$ (echo $ fvalue | cut-d. -f1) "50 fractionalPart =" $ (echo $ fvalue | cut-d. -f2) "51 52 if [! -Z $ decimalPart]; then53 if! Validint "$ decimalPart"; then54 return 155 fi56 fi57 58 if ["$ {fractionalPart % $ {fractionalPart #?}} "="-"]; Then59 echo" Invalid floating-point number: '-' not allowed \ 60 after decimal point "> & 261 return 162 fi63 64 if [" $ fractionalPart "! = ""]; Then65 if! Validint "$ fractionalPart" "0"; then66 return 167 fi68 fi69 70 if ["$ decimalPart" = "-"-o-z "$ decimalPart"]; then71 if [-z $ fractionalPart]; then72 echo "Invalid floating-point format. "> & 273 fi74 fi75 76 else77 if [" $ fvalue "="-"]; then78 echo" Invalid floating-point format. "> & 279 return 180 fi81 82 if! Validint "$ fvalue"; then83 return 184 fi85 86 fi87 88 return 089} 90 91 if validfloat $1; then92 echo "$1 is a valid floating-point value" 93 fi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.