shell指令碼nicenumber實現代碼_linux shell

來源:互聯網
上載者:User

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 valuesTD="," # thousands delimiter, to separate every three digitswhile getopts "d:t:" opt; do case $opt in  d ) DD="$OPTARG"  ;;  t ) TD="$OPTARG"  ;; esacdoneshift $(($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 0finicenumber $1 1     # second arg forces nicenumber to 'echo' outputexit 0

這指令碼我們以後分析,現在先mark下。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.