.bashrc in Mac

Mac doesn’t read ~/.bashrc file when a shell starts. The order of files the shell read is  (reference:  http://hayne.net/MacDev/Notes/unixFAQ.html#shellStartup)

  1. /etc/profile
  2. ~/.bash_profile or ~/.bash_login or ~/.profile (only one of these files will be read, if it exists. If more than one file exist, it will only read the first existing one by the above order)

So one way to make Mac work with ~/.bashrc is to add the line

source ~/.bashrc

in ~/.bash_profile

Posted in PC problem | Leave a comment

screen control of a Mac

open  vnc://your-mac-ip-address:5900
Posted in Uncategorized | Leave a comment

wi-fi setup through command line

http://blog.mattcrampton.com/post/64144666914/managing-wifi-connections-using-the-mac-osx

Turn off wifi on your macbook from the Mac OSX terminal command line:

networksetup -setairportpower en0 off

Turn on wifi on your macbook from the Mac OSX terminal command line:

networksetup -setairportpower en0 on

List available wifi networks from the Mac OSX terminal command line:

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport scan

Join a wifi network from the Mac OSX terminal command line:

networksetup -setairportnetwork en0 SSID

If the wi-fi requires a password

networksetup -setairportnetwork en0 SSID WIFI_PASSWORD

If the wifi requires both username and password

networksetup -setairportnetwork en0 SSID USERNAME WIFI_PASSWORD

Find your network interface name

networksetup -listallhardwareports
Posted in Uncategorized | Leave a comment

MySQL

install MySQL Community Server on mac

  • Go here to download the appropriate version of MySQL Community Server for Mac. Install it.
  • Go here to download the appropriate version of Python connector for MySQL. Install it.
  1. set password for root user for localhost
    • Syntax is
       
          cgao$ mysqladmin -u root password newpassword
      
    • To change it later, the syntax is 
       
          cgao$ mysqladmin -u root password newpassword -p
      
  2. transfer database remotely from machine A to machine B
    • Assume there is a table called mydb@localhost at A; there is no database called mydb@localhost at B.  The ip address of B is BBB.BBB.BBB.BBB
    • First, you need to add a new administrator account on B.
       
          mysql> CREATE USER 'cago'@'localhost' IDENTIFIED BY 'some_passwd';
          mysql> GRANT ALL PRIVILEGES ON *.* TO 'cgao'@'localhost'
                 -> WITH GRANT OPTION;
          mysql> CREATE USER 'cage'@'%' IDENTIFIED BY 'some_passwd';
          mysql>a GRANT ALL PRIVILEGES ON *.* TO 'cgao'@'%'
                 -> WITH GRANT OPTION;
      
    • Then on Machine A, you can remotely create mydb on Machine B through the account you just created on B, using mysqladmin. This step is necessary because you cannot remotely do mysqladmin with Machine B’s root account.
       
          cgao$ mysqladmin -h BBB.BBB.BBB.BBB create mydb -u cgao -p
      
    • Lastly, on Machine A, copy mydb@localhost on A to mydb@BBB.BBB.BBB.BBB on B, using mysqldump
       
          cgao$ mysqldump --user=root --password=yourrootpwdonMachineA mydb | mysql -h BBB.BBB.BBB.BBB mydb -u cgao -p
      
  3. Connection
    • Connect to mysql using local machine
       
          cgao$ mysql [-h localhost] -u root -p
      
    • Connect to mysql using remote machine: you cannot connect as root. First you need to create a user account (as mentioned above)
       
          cgao$ mysql -h xx.xxx.xx.xxx -u cgao -p
      
    • If you want to use your machine’s ip address to connect to local mysql database, you need to force a TCP/IP connection locally
       
          cgao$ mysql -h xx.xxx.xx.xxx -P3306 --protocol=tcp -u …
      
  4. Query the database sizes
     
        mysql> SELECT table_schema "Data Base Name", 
            -> SUM(data_length + index_length) / 1024 / 1024 
            -> "Data Base Size in MB" 
            -> FROM information_schema.TABLES GROUP BY table_schema; 
    
  5. Add a column to a existing database, and update the column value
    • For each of my table, I have a column “time”, which is varchar() type like “Tue Aug  7 21:42:46 2012”.  I would like to add a column with datetime type. (In the future, I’d better make the design better from the beginning, rather than change it later. It takes tremendous amount of time to modify a table…)
    • It is straight forward to add a column in a table
       
          mysql> ALTER TABLE mytable ADD datetime datetime;
      
    • To update the datetime column value, I need to use UPDATE syntax. Since the new “datetime” column takes the old “time” column and convert it form string into datetime, I need to use “STR_TO_DATE” method provided by mysql.One should be careful when formatting the time date. Python’s striftime and MySQL’s STR_TO_DATE use DIFFERENT reference code/specifier!!! (see the links) Be careful with the minutes specifier. Most of the other specifiers are same.Another issue for using STR_TO_DATE is about the ending extra characters. The official document say extra characters at the end of str are ignored.
        
          mysql> SELECT STR_TO_DATE('09:30:17a','%h:%i:%s');
      

      The result is  09:30:17
      However when we use it like

        
           mysql> UPDATE mytable SET 
               -> datetime = STR_TO_DATE('09:30:17a','%h:%i:%s') 
               -> WHERE datetime IS NULL; 
      

      It will report error

        
          ERROR 1292 (22007): Truncated incorrect date value: 
      

      So I need to use “SUBSTRING_INDEX” to removing my ending characters. In my case, if there are ending extra characters, the first should be “,”. So my code is like this:

        
          mysq> UPDATE mytable SET 
              -> datetime = STR_TO_DATE
              -> (SUBSTRING_INDEX(time, ',',1), '%a %b %d %T %Y') 
              -> WHERE datetime IS NULL; 
      
    •  And this is not everything. I have a board called ‘database’, a board called ‘e-sports’. I am not able to update datetime column for these two boards, even if I used the backtilt “, which works for other MySQL methods.
  6. To show the engine status
       
        mysql> SHOW ENGINE INNODB STATUS\G; 
    

    To kill a process

       
        mysql> kill [thread_id]; 
    
  7. space holder
  8. space holder
  9. space holde
Posted in Uncategorized | Leave a comment

[zz] Homebrew: ruby bad interpreter: No such file or directory

Today when I try to install quantlib (and boost) on my laptop, I got the error message for running

macbook:~ cgao$ brew install boost

/usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby: bad interpreter: No such file or directory

/usr/local/bin/brew: line 23: /usr/local/Library/brew.rb: Undefined error: 0

After some search, I came to this website and got the problem solved:

http://blog.ic3man.gr/2014/06/homebrew-ruby-bad-interpreter-no-such-file-or-directory/

After installing 10.10 DP1 Yosemite (yeah I couldn’t resist the temptation) homebrew gave me the following error:

/usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby: bad interpreter: No such file or directory

This error can easily be fixed in the following steps:
1) Open terminal

