shell脚本nicenumber实现代码
更新时间:2016年08月14日 11:18:26 投稿:mdxy-dxy
给出一个数字,用逗号分隔的形式显示出来,希望DD和TD被实例化等
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在shell中自动生成1到100的数组方法(两种方法)
之前自己在写shell脚本的时候,需要自动创建1-100的文本确不知道该如何去创建。今天小编给大家分享两种方法,需要的朋友参考下2017-02-02
一天一个shell命令 文本操作系列-linux dd使用教程
dd 是 Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换2016-05-05
获取服务器信息的Shell脚本分享(ubuntu、centos测试通过)
这篇文章主要介绍了获取服务器信息的Shell脚本分享(ubuntu、centos测试通过),本文直接给出实现代码,本文脚本实现获取linux发行版名称、查看系统是否为64位、系统内核版本等信息,需要的朋友可以参考下2014-12-12
shell脚本将Oracle服务器中数据定时增量刷新到ftp服务器中
这篇文章主要介绍了shell脚本将Oracle服务器中数据定时增量刷新到ftp服务器中,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下2019-08-08


最新评论