Correct source file encoding with one liner

Amoeba项目最早的代码可以追溯到2008年了,其中有多个作者贡献代码,因为一直在Windows下开发,所以没有使用UTF8编码,最近大家统一到UTF8,遇到了代码编码不正确的问题。

于是我们需要统一解决一下这个问题:

iconv -f gbk -t utf8 -o ConnectionManager.java ConnectionManager.java

这样可以把gbk编码的源文件转换为UTF8,原地转换。

推广到整个代码目录,用find和xargs做,xargs通过-I来制定一个占位符。

find . -name "*.java" -type f -perm +600 -print | xargs -I _ iconv -f gbk -t utf8 -o _ -c _

结果发现iconv运行中报了错,进一步检查发现一部分代码正常转换了,另一部分却乱码了。原来,两个作者的代码分别是gbk和gb2312,这iconv转换的时候两种编码并不兼容。这就麻烦了,必须对代码分别处理才可以,区别代码的编码,暂时就用Java源文件里的作者名字。又看了一下find似乎没有对文件内容过滤的条件,不过不要紧,我们可以用xargs做:

find . -name "*.java" -type f -perm +600 -print | xargs -I _ sh -c 'grep -q hexianmao _ && iconv -f gb2312 -t utf8 -o _ -c _ '

对这位hexianmao作者的代码,我们利用grep进程的返回值来进行判断。grep的-q参数相当于>/dev/null。这里有一个tricky的地方,再xargs里我们不能直接用&&来组合命令,不过可以通过sh -c这样的方式,并且其中的占位符会被xargs合适地替换掉。

这样执行之后,这位作者的gb2312代码就被成功转换了。而另一部分作者的gbk代码也可以用同样方式解决了。

Performance Visualization with Gnuplot, continued

After I post my command to commandlinefu.com, there is an alternative command followed:
(echo “set terminal png;plot ‘-’ u 1:2 t ‘cpu’ w linespoints;”; sudo vmstat 2 10 | awk ‘NR > 2 {print NR, $13}’) | gnuplot > plot.png

This one is appreciated for using pipes and redirection. It works on Ubuntu Jaunty, on which, gnuplot doesn’t provide a -e option. The author also showed an link to an open source project ‘vmplot’ on freshmeat. Vmplot is written in python, which combines parameters with scripts and end up with an os.system call. Grab the source from here. (Why use python instead on a simple bash command? because it’s difficult to draw a multiplot without temp file or variables, you cannot read ‘-’ twice.)

There are several people vote me down for my command! It must be primers whose use ubuntu and knows the world via Synaptic only, because they failed to run the command on their dated machine (*_*)

DB dump with chroot

I caught chroot first time when attempting to install gentoo linux. Of course, its a tool of great useful. Two months ago I had my laptop crashed, and all static files were recovery easily by copy while raw db data file seems to be difficult to handle.

The old file system is in a mobile disk, mounted at /media/disk-1/. Now I try to use chroot to rollback to that environment, so that all standard dump tools will be available.

Before you chroot to the system, don’t forget to bind /dev to new location. Or you might get error prompt such as “/dev/null Permission Denied” (it’s a common error). Just execute command below:
$ sudo mount –bind /dev /media/disk-1/dev

Then change root to my old system:
$ sudo chroot /media/disk-1

Now you become root user automatically.

MySQL Dump

In a standard ubuntu mysql installation, we should launch mysql db from init.d by:
# /etc/init.d/mysql start

But it’s no longer available in such environment. Fortunately, there is a direct way:
# mysqld_safe &

OK, go on to dump database with mysqldump:
# mysqldump –all-databases > mysql_dump_file

Postgresql Dump

We cannot use init.d to start pgsql either. Therefore, try to run it by:
# su postgres -c “/usr/lib/postgresql/8.3/bin/pg_ctl start -D /etc/postgresql/8.3/main”

/etc/postgresql/8.3/main is the default data directory in standard installation(with apt-get). This directory is supposed to contain a file named with “postgesql.conf”

Now pgsql db is also running. As root user we have no privilege to run pgsql utilities. so take following steps:
# touch pgsql_dump_file
# chmod a+w pgsql_dump_file
# su postgres -c “pg_dumpall -f pgsql_dump_file”

Conclusion

Personally, I found it’s too complex and low efficiency to backup and restore data with mysql and postgresql. If there is no critical requirement(just like personal data management), file based db(Surely I mean sqlite) is no doubt better choice. Easy management(lots of gui tool now provided), smooth copy and move, standard db interface for programming, and enough functionality.

早起,写一个变色的小程序

以前用Windows的时候,我是说用XP的时候,因为不支持颜色主题,也无从知晓如何通过自动化的方式更换Windows的主题,所以,经常是人肉地每天换主题(其实只是为了换标题栏的颜色),追求新鲜感。
现在用Gnome桌面以后依然保持着这种爱好。遗憾是之前一直没有找到设置存放的位置。前两天在command-line-fu上恰好看到一个变换桌面背景的命令,原来这类配置都存放在gconf里,用gconftool就可以进行操作了。
然后很容易地就能在图形化配置管理器里找到颜色配置的键值位于/desktop/gnome/interface/gtk_color_scheme,值是一个用换行符隔开的名值对,内容就是我们在Appearance配置窗口里看到的那些。good,写个shell小程序每天换颜色!
以下是我孱弱的shell脚本:
[codesyntax lang="bash" lines="fancy"]

case `date +%w` in
	0) color='#C4C4A0A00000';;
	1) color='#CECE5C5C0000';;
	2) color='#8F8F59590202';;
	3) color='#4E4E9A9A0606';;
	4) color='#20204A4A8787';;
	5) color='#5C5C35356666';;
	6) color='#A4A400000000';;
esac

sedcommand="s/\(selected_bg_color:\)\(.*\)/\1$color/"

color_scheme=`gconftool-2 -g /desktop/gnome/interface/gtk_color_scheme | sed $sedcommand`

gconftool-2 -t string -s /desktop/gnome/interface/gtk_color_scheme "$color_scheme"

[/codesyntax]
每天一换,颜色是tango调色板里的色彩,HOHOHAHA