博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读书笔记--每天一个shell--06
阅读量:6853 次
发布时间:2019-06-26

本文共 1773 字,大约阅读时间需要 5 分钟。

这个shell是来判断输入的数字是否为合理的浮点数

The Code

#!/bin/sh# validfloat -- Tests whether a number is a valid floating-point value.# Note that this script cannot accept scientific (1.304e5) notation.# To test whether an entered value is a valid floating-point number, we# need to split the value at the decimal point. We then test the first part# to see if it's a valid integer, then test the second part to see if it's a# valid >=0 integer, so -30.5 is valid, but -30.-8 isn't.. validint   # Bourne shell notation to source the validint functionvalidfloat(){  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        return 1      fi    fi    if [ "${fractionalPart%${fractionalPart#?}}" = "-" ] ; then      echo "Invalid floating-point number: '-' not allowed \        after 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') ] 将输入,以.分成整数和小数部分。

2):if [ "${fractionalPart%${fractionalPart#?}}" = "-" ]  判断小数点后面如果接‘-’号,这输出字符不合法

3)接着的一些if语句就是判断小数及整数部分合不合法

4)由于 valiint函数没给出,脚本不能完全执行,valiint函数是判断字符串是否全为数字.

      本文转自hb_fukua  51CTO博客,原文链接:http://blog.51cto.com/2804976/591504,如需转载请自行联系原作者

你可能感兴趣的文章
JS魔法堂:那些困扰你的DOM集合类型
查看>>
贴一些 Python 的笔记
查看>>
给root用户添加远程连接权限
查看>>
CentOS7下Apache2.4.6使用MySQL5.7验证
查看>>
linux 查看并发
查看>>
Linux下FTP服务器的安装和简单配置
查看>>
jQuery基本用法二
查看>>
Asp.net网站部署时遇到的一些问题
查看>>
WinForm webbrowser控件的使用
查看>>
<Power Shell>09 利用powershell 查找旧文件
查看>>
windows phone (16) UI变换 下
查看>>
管理中用人的两种思维
查看>>
与用户登录shell相关的文件/etc/profile,~/bashrc等浅析
查看>>
SQL数据库高级查询命令(4)
查看>>
Thrift结构分析及增加取客户端IP功能实现
查看>>
easyui radio 类型的取值和赋值方法
查看>>
Puppet的安装和初使用
查看>>
linux-firewalld
查看>>
exchange快速将断开的邮箱显示出来
查看>>
排查一些常见的系统故障
查看>>