Tuesday, December 30, 2008

Ubuntu 8.10 virtualbox transparent bridge configure

# 替换下面所有的david为实际的ubuntu用户名
$> sudo apt-get install uml-utilities
$> sudo gpasswd -a david uml-net
$> sudo apt-get install bridge-utils
# 重启ubuntu

$> sudo chmod 0666 /dev/net/tun #设置访问权限
$> sudo tunctl -t tap0 -u david #建立一个tap设备, 名字为tap0, 所有者为david
$> sudo ifconfig eth0 0.0.0.0 promisc #使eth0进入promiscuous模式
$> sudo ifconfig tap0 0.0.0.0 promisc #使tap0进入promiscuous模式
$> sudo brctl addbr br0 #增加一个网桥
$> sudo brctl addif br0 eth0 #将eth0加入网桥
$> sudo ifconfig eth0 up #激活eth0
$> sudo dhclient br0 #为br0设置IP地址
$> sudo brctl addif br0 tap0 #将tap0加入网桥
$> sudo ifconfig tap0 up #激活tap0

$> sudo /etc/init.d/networking restart
这个操作之后br0就应该会从局域网分配到ip地址,eth0和tap0则经由br0链到网络,不过这样机器重启之后这些配置就没有了,所以可以把这些信息写到/etc/network/interfaces中,
$> more /etc/network/interfaces

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

auto tap0
iface tap0 inet manual
tunctl_user david

# The primary network interface - use DHCP to find our address
auto br0
iface br0 inet dhcp
bridge_ports eth0 tap0

up ifconfig tap0 0.0.0.0 up
up ifconfig eth0 0.0.0.0 up

然后
$> sudo /etc/init.d/networking restart
就可以,并且下次重启之后br0和tap0会自动被激活,ifconfig命令可以看到如下类似信息。
$> ifconfig
br0 Link encap:Ethernet HWaddr 00:21:97:12:e5:9c
inet addr:192.168.1.226 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::221:97ff:fe12:e59c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:145 errors:0 dropped:0 overruns:0 frame:0
TX packets:33 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:28802 (28.8 KB) TX bytes:4858 (4.8 KB)

eth0 Link encap:Ethernet HWaddr 00:21:97:12:e5:9c
inet6 addr: fe80::221:97ff:fe12:e59c/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:3205 errors:0 dropped:0 overruns:0 frame:0
TX packets:965 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:478611 (478.6 KB) TX bytes:146887 (146.8 KB)
Interrupt:17

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:183 errors:0 dropped:0 overruns:0 frame:0
TX packets:183 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:15553 (15.5 KB) TX bytes:15553 (15.5 KB)

tap0 Link encap:Ethernet HWaddr 22:7c:6a:b3:1d:b8
inet6 addr: fe80::207c:6aff:feb3:1db8/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:2294 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

这个配置在机器重起后,虽然已经可以上网,但是eth0其实没有分配ip,所以进入ubuntu桌面后,会看到右上角的network disabling的网络链接。
以上是基于virtualbox2.0.4设置的,新版的2.1.0设置应该更简单,不需要设置tap0。
最后设置virtualbox:启动virtualbox,在主界面上选中要使用刚才建立的虚拟网络接口tap0的虚拟机,点“设置”,在弹出的窗口中选“网络”,选中其中一块网卡(通常为“网络适配器 0”),选中“启用网络适配器”,“连接到”后面选“Host Interface”,选中“接入网线”,然后在“主机网络界面名称”中填入刚才建立的虚拟网络接口的名字“tap0”,确定。
Reference: http://download.virtualbox.org/virtualbox/2.0.4/UserManual.pdf
http://forum.ubuntu.org.cn/viewtopic.php?t=63120
http://linux.chinaunix.net/bbs/thread-1029090-1-1.html

Saturday, December 27, 2008

clear history command when logout linux

$> vi .bash_logout
# ~/.bash_logout

history -c
clear

Reference:
http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/advanced/node125.html

linux auto logout setting

$> cd
$> vi .profile
# user will be auto logout after 300 seconds.
export TMOUT=300

如果是针对所有用户,则修改/etc/profile文件。

添加deflate gzip模块到apache2服务器

$> ./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=all --enable-modules=all
Configuring Apache Portable Runtime Utility library...

checking for APR-util... yes
configure: error: Cannot use an external APR-util with the bundled APR

