简洁的想法

仁爱、喜乐、和平、忍耐、恩慈、良善、信实、温柔、节制

Sed 批量替换文件中的字符

| Comments

下载了ruby的Standard Library Documentation, 发现其离线版本的home链接居然是’/’, 这在使用中会指向file:///, 所以还是替换一下吧: 把

1
<a href='/' target='_top' >Home</a>

替换成

1
<a href='index' target='_top' >Home</a>

用这个命令:

1
$ sed -i -e "s/'\/' target/'\/index.html' target/g" *.html

如果想替换前做个备份:

1
$ sed -i.bak -e "s/'\/' target/'\/index.html' target/g" *.html

如果想把当前目录及子目录下的文件都替换掉, 那可以这样写:

1
$ find . -type f | xargs sed -i.bak -e "s/'\/' target/'\/index.html' target/g"

当然这只是个例子, 因为子目录下的html文件中的home链接如果也换成了index.html, 那还是不能链接到父目录中的index.html.

因为换成了octopress, 所以留言也托管到disqus了, 导入留言后, 发现blog下方的留言不能显示, 查看源代码发现链接最后没有斜线 “/”, 我在disqus里面换链接的时候, 设置成都以斜线结尾.

Wordpress to Octopress Rewrite Rules

| Comments

  1. 首先编辑Gemfile, 添加gem ‘rack-rewrite’
1
2
3
4
5
6
7
8
9
10
$ vi Gemfile
source "http://rubygems.org"

group :development do
  gem 'rake'
  ...
end

gem 'sinatra', '1.2.6'
gem 'rack-rewrite'
  1. 在config.ru里面添加rewrite规则
1
2
3
4
5
6
7
8
$ vi config.ru
...
require 'rack-rewrite'

use Rack::Rewrite do
  r301 %r{/\?p=(.*)},  "http://blog.neten.de/posts/$1/"
end
...
  1. 别忘记了更新网站
1
2
3
4
5
$ bundle install
$ rake generate
$ git add .
$ git commit -am "add rewrite"
$ git push heroku master

访问一下Google中网站的旧链接吧, 是不是转到新站上来了呢?

First Blog Using Octopress

| Comments

Octopress 真是太棒了, 可以用vi来写blog, 用git来管理, 相当省心.

  • 首先下载一分代码
1
git clone git://github.com/imathis/octopress.git blog
  • 我为octopress生成一个gems包, 进入目录的同时, 运行rvm use命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vi neten/.rvmrc
# rvm use 1.9.2
rvm use ruby-1.9.2-p290@octopress

$ cd blog
====================================================================================
= NOTICE                                                                           =
====================================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory       =
= This is a shell script and therefore may contain any shell commands.             =
=                                                                                  =
= Examine the contents of this file carefully to be sure the contents are          =
= safe before trusting it! ( Choose v[iew] below to view the contents )            =
====================================================================================
Do you wish to trust this .rvmrc file? (/home/peng/Projects/RubySites/blog/.rvmrc)
y[es], n[o], v[iew], c[ancel]> y

$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

如何使用ActiveRecordStore(数据库)做为session存储方案

| Comments

首先交待环境:

1
2
3
4
$ ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
$ rails -v
Rails 3.2.2

网上一大堆过期信息, 说是可以用下面的方法, 结果如下:

1
2
3
$ rake db:session:create
rake aborted!
Don't know how to build task 'db:session:create'

其实大家找一下session的配置文件就可以发现正确方法了:
config/initializers/session_store.rb

1
2
3
4
5
6
7
8
# Be sure to restart your server when you modify this file.

Neten::Application.config.session_store :cookie_store, key: '_neten_session'

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rails generate session_migration")
# Neten::Application.config.session_store :active_record_store

正确的方法是运行

1
$ rails generate session_migration

或者用原来的命令, 不过session要加个s:
1
$ rake db:sessions:create

再把session_store.rb最后一行去掉注释, 注释掉上面那行用cookie的, 最后运行:

1
$ rake db:migrate

别忘了重启服务器。

Mysql 改变数据库目录不能启动

| Comments

mysql默认的目录是/var/lib/mysql, 修改该目录看起来很简单:

1
2
3
$ sudo vi /etc/mysql/my.cnf
#datadir = /var/lib/mysql
datadir = /home/neten/mysql

没想到 /etc/init.d/mysql restart 的时候启动不了了。

查看 log 是个习惯:

1
$ tail -f /var/log/syslog

发现一堆这种信息:

1
2
3
4
5
Mar 23 16:00:40 HP kernel: [ 3898.064351] type=1400 audit(1332514840.079:153): apparmor="STATUS" operation="profile_replace" name="/usr/sbin/mysqld" pid=10919 comm="apparmor_parser"
Mar 23 16:00:40 HP kernel: [ 3898.087415] type=1400 audit(1332514840.103:154): apparmor="DENIED" operation="mknod" parent=1 profile="/usr/sbin/mysqld" name="/home/neten/mysql/HP.lower-test" pid=10923 comm="mysqld" requested_mask="c" denied_mask="c" fsuid=0 ouid=0
Mar 23 16:00:40 HP kernel: [ 3898.087528] type=1400 audit(1332514840.103:155): apparmor="DENIED" operation="mknod" parent=1 profile="/usr/sbin/mysqld" name="/home/neten/mysql/HP.lower-test" pid=10923 comm="mysqld" requested_mask="c" denied_mask="c" fsuid=0 ouid=0
Mar 23 16:00:40 HP kernel: [ 3898.090694] type=1400 audit(1332514840.107:156): apparmor="DENIED" operation="open" parent=1 profile="/usr/sbin/mysqld" name="/home/neten/mysql/mysql/plugin.frm" pid=10923 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=115 ouid=115
Mar 23 16:00:40 HP kernel: [ 3898.112522] type=1400 audit(1332514840.127:157): apparmor="DENIED" operation="open" parent=1 profile="/usr/sbin/mysqld" name="/home/neten/mysql/ibdata1" pid=10923 comm="mysqld" requested_mask="rw" denied_mask="rw" fsuid=115 ouid=115

