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

shell脚本nicenumber实现代码

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

mdxy-dxy 通过本文主要向大家介绍了shell脚本代码,shell脚本,shell脚本编程,shell脚本学习指南,shell脚本实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Given a number, shows it in comma-separated form.Expects DD and TD to be instantiated. Instantiates nicenum. or, if a second arg is specified, the output is echoed to stdout.

废话不多说,首先是

#!/bin/sh
# nicenumber -- Given a number, shows it in comma-separated form.
# Expects DD and TD to be instantiated. Instantiates nicenum
# or, if a second arg is specified, the output is echoed to stdout.

nicenumber()
{
 # Note that we assume that '.' is the decimal separator in
 # the INPUT value to this script. The decimal separator in the output value is
 # '.' unless specified by the user with the -d flag

 integer=$(echo $1 | cut -d. -f1)       # left of the decimal
 decimal=$(echo $1 | cut -d. -f2)       # right of the decimal

 if [ $decimal != $1 ]; then
  # There's a fractional part, so let's include it.
  result="${DD:="."}$decimal"
 fi

 thousands=$integer

 while [ $thousands -gt 999 ]; do
  remainder=$(($thousands % 1000))  # three least significant digits

  while [ ${#remainder} -lt 3 ] ; do # force leading zeros as needed
   remainder="0$remainder"
  done

  thousands=$(($thousands / 1000))  # to left of remainder, if any
  result="${TD:=","}${remainder}${result}"  # builds right to left
 done

 nicenum="${thousands}${result}"
 if [ ! -z $2 ] ; then
  echo $nicenum
 fi
}

DD="." # decimal point delimiter, to separate integer and fractional values
TD="," # thousands delimiter, to separate every three digits

while getopts "d:t:" opt; do
 case $opt in
  d ) DD="$OPTARG"  ;;
  t ) TD="$OPTARG"  ;;
 esac
done
shift $(($OPTIND - 1))

if [ $# -eq 0 ] ; then
 echo "Usage: $(basename $0) [-d c] [-t c] numeric value"
 echo " -d specifies the decimal point delimiter (default '.')"
 echo " -t specifies the thousands delimiter (default ',')"
 exit 0
fi

nicenumber $1 1     # second arg forces nicenumber to 'echo' output

exit 0
</div>

这脚本我们以后分析,现在先mark下。

</div>

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

  • shell脚本nicenumber实现代码
  • Shell脚本实现的基于SVN的代码提交量统计工具
  • Shell脚本实现C语言代码行数统计
  • Shell脚本实现批量下载网络图片代码分享
  • Shell脚本实现的阳历转农历代码分享
  • Shell脚本实现批量下载网络图片代码分享
  • Shell脚本实现的阳历转农历代码分享
  • shell脚本中使用iconv实现批量文件转码的代码分享
  • 自动重启服务的shell脚本代码
  • shell脚本运行5秒后自动退出的代码

相关文章

  • linux动态链接库使用方法分享
  • linux下定时执行任务的方法及crontab 用法说明(收集整理)
  • Shell脚本注释写法
  • shell(bash)下“time” 命令的输出详解
  • putty实现自动登录的方法(ssh和ssh2)
  • linux shell实现转换输入日期的格式
  • LINUX 查找tomcat日志关键词命令
  • 获取站点的各类响应时间(dns解析时间,响应时间,传输时间)
  • 关于vi和vim的区别及命令详解
  • 五个常用的Linux监控脚本代码

文章分类

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

最近更新的内容

    • 浅谈:linux cron 计划任务常用符号小结
    • Shell脚本中获取本机ip地址的3个方法
    • 在linux的终端退出python命令行的方法
    • Linux Shell中三种引号的用法及区别
    • shell脚本编程之循环语句学习笔记
    • Shell脚本定期清空大于1G的日志文件
    • Gearman::XS在Centos下的编译安装方法
    • centos/rhel下实现nginx自启动脚本实例
    • 关于ssh连不上问题的解决方法(必看)
    • 虚拟机中使用linux系启用文件共享之后的文件存在的位置方法

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

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