• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > linux shell实现判断输入的数字是否为合理的浮点数

linux shell实现判断输入的数字是否为合理的浮点数

作者:mdxy-dxy 字体:[增加 减小] 来源:互联网

mdxy-dxy 通过本文主要向大家介绍了linux shell 数字比较,linux shell脚本,linux shell编程,linux shell脚本攻略,linux shell脚本实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

实现代码如下:

#!/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 function

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
    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
}
</div>

notice:
1): if [ ! -z $(echo $fvalue | sed 's/[^.]//g') ] 将输入,以.分成整数和小数部分。
2):if [ "${fractionalPart%${fractionalPart#?}}" = "-" ] 判断小数点后面如果接‘-'号,这输出字符不合法
3)接着的一些if语句就是判断小数及整数部分合不合法
4)由于 valiint函数没给出,脚本不能完全执行,valiint函数是判断字符串是否全为数字.

</div>

您可能想查找下面的文章:

  • linux shell实现判断输入的数字是否为合理的浮点数
  • linux shell中的比较符号与特殊符号介绍

相关文章

  • 脚本自动添加crontab示例
  • Shell命令批量修改图片文件名
  • shell脚本编程实现9*9乘法表
  • Linux磁盘空间被未知资源耗尽的解决方法
  • C语言实现的ls命令源码分享
  • linux命令scp和sftp详细介绍
  • bash脚本中if语句的使用方法
  • Shell脚本实现精准清除Squid缓存
  • 8个实用的Shell脚本分享
  • Linux shell实现HTTP服务示例代码

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • shell 中数学计算总结
    • shell脚本学习指南[二](Arnold Robbins & Nelson H.F. Beebe著)
    • Python执行Linux系统命令的4种方法
    • 轻松掌握Linux关机重启命令
    • 强制删除rpm包的方法
    • linux下编译boost.python简单方法
    • Linux 中(加、减、乘、除)实例详解
    • 详谈Linux打包与压缩及tar命令
    • Shell中判断字符串是否为数字的6种方法分享
    • 关于Shell脚本效率优化的一些个人想法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有