PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 14, 2022

[FIXED] How to calculate the real value in kb,mb,gb,b in Bash script

 May 14, 2022     bash, ubuntu, unix     No comments   

Issue

I want to convert all values with kb,mb,gb,b to the real bytes, for example, "10kb" should be convert to "10240" bytes.

Here is the test function:

#! /bin/bash

get_value_in_bytes()
{
    local value="$1"
    
    local unit_length=0
    local number_length=0
    local number=0
    
    if [[ "$value" == *"gb" ]]
    then
        unit_length=2
        number_length=$((${#value} - $unit_length))
        number=$((${value:0:$number_length})) * 1024 * 1024 * 1024 || -1
    elif [[ "$value" == *"mb" ]]
    then
        unit_length=2
        number_length=$((${#value} - $unit_length))
        number=$((${value:0:$number_length}))*1024*1024 || -1
    elif [[ "$value" == *"kb" ]]
    then
        unit_length=2
        number_length=$((${#value} - $unit_length))
        number=$((${value:0:$number_length}))*1024 || -1
    elif [[ "$value" == *"b" ]]
    then
        unit_length=1
        number_length=$((${#value} - $unit_length))
        number=$((${value:0:$number_length})) || -1
    else
        number=$((${value:0:$number_length})) || -1
    fi
    
    if (( $number < 0 ))
    then
        echo "error : $value"
    else
        echo "$number : $value"
    fi
}

Here is the test:

get_value_in_bytes 10kb
get_value_in_bytes 10mb
get_value_in_bytes 10gb
get_value_in_bytes 10b
get_value_in_bytes 10
get_value_in_bytes not_number_mb
get_value_in_bytes not_number_gb

Here is the output:

10*1024 : 10kb
10*1024*1024 : 10mb
./test.sh: line 15: test.sh: command not found
./test.sh: line 15: -1: command not found
0 : 10gb
10 : 10b
0 : 10
0*1024*1024 : not_number_mb
./test.sh: line 15: test.sh: command not found
./test.sh: line 15: -1: command not found
0 : not_number_gb

I want this function can accept any values as input, and if the value is not number string, then convert it to -1. But the above function doesn't work as expected.


Solution

Here's a solution based on a bash ERE and a case statement:

get_value_in_bytes() {
    local bytes=-1
    if [[ $1 =~ ^([[:digit:]]+)(b|kb|mb|gb)?$ ]]
    then
        bytes=${BASH_REMATCH[1]}
        case ${BASH_REMATCH[2]} in
            kb) bytes=$((bytes * 1024)) ;;
            mb) bytes=$((bytes * 1048576)) ;;
            gb) bytes=$((bytes * 1073741824)) ;;
        esac
    fi
    printf '%s\n' "$bytes"
}
remarks:
  • Shell arithmetic can only process integers. For floating point calculations you'll have to use an external tool like bc.

  • The b unit is for bit. You should use B when you mean byte.

  • Aside: the units based on 2^10 are KiB MiB GiB etc...



Answered By - Fravadona
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing