Saturday, February 7, 2009

substring operation with bash

You can get a substring (substr) value of an string variable as following in bash.
Syntax is simple ${VARIABLE_NAME:start:length}

let's assume we have a string variable called mystring and it has the value "test".


If you want to access first character of the string you can use following statement.

now c value contains "t" character.


Now c contains "st" substring.

Hint: You can calculate the length of a string value as follows.


Friday, February 6, 2009

Mysql General Query Log

Before Mysql version 5.1.6, you can only log to files what mysqld was doing. 5.1.6 comes with an option where it let's you keep this log in a table called mysql.general_log. Even further, version 5.1.12 came with a new ability to let you disable general query logging on the fly.

You can enable/disable mysql general query logging by setting global variable "general_log".For example following command, starts general logging on the fly.

SET GLOBAL general_log = 'ON';

You have to options, either you can log the queries on in a file where it was the only option before version 5.1.6 , or you can log in the table I mentioned above called mysql.general_log by using global variable "log_output".
The following statement tells mysql to log queries in table mysql.general_log instead of a file.

set global log_output='table';

In any time, you can truncate the table to delete it's contents.

After logging the queries, you can see them by simply using a select statement.

select * from mysql.general_log;

to make it more readable you can use \G at the end of the query.
select * from mysql.general_log\G;

arrays in bash shell

BASH shell is very powerful script language. In my previous blog posts, I give some detail about bash regular expression support. In this post, I will give some information about bash array support.
Declaring an array is very simple in bash. Here is some examples.

# declared an array named called "array" and containing elements.
# "This", "is", "an", "array"
array=(This is an array)

# declaring an array called a by giving elements one by one.

# get output of an df -h

myarray=($(df -h))

Reading value element of an array is in following syntax ${array[#index_number]}. Please note the curly bracket notation.

# print element number 2 of array called myarray
echo ${myarray[2]}

# print all elements
echo ${myarray[*]}

# print index numbers of array
echo ${!myarray[*]}

# number of elements in array
echo ${#myarray[*]}

Here is a simple script using arrays. It simply reports the file systems where disk usage is
over 90% percent.


(df -h|grep "^/") | while read d; do
array=( $d )
if [ ${array[4]} -gt 90 ]; then
echo "${array[5]} is over limit. current size is ${array[4]}%"
#do whatever you want here, for example send an email to warn sysadmin about high disk usage.

Tuesday, February 3, 2009

Solaris pargs command

Solaris pargs gives you environment and arguments information for any given process id. If you pass just a process id (pid) to pargs, it gives you all argument variables of the running process. If you need to have arguments as a single line for using it with for example on shell use -l argument with it. Also pargs can give you environment information of any running process if you run it with -e parameter.
Sometimes this utility can be handy if you cannot see the parameters or environment variables of a process with other methods like ps or any other well known utilities.