2) nano /usr/local/Library/brew.rb

3) In the first line change “1.8″ to “Current”, so it should look like this:

#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby -W0

Cheers~

Posted in Uncategorized | 1 Comment

remove files in bash

I have files image1, …, image5.

I want to rename them as image_test_2,…., image_test_6

 for i in $(seq 5 -1 1); do mv -i image$i image_test_$[i+1]; done

Do it in reversing way to avoid overwritting. And better use “-i” to check overwrite.

 

 

 

 

Posted in PC problem | Leave a comment

xcov and xcorr in Matlab

Matlab provides xcorr to calculate the cross/auto-correlation of two/one random variables.

[r,lags]=xcorr(x,y,’coeff’)

will calculate the cross-correlation of x and y using
R_m(x,y)=\Sigma_{n=0}^{N-m-1}x_{n+m}*y_n/\sqrt{\Sigma_{i=0}^{N-1}x_i^2*\Sigma_{i=0}^{N-1}y_i^2}

Here “coeff” optional keyword is used to normalize the result with L2-norm.

However
[c,lags]=xcov(x,y,’coeff’)

calculates the cross-variance of x and y using
C_m(x,y)=E[(x-E[x])_{m}*(y-E[y])]/\sqrt{Var[x]*Var[y]}=\frac{N-1}{N}\Sigma_{n=0}^{N-m-1}(x_{n+m}-\mu_x)*(y_n-\mu_y)/\sqrt{\Sigma_{i=0}^{N-1}(x_i-\mu_x)^2*\Sigma_{i=0}^{N-1}(y_i-\mu_y)^2}

Here “coeff” keyword will normalize the result with the variance of X and Y. The factor \frac{N-1}{N} comes from the unbiased variance

\sigma_x^2=\frac{1}{N-1}\Sigma_0^{N-1}(x_i-\mu_x)^2
\mu_x=\frac{1}{N}\Sigma_0^{n-1}x_i

Posted in Academic | Leave a comment