Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am creating bash script that gives me size of mysql innodb log file in MB. Currently I am getting its output in bytes. Please check it out following script and help to get output in MB.

#! /bin/sh

  a=$(mysql -uroot -proot -e  "SHOW VARIABLES" | grep innodb_log_file_size | cut -f2)
  b=$(echo "$a/$10240" )

 echo {"Innodb_log_file_size":"'$b'"' }
share|improve this question
    
You can directly check du -sh /var/lib/mysql/ib_logfile* –  Rahul Patil Apr 9 '13 at 6:31
    
what about innodb buffer size ? –  K.K Patel Apr 9 '13 at 6:32
add comment

1 Answer

up vote 0 down vote accepted

In bash you could do something like this.

You can use (( )) to enforce an arithmetic context.

When it comes to sizes you have MB vs MiB, see chart on right hand side.

#!/bin/bash

psize()
{
    local name="$1"
    local -i val="$2";
    local u=""

    case "$3" in
    "B") u="B"  ;;
    "K") u="KiB"; ((val /= 1024 ));;
    "G") u="GiB"; ((val /= 1024 * 1024 * 1024 ));;
    # Defaults to MiB
      *) u="MiB"; ((val /= 1024 * 1024 ));;
    esac

    printf "%-22s: %d %s\n" "$name" "$val" "$u"
}

unit=""

# Check if wanted size is given by user
if [[ "$1" =~ ^(B|K|M|G)$ ]]; then
    unit="$1"
fi

# Use tab as delimiter and read into variables "name" and "value"
while IFS=$'\t' read -r name value; do
    case "$name" in
    "innodb_log_file_size"|"innodb_log_buffer_size")
        psize "$name" "$value" "$unit"
        ;;
    esac
done < <(mysql -uroot -ppass -e "SHOW VARIABLES")

With grep

If you want to mix in grep etc.:

psize()
{

    local -i a=$(mysql -uroot -ppass -e "SHOW VARIABLES" | \
            grep innodb_log_file_size | cut -f2)
    local -i b=0;
    local u=""

    case "$1" in
    "B") u="B"  ;   b="$a";;
    "K") u="KiB"; ((b = a / 1024 ));;
    "G") u="GiB"; ((b = a / 1024 / 1024 / 1024 ));;
     *)  u="MiB"; ((b = a / 1024 / 1024 ));;
    esac

    printf "Innodb_log_file_size: %d %s\n" "$b" "$u"
}

unit="M"

if [[ "$1" =~ ^(B|K|M|G)$ ]]; then
    unit="$1"
fi

psize "$unit"

Which you could use by:

./myscript   # Defaults to MiB
./myscript B # Print in bytes
./myscript G # Print in GiB
...

For ref.

Using awk:

psize()
{
awk -v unit="$1" '
BEGIN {
    if (unit == "")
        unit="M"
    switch (unit) {
    case "B":
        ratio = 1;
        break;
    case "K":
        ratio = 1024;
        break;
    case "G":
        ratio = 1024 * 1024 * 1024;
            break;
    default:
        ratio = 1024 * 1024;
        break;
    }
    if (unit != "B")
        unit = unit "iB"
}
/^innodb_log_file_size\s/ {
    printf("Log size   : %d %s\n", $2 / ratio, unit);
}
/^innodb_log_buffer_size\s/ {
    printf("Buffer size: %d %s\n", $2 / ratio, unit);
}
' <(mysql -uroot -e "SHOW VARIABLES" -ppass)
}

psize "$unit"

share|improve this answer
    
I don't want to use awk, Just define new variable b and make that converts bytes sizes in MB and print it. –  K.K Patel Apr 9 '13 at 6:52
    
@ChrisDown: Nope. It is in bytes. –  Sukminder Apr 9 '13 at 7:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.