linux中浮點數驗證

來源:互聯網
上載者:User

      /> cat > test29.sh
      #!/bin/sh
      #1. 之前的一個條目已經介紹了awk中match函數的功能,如果匹配返回匹配的位置值,否則返回0。
      #2. 對於Shell中的函數而言,返回0表示成功,其他值表示失敗,該語義等同於Linux中的進程退出值。調用者可以通過內建變數$?擷取傳回值,或者作為條件運算式的一部分直接判斷。
      validint() {
          ret=`echo $1 | awk '{start = match($1,/^-?[0-9]+$/); if (start == 0) print "1"; else print "0"}'`
          return $ret
      }
   
      validfloat() {
          fvalue="$1"
          #3. 判斷當前參數中是否包含小數點兒。如果包含則需要將其拆分為整數部分和小數部分,分別進行判斷。
          if [ ! -z  $(echo $fvalue | sed 's/[^.]//g') ]; then
              decimalpart=`echo $fvalue | cut -d. -f1`
              fractionalpart=`echo $fvalue | cut -d. -f2`
              #4. 如果整數部分不為空白,但是不是合法的整型,則視為非法格式。
              if [ ! -z $decimalpart ]; then
                  if ! validint "$decimalpart" ; then
                      echo "decimalpart is not valid integer."
                      return 1
                  fi
              fi
              #5. 判斷小數部分的第一個字元是否為-,如果是則非法。
              if [ "${fractionalpart:0:1}" = "-" ]; then
                  echo "Invalid floating-point number: '-' not allowed after decimal point." >&2
                  return 1
              fi
              #6. 如果小數部分不為空白,同時也不是合法的整型,則視為非法格式。
              if [ "$fractionalpart" != "" ]; then
                  if ! validint "$fractionalpart" ; then
                      echo "fractionalpart is not valid integer."
                      return 1
                  fi
              fi
              #7. 如果整數部分僅為-,或者為空白,如果此時小數部分也是空,則為非法格式。
              if [ "$decimalpart" = "-" -o -z "$decimalpart" ]; then
                  if [ -z $fractionalpart ]; then
                      echo "Invalid floating-point format." >&2
                      return 1
                  fi
              fi
          else
              #8. 如果當前參數僅為-,則視為非法格式。
              if [ "$fvalue" = "-" ]; then
                  echo "Invalid floating-point format." >&2
                  return 1
              fi
              #9. 由於參數中沒有小數點,如果該值不是合法的整數,則為非法格式。
              if ! validint "$fvalue" ; then
                  echo "Invalid floating-point format." >&2
                  return 1
              fi
          fi
          return 0
      }   
      if validfloat $1 ; then
          echo "$1 is a valid floating-point value."
      fi
      exit 0
      CTRL+D
      /> ./test29.sh 47895      
      47895 is a valid floating-point value.
      /> ./test29.sh 47895.33
      47895.33 is a valid floating-point value.
      /> ./test29.sh 47895.3e
      fractionalpart is not valid integer.
      /> ./test29.sh 4789t.34
      decimalpart is not valid integer. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.