如果编译过程中发现APR-util的错误,可以加上--with-included-apr这个参数,如下:
$> ./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=all --enable-modules=all --with-included-apr
$> make
$> cp ./modules/filters/.libs/mod_deflate.so /usr/local/apache2/modules
$> vi /usr/local/apache2/conf/httpd.conf
# append 2 line to httpd.conf
LoadModule deflate_module modules/mod_deflate.so
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/x-json application/x-javascript
$> /usr/local/apache2/bin/apachectl graceful

更多关于deflate module的设置可参考官方说明:http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

Reference:
http://yuweijun.blogspot.com/2007/09/apache22module.html

Thursday, December 25, 2008

使用arping查询得到局域网里其他机器的mac地址

#!/bin/bash
# 使用arping查询得到mac地址
for i in `seq 254` ; do
sudo /sbin/arping -c2 192.168.1.$i | awk '/Unicast reply from/{print $4,$5}' | sed 's/\[//' | sed 's/\]//'
done

#!/bin/bash
for ((i = 1; i < 254; i++))
do
sudo /sbin/arping -i eth0 192.168.1.$i -c 1
done
/sbin/arp -a > mac_table

Tuesday, December 23, 2008

在搜索引擎上查询网站的收录链接信息(SEO)

1. http://www.sogou.com
site:http://www.php.net/
link:http://www.php.net/

2. http://www.google.cn
link:http://www.php.net/ 不准确
site:http://www.php.net/
intext:http://www.php.net
www.php.net -site:www.php.net
注意,该命令反馈的链接中可能包括那些仅仅提及你的URL而实际上并没与你做链接的网站。
http://www.google.com/search?q=php.net&as_qdr=d1
http://www.google.com/search?hl=en&q=site%3Aphp.net&as_qdr=m

3. http://www.baidu.com
site:(php.net)

4. http://www.yahoo.cn/中直接输入网址可以查询网页收录数量
如直接输入:http://www.php.net/

5. 其他一些工具:
http://sitemap.cn.yahoo.com
http://indexed.webmasterhome.cn/?domain=php.net
http://siteexplorer.search.yahoo.com/
http://tool.admin5.com/
http://tool.chinaz.com/
http://www.yisou.com/search:site%3Ahttp%3A//www.php.net

IE6/IE7上ajax请求的一个bug

UTF-8编码的页面,在IE6/IE7中用ajax方式和普通直接访问方式请求此url,结果是不一样的:
http://localhost/test.php?q=测试中文

ajax: http://localhost/test.php?q=测试中文
服务器端收到的请求,其中的中文参数是以ISO-8859-1编码的(浏览器的默认编码方式),所以服务器端收到的参数其实是乱码的,所以ajax请求URL中的参数一定要经过encode,除了ie之外,其他浏览器测试下来都会自动url encode其中的参数。

非ajax: http://localhost/test.php?q=%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87
服务器端收到正常的UTF-8编码后的参数。

ie中空白渲染的问题

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<form action="test_submit" method="get" accept-charset="utf-8">
<input type="text" name="some_name" value="" id="name">
<div id="x" style="white-space:pre;">x xx x</div>
<div id="y">y yy y</div>
<p><input type="button" value="click here" onclick="document.getElementById('name').value=document.getElementById('x').innerHTML"></p>
<p><input type="button" value="click here" onclick="document.getElementById('name').value=document.getElementById('y').innerHTML"></p>
</form>
</body>
</html>

上面的例子在ie6/ie7中测试时可发现y组成的字符串,其中的空格被ie渲染过后,取到的innerHTML已经变为一个空格了,在firefox/safari上在渲染后看上去是只有一个空格,但innerHTML取到的还是与原码是保持一致的。

install ruby-mysql gem in mac osx 10.5

首先用dmg包安装的mysql(/usr/local/mysql),来安装mysql-ruby gem包:
$> sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions. This could take a while...
Successfully installed mysql-2.7
1 gem installed

$> sudo gem install dbd-mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
Successfully installed dbd-mysql-0.4.2
1 gem installed
Installing ri documentation for dbd-mysql-0.4.2...
Installing RDoc documentation for dbd-mysql-0.4.2...
看上去是装成功了,但实际使用时却抛出了以下错误:
dyld: lazy symbol binding failed: Symbol not found: _mysql_init
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle
Expected in: dynamic lookup

