Bash conversions: Difference between revisions
Jump to navigation
Jump to search
(Created page with "==== Convert formats ==== yq is a YAML converter... yq -o=json test.yml Category:bash") |
mNo edit summary |
||
Line 4: | Line 4: | ||
yq -o=json test.yml | yq -o=json test.yml | ||
== Convert value scales == | |||
<pre> | |||
#=== FUNCTION ================================================================ | |||
# NAME: byteMe | |||
# DESCRIPTION: Divides by 2^10 until < 1024 and then append metric suffix | |||
# PARAMETERS: Integer | |||
# RETURNS: String | |||
#=============================================================================== | |||
function byteMe() { | |||
declare -a METRIC=(' Bytes' 'KB' 'MB' 'GB' 'TB' 'XB' 'PB') # Array of suffixes | |||
MAGNITUDE=0 # magnitude of 2^10 | |||
PRECISION="scale=1" # change this numeric value to inrease decimal precision | |||
local RAW=$1 | |||
echo "RAW $RAW" | |||
UNITS=`echo ${RAW} | tr -d ','` # numeric arg val (in bytes) to be converted | |||
while [[ "${UNITS/.*}" -ge 1024 ]] # compares integers (b/c no floats in bash) | |||
do | |||
UNITS=`echo "$PRECISION; $UNITS/1024" | bc` # floating point math via `bc` | |||
((MAGNITUDE++)) # increments counter for array pointer | |||
done | |||
echo "$UNITS${METRIC[$MAGNITUDE]}" | |||
} | |||
</pre> | |||
[[Category:bash]] | [[Category:bash]] |
Revision as of 11:20, 12 December 2023
Convert formats
yq is a YAML converter...
yq -o=json test.yml
Convert value scales
#=== FUNCTION ================================================================ # NAME: byteMe # DESCRIPTION: Divides by 2^10 until < 1024 and then append metric suffix # PARAMETERS: Integer # RETURNS: String #=============================================================================== function byteMe() { declare -a METRIC=(' Bytes' 'KB' 'MB' 'GB' 'TB' 'XB' 'PB') # Array of suffixes MAGNITUDE=0 # magnitude of 2^10 PRECISION="scale=1" # change this numeric value to inrease decimal precision local RAW=$1 echo "RAW $RAW" UNITS=`echo ${RAW} | tr -d ','` # numeric arg val (in bytes) to be converted while [[ "${UNITS/.*}" -ge 1024 ]] # compares integers (b/c no floats in bash) do UNITS=`echo "$PRECISION; $UNITS/1024" | bc` # floating point math via `bc` ((MAGNITUDE++)) # increments counter for array pointer done echo "$UNITS${METRIC[$MAGNITUDE]}" }