Bash conversions: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
|||
Line 1: | Line 1: | ||
<metadesc>Bash functions to convert values.</metadesc> | |||
== Convert formats == | == Convert formats == | ||
Latest revision as of 13:58, 25 April 2024
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]}" }
RESULT2=$(byteMe ${TOTAL_MEMORY}) ++(./linux_check_memory.sh:111): byteMe 33555009536 ++(./linux_check_memory.sh:62): byteMe(): METRIC=(' Bytes' 'KB' 'MB' 'GB' 'TB' 'XB' 'PB') ++(./linux_check_memory.sh:62): byteMe(): declare -a METRIC ++(./linux_check_memory.sh:63): byteMe(): MAGNITUDE=0 ++(./linux_check_memory.sh:64): byteMe(): PRECISION=scale=1 ++(./linux_check_memory.sh:65): byteMe(): local RAW=33555009536 ++(./linux_check_memory.sh:66): byteMe(): echo 'RAW 33555009536' +++(./linux_check_memory.sh:67): byteMe(): echo 33555009536 +++(./linux_check_memory.sh:67): byteMe(): tr -d , ++(./linux_check_memory.sh:67): byteMe(): UNITS=33555009536 ++(./linux_check_memory.sh:68): byteMe(): [[ 33555009536 -ge 1024 ]] +++(./linux_check_memory.sh:70): byteMe(): echo 'scale=1; 33555009536/1024' +++(./linux_check_memory.sh:70): byteMe(): bc ++(./linux_check_memory.sh:70): byteMe(): UNITS=32768564.0 ++(./linux_check_memory.sh:71): byteMe(): (( MAGNITUDE++ )) ++(./linux_check_memory.sh:68): byteMe(): [[ 32768564 -ge 1024 ]] +++(./linux_check_memory.sh:70): byteMe(): echo 'scale=1; 32768564.0/1024' +++(./linux_check_memory.sh:70): byteMe(): bc ++(./linux_check_memory.sh:70): byteMe(): UNITS=32000.5 ++(./linux_check_memory.sh:71): byteMe(): (( MAGNITUDE++ )) ++(./linux_check_memory.sh:68): byteMe(): [[ 32000 -ge 1024 ]] +++(./linux_check_memory.sh:70): byteMe(): echo 'scale=1; 32000.5/1024' +++(./linux_check_memory.sh:70): byteMe(): bc ++(./linux_check_memory.sh:70): byteMe(): UNITS=31.2 ++(./linux_check_memory.sh:71): byteMe(): (( MAGNITUDE++ )) ++(./linux_check_memory.sh:68): byteMe(): [[ 31 -ge 1024 ]] ++(./linux_check_memory.sh:73): byteMe(): echo 31.2GB +(./linux_check_memory.sh:111): RESULT2='RAW 33555009536 31.2GB'