这个主要是因为平台原因造成的,分别查看dmg包和二进制包的mysql_config可看到-arch的差异:
$> /usr/local/mysql/bin/mysql_config
Usage: /usr/local/mysql/bin/mysql_config [OPTIONS]
Options:
--cflags [-I/usr/local/mysql/include -Os -arch ppc -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL]
--include [-I/usr/local/mysql/include]
--libs [-L/usr/local/mysql/lib -lmysqlclient -lz -lm]
--libs_r [-L/usr/local/mysql/lib -lmysqlclient_r -lz -lm]
--socket [/tmp/mysql.sock]
--port [0]
--version [5.1.23-rc]
--libmysqld-libs [-L/usr/local/mysql/lib -lmysqld -lz -lm]
$> /usr/local/mysql5/bin/mysql_config
Usage: /usr/local/mysql5/bin/mysql_config [OPTIONS]
Options:
--cflags [-I/usr/local/mysql5/include -g -Os -arch i386 -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT]
--include [-I/usr/local/mysql5/include]
--libs [-L/usr/local/mysql5/lib -lmysqlclient -lz -lm -lmygcc]
--libs_r [-L/usr/local/mysql5/lib -lmysqlclient_r -lz -lm -lmygcc]
--socket [/tmp/mysql.sock]
--port [0]
--version [5.0.67]
--libmysqld-libs [-L/usr/local/mysql5/lib -lmysqld -lz -lm -lmygcc]

而mac osx 10.5的macbook则是i386的:
$> uname -a
Darwin Macintosh.local 9.5.0 Darwin Kernel Version 9.5.0: Wed Sep 3 11:29:43 PDT 2008; root:xnu-1228.7.58~1/RELEASE_I386 i386

所以用i386平台编译的mysql5来编译安装mysql-ruby包:


$> sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql5/bin/mysql_config
Building native extensions. This could take a while...
Successfully installed mysql-2.7
1 gem installed

$> sudo env ARCHFLAGS="-arch i386" gem install dbd-mysql -- --with-mysql-config=/usr/local/mysql5/bin/mysql_config
Successfully installed dbd-mysql-0.4.2
1 gem installed
Installing ri documentation for dbd-mysql-0.4.2...
Installing RDoc documentation for dbd-mysql-0.4.2...

这样才能正确安装上ruby-mysql和dbd-mysql。

在ubuntu 8上安装字体

将字体复制到truetype目录中:
$> sudo mv Desktop/droids /usr/share/fonts/truetype/droids
然后更新字体缓存:
$> sudo fc-cache

Reference:
http://www.fwolf.com/blog/post/170

linux 用户登录时的欢迎词修改

$> vi /etc/motd

Monday, December 22, 2008

php中self关键字说明

self一般指向当前类的静态方法和常量,用self::加方法名和常量名方式引用。
$this则是指向当前类的实例对象,用$this->加方法名和实例变量方式引用。
在一些参数为callback的方法里,可以用字符串'self'形式指向当前类,而不要直接用self,如call_user_func('self', $method)中。
另外self引用的总是当前类的方法和常量,子类调用父类的静态方法,其中的父类方法中的self仍是指向父类本身的,如果子类的同名方法覆盖了父类方法,则可以用parent::来引用父类方法。

<?php
interface AppConstants {
const FOOBAR = 'Hello, World.';
}

class Example implements AppConstants {
public function test() {
echo self :: FOOBAR;
}
}

$obj = new Example();
$obj->test(); // outputs "Hello, world."

class MyClass {
const NAME = 'Foo';

protected function myFunc() {
echo "MyClass::myFunc()\n";
}
static public function display() {
echo self :: NAME;
}
static public function getInstance() {
$instance = new self;
return $instance;
}
}

class ChildClass extends MyClass {
const NAME = 'Child';

// Override parent's definition
public function myFunc() {
// But still call the parent function
parent :: myFunc();
echo "ChildClass::myFunc()\n";
}
}

$class = new ChildClass();
$class->myFunc();


echo('Class constant: ');
ChildClass :: display();
echo('Object class: ');
echo(get_class(ChildClass :: getInstance()));
?>

== output ==
Hello, World.
MyClass::myFunc()
ChildClass::myFunc()
Class constant: Foo
Object class: MyClass
另外可以再参考一个php 5.3.0的运行时绑定(迟绑定)的用法,地址是:
http://cn2.php.net/oop5.late-static-bindings

在ubuntu 8.10 上安装passenger 2.0.6

按照http://www.rubyenterpriseedition.com/download.html#ubuntu上的说明,在ubuntu8.10上安装passenger时报以下错误:
Welcome to the Phusion Passenger Apache 2 module installer, v2.0.6.

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

Here's what you can expect from the installation process:

1. The Apache 2 module will be installed for you.
2. You'll learn how to configure Apache.
3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.

Checking for required software...

