shell腳本nicenumber實(shí)現(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全網(wǎng)最全面常用命令整理(附實(shí)例)
這篇文章主要介紹了Linux命令,是目前最全面的集合,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
一篇教會(huì)你寫90%的shell腳本(入門小結(jié))
這篇文章主要介紹了一篇教會(huì)你寫90%的shell腳本,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Shell中實(shí)現(xiàn)字符串反轉(zhuǎn)方法分享
這篇文章主要介紹了Shell中實(shí)現(xiàn)字符串反轉(zhuǎn)方法分享,本文同時(shí)提供了多種語言的實(shí)現(xiàn)方法,如awk、python、bash、C語言等,需要的朋友可以參考下2014-12-12
Linux shell編程中IO和條件及循環(huán)處理的細(xì)節(jié)問題討論
這篇文章主要介紹了Linux shell編程中IO和條件及循環(huán)處理的細(xì)節(jié)問題討論,需要的朋友可以參考下2016-02-02
Linux命令定位與查找之which、whereis和find的用法示例詳解
Linux命令的定位與查找是我們?nèi)粘9ぷ髦斜貍涞募寄?掌握which、whereis和find這三個(gè)命令,可以幫助我們更加高效地進(jìn)行文件搜索和定位工作,這篇文章主要介紹了Linux命令定位與查找:which、whereis和find的用法詳解,需要的朋友可以參考下2023-10-10
shell腳本學(xué)習(xí)指南[六](Arnold Robbins & Nelson H.F. Beebe著)
這篇文章主要介紹了shell腳本學(xué)習(xí)指南[六](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以參考下2014-02-02
linux 隨機(jī)密碼生成工具mkpasswd詳解及實(shí)例
這篇文章主要介紹了linux 隨機(jī)密碼生成工具mkpasswd詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
在linux上定期執(zhí)行命令、腳本(cron,crontab,anacron)
在linux下,如果想要在未來的某個(gè)時(shí)刻執(zhí)行某個(gè)任務(wù),并且在未來的每一個(gè)這樣的時(shí)刻里都要執(zhí)行這個(gè)任務(wù)。這篇文章主要介紹了在linux上定期執(zhí)行命令、腳本(cron,crontab,anacron)的相關(guān)知識,需要的朋友可以參考下2018-07-07
shell腳本執(zhí)行命令自動(dòng)填充密碼(自動(dòng)輸入密碼)
這篇文章主要介紹了shell?執(zhí)行命令自動(dòng)填充密碼,文中結(jié)合實(shí)例代碼通過三種方式講解了Shell?腳本自動(dòng)輸入密碼的方法,需要的朋友可以參考下2023-02-02
Linux echo命令的使用及三種實(shí)現(xiàn)方式
這篇文章主要介紹了Linux echo命令的使用及三種實(shí)現(xiàn)方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05

