From 97a1db5f551aec8d38a335455faf6b5cfd4cf63a Mon Sep 17 00:00:00 2001 From: deajan Date: Sun, 23 Oct 2016 10:35:38 +0200 Subject: [PATCH] Added HumanToNumeric function --- dev/ofunctions.sh | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/dev/ofunctions.sh b/dev/ofunctions.sh index a6a2c3f..10a6c3a 100644 --- a/dev/ofunctions.sh +++ b/dev/ofunctions.sh @@ -1,6 +1,6 @@ #### MINIMAL-FUNCTION-SET BEGIN #### -## FUNC_BUILD=2016102203 +## FUNC_BUILD=2016102301 ## 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: @@ -902,6 +902,34 @@ function IsInteger { fi } +# Converts human readable sizes into integer kilobyte sizes +function HumanToNumeric { + local notation + local suffix + local suffixPresent + local multiplier + + notation=(K M G T P E) + for suffix in "${notation[@]}"; do + multiplier=$((multiplier+1)) + if [[ "$disk_space" == *"$suffix"* ]]; then + suffixPresent=$suffix + break; + fi + done + + if [ "$suffixPresent" != "" ]; then + disk_space=${disk_space%$suffix*} + disk_space=${disk_space%.*} + # /1024 since we convert to kilobytes instead of bytes + disk_space=$((disk_space*(1024**multiplier/1024))) + else + disk_space=${disk_space%.*} + fi + + echo $disk_space +} + ## from https://gist.github.com/cdown/1163649 function urlEncode { local length="${#1}"