* GNU C++ compiler... found at /usr/bin/g++
* Ruby development headers... found
* OpenSSL support for Ruby... found
* RubyGems... found
* Rake... found at /opt/ruby-enterprise/bin/rake
* Apache 2... found at /usr/sbin/apache2
* Apache 2 development headers... found at /usr/bin/apxs2
* Apache Portable Runtime (APR) development headers... found at /usr/bin/apr-1-config
* Apache Portable Runtime Utility (APR) development headers... found at /usr/bin/apu-1-config
* fastthread... found
* rack... found
--------------------------------------------

--------------------------------------------
Compiling and installing Apache 2 module...
cd /opt/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-2.0.6
/opt/ruby-enterprise/bin/ruby -S rake clean apache2
/usr/bin/rake:27:in `require': no such file to load -- rake (LoadError)
from /usr/bin/rake:27

--------------------------------------------
It looks like something went wrong

Please read our Users guide for troubleshooting tips:

/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-2.0.6/doc/Users guide.html

If that doesn't help, please use our support facilities at:

http://www.modrails.com/

We'll do our best to help you.

在/usr/bin/rake文件的27行前加上一句:
require 'rubygems'
重新运行passenger命令就可以正确安装上。
ubuntu 8.10中原来已经安装了ruby 1.8.7,与ruby-enterprise中的ruby版本不一样,需要在用户根目录下的.bashrc文件中调整一下PATH:
export PATH=/opt/ruby-enterprise/bin:$PATH

另外系统中sudo没有gem命令,需要加个软链接,这样才能用sudo安装gem包:
$> sudo ln -s /opt/ruby-enterprise/bin/gem /usr/bin/gem
(不要用sudo apt-get install rubygems 安装)

之后再生成二个文件:
$> sudo vi /etc/apache2/conf.d/rails
LoadModule passenger_module /opt/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so
PassengerRoot /opt/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-2.0.6
PassengerRuby /opt/ruby-enterprise/bin/ruby
RailsEnv development
RailsDefaultUser test

$> sudo vi /etc/apache2/sites-enabled/rails_app
<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot /somewhere/public
</VirtualHost>

重启apache2之后访问www.yourhost.com.

Wednesday, December 17, 2008

修改gnome-terminal的默认窗口大小

原来打开时为80*24的尺寸,如果需要调整默认的大小,可按下操作:
$> sudo vi /usr/share/vte/termcap/xterm
找到下面这行
:co#80:it#8:li#24\
修改其中的80和24,分别为terminal的高和宽。
也可以用以下命令查看参数:
$> gnome-terminal --help-all

Tuesday, December 16, 2008

rails error ActionController::InvalidAuthenticityToken

升级到Rails 2.2.2后,在用户登录时抛出以上错误,因为Rails新版本对安全控制做了一些加强措施,只要在form中添加<%= token_tag %>即可,Rails会添加一个token(在action中的form_authenticity_token方法生成这个token)在form中,随表单一起提交,可以适当的防止一些web攻击。
<input name="authenticity_token" type="hidden" value="d688e6bf60f43bd171504e059de1ba03f876d129" />
具体可参考 ActionController::RequestForgeryProtection 和 config/environment.rb 中的配置说明:
# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
config.action_controller.session = {
:session_key => '_sv3_session',
:secret => 'b35a7a9ffb88288f11c03b0c24fe45f90879d8fbb83f9ba70649b489d8165f06d4484dd07c6e41c2d9616630781fd739a127d1285c589db3d77801afd30a9d35'
}

Monday, December 15, 2008

mongrel and rails 2.2.2 环境下报 mysql lib 的错误及解决方法

Processing Rails::InfoController#properties (for 127.0.0.1 at 2008-12-15 17:10:53) [GET]

LoadError (dlopen(/Library/Ruby/Site/1.8/universal-darwin9.0/mysql.bundle, 9): Library not loaded: /usr/local/mysql/lib/libmysqlclient.15.dylib
Referenced from: /Library/Ruby/Site/1.8/universal-darwin9.0/mysql.bundle
Reason: image not found - /Library/Ruby/Site/1.8/universal-darwin9.0/mysql.bundle):
/Library/Ruby/Site/1.8/universal-darwin9.0/mysql.bundle
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
/Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:153:in `require'

在MAC OSX 10.5 和 mysql 5.1.23-rc 上,rails 2.2.2 用 mongrel server 启动时访问首页mysql连接属性时抛出以上错误,在对应的/usr/local/mysql/lib下根本没有/usr/local/mysql/lib/libmysqlclient.15.dylib这个文件,当然会加载失败,之前在 rails 2.0.2 上倒没有碰到过这个问题。从mysql官网重新下载了一个 mysql 5.0.67,从其下面拷了一个libmysqlclient.15.dylib 到 /usr/local/mysql/lib/libmysqlclient.15.dylib,就可以解决此问题。
$> sudo cp /usr/local/mysql5/lib/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib

