Fixed HumanToNumeric function

This commit is contained in:
deajan 2016-10-23 13:27:35 +02:00
parent 80a5bb0102
commit 0e897f66c5
1 changed files with 10 additions and 7 deletions

View File

@ -1,6 +1,6 @@
#### MINIMAL-FUNCTION-SET BEGIN #### #### MINIMAL-FUNCTION-SET BEGIN ####
## FUNC_BUILD=2016102302 ## FUNC_BUILD=2016102303
## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
## To use in a program, define the following variables: ## To use in a program, define the following variables:
@ -903,7 +903,10 @@ function IsInteger {
} }
# Converts human readable sizes into integer kilobyte sizes # Converts human readable sizes into integer kilobyte sizes
# Usage numericSize="$(HumanToNumeric $humanSize)"
function HumanToNumeric { function HumanToNumeric {
local value="${1}"
local notation local notation
local suffix local suffix
local suffixPresent local suffixPresent
@ -912,22 +915,22 @@ function HumanToNumeric {
notation=(K M G T P E) notation=(K M G T P E)
for suffix in "${notation[@]}"; do for suffix in "${notation[@]}"; do
multiplier=$((multiplier+1)) multiplier=$((multiplier+1))
if [[ "$disk_space" == *"$suffix"* ]]; then if [[ "$value" == *"$suffix"* ]]; then
suffixPresent=$suffix suffixPresent=$suffix
break; break;
fi fi
done done
if [ "$suffixPresent" != "" ]; then if [ "$suffixPresent" != "" ]; then
disk_space=${disk_space%$suffix*} value=${value%$suffix*}
disk_space=${disk_space%.*} value=${value%.*}
# /1024 since we convert to kilobytes instead of bytes # /1024 since we convert to kilobytes instead of bytes
disk_space=$((disk_space*(1024**multiplier/1024))) value=$((value*(1024**multiplier/1024)))
else else
disk_space=${disk_space%.*} value=${value%.*}
fi fi
echo $disk_space echo $value
} }
## from https://gist.github.com/cdown/1163649 ## from https://gist.github.com/cdown/1163649