shell脚本nicenumber实现代码

所属分类: 脚本专栏 / linux shell 阅读数: 1057
收藏 0 赞 0 分享

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

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

更多精彩内容其他人还在看

linux whatis与whatis database 使用及查询方法(man使用实例)

在学习man命令时候,估计很多朋友都发现有man –f ,man –k 参数,可以查出很多数据。这些有的与man手册页相同,有些不是属于手册页的。它们数据怎么收集来的,并且whatis是怎么样工作的
收藏 0 赞 0 分享

linux dev 常见特殊设备介绍与应用(loop,null,zero,full,random)

这篇文章主要介绍了linux dev 常见特殊设备介绍与应用(loop,null,zero,full,random),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell命令快捷获得系统帮助(一)[man-pages定义规范]

linux命令帮助,一般有2种,命令自身代码里面带有使用帮助说明,这种一般很精简,太长了,程序自身的大小以及日常维护不方便。还有一种,就是带有帮助文件,类似windows的chm格式文件。下面我说下这2种怎么样查阅
收藏 0 赞 0 分享

linux shell实现随机数几种方法分享(date,random,uuid)

这篇文章主要介绍了linux shell实现随机数多种方法(date,random,uuid),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell 脚本实现tcp/upd协议通讯(重定向应用)

这篇文章主要介绍了linux shell 脚本实现tcp/upd协议通讯(重定向应用),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell数据重定向(输入重定向与输出重定向)详细分析

这篇文章主要介绍了linux shell数据重定向(输入重定向与输出重定向)详细分析,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell 管道命令(pipe)使用及与shell重定向区别

这篇文章主要介绍了linux shell 管道命令(pipe)使用及与shell重定向区别,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux下使用tcpdump抓包的实现方法

tcpdump是Linux下面的一个开源的抓包工具,和Windows下面的wireshark抓包工具一样, 支持抓取指定网口、指定目的地址、指定源地址、指定端口、指定协议的数据。这篇文章主要介绍了Linux下使用tcpdump抓包的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

零基础入门篇之Linux及Arm-Linux程序开发笔记

这篇文章主要介绍了零基础入门篇之Linux及Arm-Linux程序开发笔记,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux 中 CURL常用命令详解

这篇文章主要介绍了Linux 中 CURL常用命令详解,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多