Tuesday, December 09, 2008

svn:ignore command line

$> svn propset svn:ignore -F .cvsignore .
property 'svn:ignore' set on '.'

Reference: http://svnbook.red-bean.com/en/1.1/ch07s02.html

Monday, December 08, 2008

MySQL5.0 同步错误 errno 2013 及解决方法

081208 11:28:29 [ERROR] Slave I/O thread: error connecting to master 'repl@server:3306': Error: 'Lost connection to MySQL server at 'reading initial communication packet', system error: 113' errno: 2013 retry-time: 60 retries: 86400

主要是因为slave连接不到master,可以从以下几点着手解决:
1. iptables是否将对应的master db port给禁了?
2. master db server 是否能够上网? slave db server 是否能够上网?

usage of call_user_func_array function in php


<?php

class Foo {
static public function test($name) {
print "Hello {$name}!\n";
}

public function bar($name){
print "Hello {$name}\n";
}
}

// call static Class methods.
call_user_func_array('Foo::test', array('Hannes'));
// Hello Hannes!
call_user_func_array(array('Foo', 'test'), array('Philip'));
// Hello Philip!


$foo = new Foo();
// call object instance methods.
call_user_func_array(array($foo, 'bar'), array('World!'));
call_user_func_array(array($foo, 'bar'), 'World!');
// Hello World!

?>

参考php手册:
callback
有些诸如 call_user_function() 或 usort() 的函数接受用户自定义的函数作为一个参数。Callback 函数不仅可以是一个简单的函数,它还可以是一个对象的方法,包括静态类的方法。
一个 PHP 函数用函数名字符串来传递。可以传递任何内置的或者用户自定义的函数,除了 array(),echo(),empty(),eval(),exit(),isset(),list(),print() 和 unset()。
一个对象的方法以数组的形式来传递,数组的下标 0 指明对象名,下标 1 指明方法名。
对于没有实例化为对象的静态类,要传递其方法,将数组 0 下标指明的对象名换成该类的名称即可。
Reference:
http://cn.php.net/manual/en/function.call-user-func-array.php

Friday, December 05, 2008

Hack ruby String#length

en = 'test'
cn = '一'
p en # >> "test"
p cn # >> "\344\270\200"
puts cn.inspect # >> "\344\270\200"
p "344".oct.to_s(16)
# >> "e4"
p "270".oct.to_s(16)
# >> "b8"
p "200".oct.to_s(16)
# >> "80"
p "344".oct
# >> 228
p "270".oct
# >> 184
p "200".oct
# >> 128
cn.scan(/./).each do |ch|
p ch, ch[0]
end

# hack ruby String#length
puts en.length # >> 4
puts cn.length # >> 3
puts en.scan(/./).length # >> 4
puts cn.scan(/./).length # >> 3
puts en.scan(/./u).length # >> 4
puts cn.scan(/./u).length # >> 1

Thursday, December 04, 2008

在Ruby中将unicode直接量转化成为utf8字符

require 'cgi'
def unicode_utf8(unicode_string)
unicode_string.gsub(/\\u\w{4}/) do |s|
str = s.sub(/\\u/, "").hex.to_s(2)
if str.length < 8
CGI.unescape(str.to_i(2).to_s(16).insert(0, "%"))
else
arr = str.reverse.scan(/\w{0,6}/).reverse.select{|a| a != ""}.map{|b| b.reverse}
# ["100", "111000", "000000"]
hex = lambda do |s|
(arr.first == s ? "1" * arr.length + "0" * (8 - arr.length - s.length) + s : "10" + s).to_i(2).to_s(16).insert(0, "%")
end
CGI.unescape(arr.map(&hex).join)
end
end
end

puts unicode_utf8('test\u4E2Dtest\u6587test\u6D4Btest\u8BD5test')

find command examples related with file status

空目录
find . -type d -empty

n*24小时前访问过的文件
find . -atime +2

n分钟前状态发生改变的文件
find . -cmin +10

n*24小时前内容有被修改过的文件
find . -mtime +1

n*24小时与(n+1)*24小时这一天中,内容有被修改过的文件(数字n前没有"+"号)
find . -mtime 1

删除find命令找到的文件
find . -cmin 1 -delete

这上面的命令中需要注意一点是其中数字前面需要带上"+"号,才表示这个时间点之前的时间段,如果没有提供"+"号,则指这的就限定在这个时间点内,如这一天内或者这一分钟内。

更多find相关用法可参考http://linux.about.com/od/commands/l/blcmdl1_find.htm