Posts Tagged ‘tutorial’

solve the problem of no output in console on windows eclipse with MinGW

October 14th, 2011

If you are experiencing the problem of no output in console on windows eclipse with MinGW, here is the solution.

And actually the solution the is very simple, you only have to add a PATH variable to your MinGW bin directory in the run configuration.

Here are the steps:

From menu: Run -> Run Configuration…, open the run configuration window, and add the PATH variable to your MinGW bin directory (as shown in the picture below).

bash script for killing all mysql processes / connections

September 29th, 2011

Here is a small bash script for killing all mysql processes (or connections).

The basic idea is to select all process IDs into a tmp file (each process ID a line), and then read the file line by line and kill the process, finally remove the tmp file.

NOTE:
This bash only works for MYSQL >= 5.1.7. Becuase before 5.1.7 process list is not stored in the table INFORMATION_SCHEMA.PROCESSLIST.

USAGE:

  • either by providing the MYSQL root password as a parameter to the bash as:
    user> mysql_kill_all.sh MysqlRootPass
  • or by editing the password the line mysqlpass=$1 as:
    mysqlpass=MysqlRootPass and then excute the bash without parameter:
    user> mysql_kill_all.sh.

    Of course, you can also edit the other variables in the bash file.

——————————-
SOURCE FILE: mysql_kill_all.sh
——————————-

#!/bin/bash

# mysql host/user/pass who has the privilege to read schema INFORMATION_SCHEMA
mysqlhost=localhost
mysqluser=root
mysqlpass=$1
# tmp file path
tmpprocesslistfile=/tmp/mysql_processlist.txt

# select all process IDs into the tmp file. each line has a process id.
echo "SELECT id FROM INFORMATION_SCHEMA.PROCESSLIST INTO OUTFILE '$tmpprocesslistfile';" | mysql -h$mysqlhost -u$mysqluser -p$mysqlpass INFORMATION_SCHEMA

# read the file line by line and kill the process
while read qryid
do
echo "kill $qryid;" | mysql -h$mysqlhost -u$mysqluser -p$mysqlpass
done < "$tmpprocesslistfile"

# remove the tmp file
echo 'All connections have been killed.';
echo 'Please enter sudo password to remove the tmp file if necessary.';
sudo rm $tmpprocesslistfile

Install & use mysql++ on linux(debian squeeze, ubuntu)

July 28th, 2011

The key to use mysql++ on debian/ubuntu system is to find where the mysql++ .h head files and the .so binary files.

Firrst install mysqlclient and mysql++ on debian squeeze:

root# apt-get install libmysqlclient-dev libmysqld-dev libmysql++-dev libmysql++-doc libmysql++3

This will install the mysql head files in /usr/include/mysql, mysql++ head files in /usr/include/mysql, and libmysqlpp.so and libmysqlclient.so in /usr/lib. These are all the files we need.

Now let us use the mysql++ library.

Save the simple code below as test.cpp

#include <iostream>
#include <mysql++.h>

int main() {

mysqlpp::String s(“hello, world”);

std::cout << s << std::endl;

return 0;

}

And save the makefile as Makefile:

CXX := g++
CXXFLAGS := -I/usr/include/mysql -I/usr/include/mysql++

LDFLAGS := -L/usr/lib -lmysqlpp -lmysqlclient -lnsl -lz -lm
EXECUTABLE := main

all: test
clean:        rm -f $(EXECUTABLE) *.o

Finally, excute

make all

The compile result would be test.

bash commands

November 12th, 2010

1. for loop

for (( EXP1; EXP2; EXP3 ))
do
	command1
	command2
	command3
done

eg:

#!/bin/bash
for (( i=0; i<5; i++ ))
do
	echo "Hello $i..."
done

2. if / else / fi statements

basic grammar (note the spaces between the [ ]):

if [ EXP ]; then
	statements...
else
	statements 2...
fi

eg:

step=5
alpha=$(echo "scale=1; $step/10" | bc)
if [ $(echo "$alpha<1" | bc) -eq 1 ]; then
	alpha=0$alpha
fi

2.1 Arithmetic Comparisons

-lt <
-gt >
-le <=
-ge >=
-eq ==
-ne !=

2.2 String Comparisons

= equal
!= not equal
< less then
> greater then
-n s1 string s1 is not empty
-z s1 string s1 is empty

3. real number calculation

(echo "scale=2; EXP" | bc)

where scale=2 sets the number of the digits after decimal point.
eg1:

a=1;b=2;
c=$(echo "scale=2; $a/$b" | bc)
echo $c

output:

.50

eg2:

exp="1223/234";
d=$(echo "scale=2; ${exp}" | bc)
echo $d

output:

5.22

reference: http://linuxconfig.org/Bash_scripting_Tutorial

4. functions

basic grammar:

function div() {
    STATEMENTS
    return XXX
}>
NOTE: A bash function can only return a number. There is no way to return directly a string. So we need to use $() to capture the result outputted in the function body.
eg:
# ----------------------------------------------
# return $1/$2 with 1 digit after decimal point.
# ----------------------------------------------
function div() {
   res=$(echo "scale=1; $1/$2" | bc)
   if [ $(echo "$res<1" | bc) -eq 1 ]; then
      res=0$res
   fi
   echo $res
}
# use the function
step=5
alpha=$(div $step 10)