Bash conversions: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
m (Chubbard moved page Bash converter to Bash conversions: better names) |
Revision as of 11:21, 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]}"
}