shell腳本nicenumber實現(xiàn)代碼
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
這腳本我們以后分析,現(xiàn)在先mark下。
相關(guān)文章
linux上搭建solr的實現(xiàn)方法(用jetty部署)
下面小編就為大家分享一篇linux上搭建solr的實現(xiàn)方法(用jetty部署),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
Linux在shell中自動生成1到100的數(shù)組方法(兩種方法)
之前自己在寫shell腳本的時候,需要自動創(chuàng)建1-100的文本確不知道該如何去創(chuàng)建。今天小編給大家分享兩種方法,需要的朋友參考下2017-02-02
一天一個shell命令 文本操作系列-linux dd使用教程
dd 是 Linux/UNIX 下的一個非常有用的命令,作用是用指定大小的塊拷貝一個文件,并在拷貝的同時進行指定的轉(zhuǎn)換2016-05-05
獲取服務(wù)器信息的Shell腳本分享(ubuntu、centos測試通過)
這篇文章主要介紹了獲取服務(wù)器信息的Shell腳本分享(ubuntu、centos測試通過),本文直接給出實現(xiàn)代碼,本文腳本實現(xiàn)獲取linux發(fā)行版名稱、查看系統(tǒng)是否為64位、系統(tǒng)內(nèi)核版本等信息,需要的朋友可以參考下2014-12-12
shell腳本將Oracle服務(wù)器中數(shù)據(jù)定時增量刷新到ftp服務(wù)器中
這篇文章主要介紹了shell腳本將Oracle服務(wù)器中數(shù)據(jù)定時增量刷新到ftp服務(wù)器中,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-08-08
Linux shell查找文件顯示行號和對應(yīng)區(qū)間的內(nèi)容
今天小編就為大家分享一篇關(guān)于Linux shell查找文件顯示行號和對應(yīng)區(qū)間的內(nèi)容,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
linux下python3連接mysql數(shù)據(jù)庫問題
這篇文章主要介紹了linux下python3連接mysql數(shù)據(jù)庫問題,需要的朋友可以參考下2015-10-10
Shell中創(chuàng)建序列和數(shù)組(list、array)的方法
這篇文章主要介紹了Shell中創(chuàng)建序列和數(shù)組(list、array)的方法,本文講解了seq方法生成以及通過內(nèi)部{begin..end}生成法,需要的朋友可以參考下2015-07-07

