shell脚本nicenumber实现代码
Givenanumber,showsitincomma-separatedform.ExpectsDDandTDtobeinstantiated.Instantiatesnicenum.or,ifasecondargisspecified,theoutputisechoedtostdout.
废话不多说,首先是
#!/bin/sh #nicenumber--Givenanumber,showsitincomma-separatedform. #ExpectsDDandTDtobeinstantiated.Instantiatesnicenum #or,ifasecondargisspecified,theoutputisechoedtostdout. nicenumber() { #Notethatweassumethat'.'isthedecimalseparatorin #theINPUTvaluetothisscript.Thedecimalseparatorintheoutputvalueis #'.'unlessspecifiedbytheuserwiththe-dflag integer=$(echo$1|cut-d.-f1)#leftofthedecimal decimal=$(echo$1|cut-d.-f2)#rightofthedecimal if[$decimal!=$1];then #There'safractionalpart,solet'sincludeit. result="${DD:="."}$decimal" fi thousands=$integer while[$thousands-gt999];do remainder=$(($thousands%1000))#threeleastsignificantdigits while[${#remainder}-lt3];do#forceleadingzerosasneeded remainder="0$remainder" done thousands=$(($thousands/1000))#toleftofremainder,ifany result="${TD:=","}${remainder}${result}"#buildsrighttoleft done nicenum="${thousands}${result}" if[!-z$2];then echo$nicenum fi } DD="."#decimalpointdelimiter,toseparateintegerandfractionalvalues TD=","#thousandsdelimiter,toseparateeverythreedigits whilegetopts"d:t:"opt;do case$optin d)DD="$OPTARG";; t)TD="$OPTARG";; esac done shift$(($OPTIND-1)) if[$#-eq0];then echo"Usage:$(basename$0)[-dc][-tc]numericvalue" echo"-dspecifiesthedecimalpointdelimiter(default'.')" echo"-tspecifiesthethousandsdelimiter(default',')" exit0 fi nicenumber$11#secondargforcesnicenumberto'echo'output exit0
这脚本我们以后分析,现在先mark下。