看来是apparmor不认识新朋友, 那就给介绍一下吧:

1
2
3
4
$ sudo vi /etc/apparmor.d/usr.sbin.mysqld

/home/neten/mysql/ r,
/home/neten/mysql/** rwk,

重启一下, 一切OK了吧:

1
2
$ sudo /etc/init.d/apparmor restart
$ sudo /etc/init.d/mysql restart

Ubuntu Precise 12.04 无线网卡 慢 Broadcom Brcmsmac

| Comments

标题没看懂是正常的, 我写的全是关键字。

装12.04后, 无线网卡网速超级慢。 Google后发现解决方法

If the output of:

1
lspci -k | grep  brcmsmac

includes:

1
Kernel driver in use: brcmsmac

Then you have this problem too.

To solve this, you’ll need to install broadcom-sta driver and blacklist the default kernel one.

1
sudo apt-get install bcmwl-kernel-source

Then open /etc/modprobe.d/blacklist-bcm43.conf and add these lines:

1
2
blacklist brcmsmac
blacklist bcma

Restart and check that:

1
lspci -k | grep  wl

includes:

1
Kernel driver in use: wl

That should fix your wifi.
Hope ubuntu devs fix this and push out updates soon.

用matlab求应力Sigma

| Comments

工字钢沿Z轴向下,
Lz是nx1的Array
Mz是nx2的Vector, Mz(:,1)是x向, Mz(:,2)是y向:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    % Sigma of 9 points
    %     4 ___              ___ 3  
    %       \  \             \  \
    %        \  \8__________7_\  \ 
    %  Y+/____\_________9         \
    %    \     \   ______\_______  \
    %           \  \ 5    \     6\  \
    %            \__\      \      \__\
    %           1          _\/         2
    %                        X
    %
    % 9个点坐标
    Points = [...
             B,    H/2;
             B,   -H/2;
            -B,   -H/2;
            -B,    H/2;
             t/2,  h/2;
             t/2, -h/2;
            -t/2, -h/2;
            -t/2,  h/2;
             0,    0;
          ];
    Sigma = ...
      repmat(Lz / area,1,9) ...
    + repmat(Mz(:,1),1,9) ...
        .* repmat(Points(:,2),1,length(Mz))' / obj.smaIx ...
    - repmat(Mz(:,2),1,9) ...
        .* repmat(Points(:,1),1,length(Mz))' / obj.smaIy;

下面附带是个实验:

1
2
3
         a =repmat([1:20]',1,9);
         b =repmat([1:9]',1,20);
         a .* b'

没有配置用户名的时候git报错信息

| Comments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[master 1861d1a] SomeDocuments
 Committer: Neten <neten@HP.(none)>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1580 files changed, 205989 insertions(+), 0 deletions(-)

SparkleShare

| Comments

1
2
3
4
5
lsb_release -sc
lucid

git --version
git version 1.7.7

我想安装SparkleShare:

1
2
3
4
mkdir -p ~/.ssh
sudo add-apt-repository ppa:warp10/sparkleshare
sudo apt-get update
sudo apt-get install sparkleshare libwebkit1.1-cil

杯具了:

1
2
3
4
5
6
7
8
9
10
11
12
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
git-core 已经是最新的版本了。
有一些软件包无法被安装。如果您用的是 unstable 发行版,这也许是
因为系统无法达到您要求的状态造成的。该版本中可能会有一些您需要的软件
包尚未被创建或是它们已被从新到(Incoming)目录移出。
下列信息可能会对解决问题有所帮助:

下列软件包有未满足的依赖关系:
  sparkleshare: 依赖: git (>= 1.7.1) 但无法安装它
E: 破损的软件包

OK, 升级Git

1
2
3
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git-core

On the server

1
$ git init --bare /home/neten/MyBackup.git

On the client(s)
1, 再装一次:

1
2
sudo apt-get install sparkleshare libwebkit1.1-cil
cd /home/neten

2, Install the SSH client:

1
$ sudo apt-get install openssh-client

3, Generate an RSA key pair used to authenticate with the server (instead of using a password):

1
$ ssh-keygen

(accept defaults and leave password blank here)

4, Send your RSA key to your server*(neten is my Username):

1
$ ssh-copy-id neten@192.168.1.2

5, Now you can

1
$ sparkleshare start

and a Dialog box should appear.

6, Enter your name and e-mail, click next.

7, Tick “On my own server:” and enter neten@192.168.1.2 in the box there.

8, In the “Folder Name” field, enter the full server side path to the git repo you created on your server (maybe something like /home/neten/MyBackup.git

9, click Sync