views:

38

answers:

1

The mysqladmin command returns the values in bytes. I will like to see the value in MB if it is greater than 1 MB (1048576 bytes).

$ mysqladmin variables
+---------------------------------+----------------------------------+
| Variable_name                   | Value                            |
+---------------------------------+----------------------------------+
| auto_increment_increment        | 1                                |
| auto_increment_offset           | 1                                |
...
| interactive_timeout             | 14400                            |
| join_buffer_size                | 10481664                         |
| key_buffer_size                 | 1073741824                       |

I can save and calculate each variable one at a time. But how do I show all the values in MB?

myval1=$(((`mysqladmin variables | grep '\<key_buffer_size\>' | awk '{print $4}'`)/1048576))
+1  A: 

Off the top of my head, something like this would work:

#!/bin/bash

if [ "$1" = variables ]; then
   /moved/elsewhere/mysqladmin $* | awk '{ sz=$4; if (sz>1048576) 
      { sz=sz/1048576 "Mb"; }
      print $1 $2 $3 sz $5; }'
else
   /moved/elsewhere/mysqladmin $*
fi
symcbean
Interesting! Is it possible to round?
shantanuo
Yes - but awk doesn't have a built-in round function - see http://www.gnu.org/manual/gawk/html_node/Round-Function.html
symcbean
The page says printf can do rounding (though not accurate). Can you let me know how to add printf here?
shantanuo