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编程中IO和条件及循环处理的细节问题讨论
这篇文章主要介绍了Linux shell编程中IO和条件及循环处理的细节问题讨论,需要的朋友可以参考下2016-02-02Linux命令定位与查找之which、whereis和find的用法示例详解
Linux命令的定位与查找是我们日常工作中必备的技能,掌握which、whereis和find这三个命令,可以帮助我们更加高效地进行文件搜索和定位工作,这篇文章主要介绍了Linux命令定位与查找:which、whereis和find的用法详解,需要的朋友可以参考下2023-10-10shell脚本学习指南[六](Arnold Robbins & Nelson H.F. Beebe著)
这篇文章主要介绍了shell脚本学习指南[六](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以参考下2014-02-02在linux上定期执行命令、脚本(cron,crontab,anacron)
在linux下,如果想要在未来的某个时刻执行某个任务,并且在未来的每一个这样的时刻里都要执行这个任务。这篇文章主要介绍了在linux上定期执行命令、脚本(cron,crontab,anacron)的相关知识,需要的朋友可以参考下2018-07-07
最新评论