bash script notes (1)

I tied to study the shell script a few times. But on that time, I didn’t get enough knowledge to deal with it. Now, I’m back.

Situation 1

I want to startup the vpn service when the computer startup. I added pon vpn_name to rc.local. However, this can be failed because when this line execute, the network may not be ready. So I decide to write a script to do this. It will wait until the Internet up. Here’s it.

#!/bin/bash

sleep_time=1
export PATH=$PATH:/usr/bin

while [ 1 ]
do
  ping_success=`ping -c 1 -n vpn.yegong.net | \
    sed -n -r 's/^.* (.) received.*$/\1/p'`
  if [ $ping_success -eq 1 ]
  then
    pon yegong
    exit 0
  fi
  sleep $sleep_time
  sleep_time=$(expr $sleep_time + 1)
done
  1. export PATH is very important.
  2. `ping -c 1 -n vpn.yegong.net | sed -n -r ‘s/^.* (.) received.*$/\1/p’`, using ` surround the command to get the output text.
  3. sed -n -r ‘s/^.* (.) received.*$/\1/p’, using sed to simplify the output. It’s the regular expression. Very similar with VIM replace syntax, isn’t it?
  4. if [ $ping_success -eq 1 ], [ condition ] means the test program, equivalent to test condition, so man test will show more usage.
  5. sleep_time=$(expr $sleep_time + 1), it seems that every variable in shell is a string, so you can’t simply write x=x+1. Instead of, expr program read a string expression and output the results. Please notice the space in the expression.

Situation 2

For each text files in a directory, convert the file encoding to utf8.

There’s a simple util can guess the file encoding. It can be found at Wu Yongwei’s Programming Page (Can’t access when I write the article). Then I just need a script to combine the tellenc and iconv.

TMP_FILE="/tmp/utf8lize.output"
ENCODING="utf-8"

if [ $# == 0 ]
then
  echo "Usage: utf8lize FILES"
  exit 1
fi

for f in "$@"
do
  if [ -f "$f" ]
  then
    enc=`tellenc "$f"`
    if [ $enc != $ENCODING ] && [ $enc != "binary" ]
    then
      echo $f : $enc
      cp "$f" "$f.bak"
      iconv -f "$enc" -t "$ENCODING" -o "$TMP_FILE" "$f"
      cp "$TMP_FILE" "$f"
      rm -f "$TMP_FILE"
    fi
  fi
done
  1. $# results the number of arguments when it been executed.
  2. for f in “$@” is the for-each loop. And “$@” indicates the all program arguments. In addition, when you run the program utf8lize *, * will convert to filenames array. Using the ” to surrounding $@ can deal the filename with space.

on December 15th, 2009 | No Comments »

Reset the layout/views setting of eclipse

It’s the second time to get such a bug of eclipse.

It seems that eclipse can’t work fine with the newer/older version of XulRunner other than 1.9.* in many distribution of Linux. Ubuntu is in the list.

The result is that I can’t open the javadoc view of eclipse. Everytime when I click the tab, a error dialog will pop. Then I switch to other tab and go on coding. But this time I’m not so lucky. I just close the eclipse. It got crazy then. Eclipse can’t startup any more. Eclipse can’t startup because it want to restore the last state. But it failed to open the javadoc view, and then break.

Too many article about this problem from Google. The keyword can be found at %workspace%/.metadata/.log.

SWTError, XPCOM

Some solutions looks good, and proved by the follow comments. However, none of them works on my machine.

Surely, I can find a way for me. I just need to reset the layout of eclipse. Then Google tell us it’s controlled by this file, %workspace%/.metadata/.plugins/org.eclipse.ui.workbench/workbench.xml. I just delete it and then everything works fine now.

In addition, I also remove the javadoc view to prevent the accident happen. This can’t be done from eclipse UI because XulRunner problem. However, you can do it in the config file. Open the workbench.xml, and delete this one

<view id=”org.eclipse.jdt.ui.JavadocView”/>

on December 1st, 2009 | No Comments »

VIM tips (2)

  1. 利用”ayy复制后,可使用”Ayy继续复制,新的内容会添加到寄存器a的尾部。
  2. 除了利用*来匹配当前光标下的单词外,还可以用表示向后查找的#。此外还有g*和g#,区别在于*,#要求严格匹配一个单词。
  3. 启动参数:
    vim + filename,vim打开后自动把光标移动到文末
    vim +/pattern filename,vim打开后自动查找pattern
  4. 多缓冲区编辑:
    利用:n和:N在缓冲区间移动
    利用:e filename来新开一个缓冲区
    利用:e#回到上一个缓冲区,其中#表示上一个被编辑的缓冲区,类似的%表示当前缓冲区
  5. 外部命令:
    :! command,执行外部命令
    :!!,执行最近一次执行的外部命令
  6. 录制与回放:
    当执行了:命令后,命令会自动存入:缓冲区,可以用@:回放
  7. :r !command与!!command有类似的作用,区别在于前者是插入而后者是替换

on November 13th, 2009 | 8 Comments »

Ctrl Tips

Control键对于国人来说主要就是Ctrl+C和Ctrl+V。

今天要说的是Ctrl+Backspace, Ctrl+Delete, Ctrl+Left, Ctrl+Right。

其实很简单,就是在四个键原有的功能上加上的“按单词…”的限定。例如:Ctrl+Backspace,从光标所在处开始向前删除一个单词。

  1. 这在写英文时比较有用
  2. 写代码时也很方便
  3. 中文词组没有空格,因此其效果变为按标点符号进行删除和移动。也许有时会有用,一旦误操作要及时Ctrl+Z喔
  4. 大多数的文本编辑器都支持这一操作,以及Firefox, Internet Explorer, eclipse等,你甚至可以在Windows Explorer的地址栏这样做。Notepad支持除Ctrl+Backspace以外的三个组合键。
  5. 对于Vim,需要自行定义inoremap <C-BS>    <C-W>在.vimrc中

PS. 今天中午发现,我键盘上磨损的顺序是Space > F > IAJ > K;N > LEUON。这是Vim的效果么?

on November 9th, 2009 | 1 Comment »

悼念那八年

昨晚,和胖娃聊到他和现任女友的事情,把本来我基本隔天就在想起的那些事又过了一遍。胖娃1点睡了之后,我老是睡不着,一睡不着就开始胡思乱想。这十几天来,psp内播放得最多的就是“对不起谢谢”“goodbye to romance””don’t look back in angers”。前天从猩猩宿舍出来本来说晚上去看陈绮贞的演唱会,但是一方面走嘿累了,一方面确实不想和同事去听容易挑起情绪的歌。每次听到“你到过下雪的北京”就莫名的伤感。

2001冬天开始,2004年带着一种得意的心情去了成都,2009年不晓得朗阁的就拉爆了。其实我晓得,小的矛盾因为我到了北京就放得无穷大了,09年春节的经历真的让我好痛苦。8年就这样过了给。也没得愤怒,就是带着遗憾就过了。一直在想,关于自己,8年做了好多错事,做了好多能做好没做好的事,想起都让个人包眼睛水。胖娃昨天说以后我老婆比他老婆幸福,我能理解人。其实胖娃只是把好处放大了,却没看到我对ex做的那些哈事。

昨晚把签名档改了之后,今早ex来问我,聊了几句之后突然好想哭,30分钟前就跑到厕所隔间里面偷偷的抹眼睛水。我把ex从一个相信爱情的小妮子变成了一个不相信永远的女人,给人家画了好大个美丽的前景然后又自己撕了。经常想起大学四年P事没做,如果我能留在重庆又是另外的境遇,如果我能学点能养活自己的东西而不是在毕业后才开始想学习可能又是另外的境遇。所以现在只想好好对家人朋友也是自己该遭。无聊之余有asn的比赛看也算是个慰籍。

昨天和胖娃说的两件事情我是真的在冷个想,一是有认真的考虑去当老师,小学老师,中学老师都行;一是如果最好的兄弟们都结婚了,快的话30岁之前送完所有人应该没问题吧,我想自己去个新的地方。

呵呵,刚才聊完,ex说以后就叫我慧能了,我突然觉得好好笑。又哭又笑,黄狗撒尿。

end

on November 9th, 2009 | 2 Comments »

Repair the network infrastructure of the rental house

Today, I fixed the cables of my house.

The main reason that drives me to do is the weak signal of the wireless network.

I have a Netgear WGT624 v4, which is sending a very bad signal.1 One week ago, it’s about -50dB when I check it from the iwconfig. But yesterday, it’s just -76dB.

It’s true that there’re some antennae can work with the bad signal. But most of them just can gain 10dB. So this can’t be a final solution. Then I checked the preserved cables of house. It’s nice that they all roads to the SCTR weak current box. However, the cables is not connected, with no sign, and even have no RJ45 connector. It took me about two hours to pair the cables and make connectors for them.

Quite a heavy work. I learnt the order of the subcables from it. In Chinese, it’s 白橙 橙, 白绿 蓝, 白蓝 绿, 白棕 棕.

Actually, you can’t make connectors with the customized order. Because the subcables are in twisted pair, not just simple lines. So the electronic signal will interfere with each other if you got a wrong order.

Notes,

1. I want to but don’t know how to setup a DD-WRT or OpenWrt on it. Many appreciation if someone can help.

PS,

I’ve made a very nice configuration of compiz and terminator. It’s amazing to key down the shell commands/vim on the beautiful desktop background. Maybe I will try the awesome next month.

chars and words is now floating before the cloud.

chars and words is now floating before the cloud.

on November 4th, 2009 | 3 Comments »

Experience of ubuntu karmic and the fix of eclipse

Thanks, Arsenek, for your nice prologue.

I just upgraded the ubuntu to 9.10 rc for the machine in office this week.

It’s cool.

The new input method is easy to use.You just need to apt-get purge ibus and reinstall them. I think the package in the cd image is out-of-date.

Also, I installed a new icon theme, Nerdy Lines. Then I found much icons didn’t work correctly, so I fixed most of them. As the license of origin work is cc-by-nc-nd 2.0, so my modification can’t be released.

icon-nerdy-liines

Only got one problem of karmic today, I found that eclipse can’t create the new project / import a existed project / quick search.

Lots guys reported the same issue everywhere. It seems a problem about the gtk library. A easy solution is

export GDK_NATIVE_WINDOWS=true
cd ~/share/eclipse/
./eclipse

Please copy it to a shell script in the PATH to replace the eclipse symbol link.

If you got another problem which is in the eclipse starting up stage — eclipse dies at one small blank popup window. I just can tell it’s caused by the XULRunner. You can uninstall it and get a standalone version of firefox.

Update Oct.31 2009 : The home machine is also updated to the karmic.

on October 30th, 2009 | No Comments »

第一篇

刚看完阿森纳对AZ的欧冠小组比赛,最后一分钟被扳平,还真是让人感叹现实不能完美。原本这场比赛也不会关系到小组出线,更不是什么重要比赛,我说不完美,是因为接下来的那天,即10月22日,是Arsene Wenger的60岁生日。天朝60岁能把全国都折腾一下,可Wenger’s baby却没给他们的boss带来一场胜利,足够遗憾,也足够我感叹贵党真是能集中力量干大事。。。

根据cooper的说法,15号就进这个店了,一直也憋不出来什么,趁着看完比赛,就继续说阿森纳好了。阿森纳官方有推出一个类似访谈的纪录片系列,当然球员会有昔日在球场上驰骋的片段。这个系列叫Legend,我收藏了Arsene Wenger,Herbert Chapman,Dennis Bergkamp,Patrick Vieira,Thierry Henry,当然都是网上下载的。我不知道什么时候能收到一封官方的邮件给我说:arsenek你好,请去购买(下载)Legend——Robert Pires,啊哈哈哈,Pires很赞。

再来介绍下自己的名字,Arsenek,一个很简单的拆字就明了了,Arsene+K,需要说一下K是Keane的K,而且还是Roy Keane的K,不过反正两个成名的Keane,不论Roy还是Robbie都是阿森纳的死敌,这也是个怪事,哈哈。对此我又想到一点,我坚信不疑喜欢上阿森纳就是命运。98年才开始看球的时候是支持意大利的,可是荷兰的精彩演出,让我记住了Bergy,一个球风十分令人愉悦的大叔。98年让我记住的另一支球队就是法国,不过我不喜欢98年的冠军和亚军,一个也不。再来就是初中班队要买球服了,一群人跑到大田湾的市场,兴冲冲的想买Liverpool的金黄色比赛服,可是老板说货不够。于是就换了同样是黄色的阿森纳比赛服,那时候的赞助商还是SEGA。这件球衣一直都留着,虽然是30多块买的盗版,初中完了我妈想扔,高中完了我妈也想扔,到了大学完了我妈懂了,搬家后都帮我好好收着的,嘿嘿嘿。所以说,对于一个信奉Once be…Always be…句式的男人来说,喜欢上阿森纳就是水到渠成的事了。

Btw,2002年让我真心喜欢上爱尔兰这支球队,而不仅仅是因为Keane的原因,一个团队通过合作去和强大的对手竞争总让人感到高兴,至于胜利,见鬼去吧。04年无所谓支持,06、08年支持德国,这是另一个大叔Jens Lehmann的故事。希望接下来的世界杯预选赛附加赛中爱尔兰能搞死法国,让我明年也有个主队,哈哈哈哈。

说到阿森纳,那就要说一下几句和阿森纳有关的话。第一句是Victoria Concordia Crescit,这个等于队训的东西,翻译成英语是Victory though harmory,涛哥估计要说成“胜利来自和谐”,个人认为很好的解释了我对足球的理解。Btw,你可以是C.Ronaldo和Messi那样的超人,也可以是巴萨和曼联那种宇宙队,但是见鬼去吧,哈哈。第二句和第三句都是来自Arsene Wenger的妙语,原文记不住了,翻译过来是“整个生命的成功是我们通过态度引发的幸福巧合”,以及“相互适应的完美,远胜于独秀于世的华丽”;这基也是我现在的人生观了。好了,说完这两句话,这段没有了,下段开始说Wenger。

Arsene Wenger,阿塞温格,一般我和一个阵营的同学聊天,总称之为温父,一个固执、儒雅、温情的完美主义追求者。对,固执排第一位,原因不说了,我懒。可以这样说,Wenger的足球不仅是表面上的美丽和青春,Wenger的足球透露出公平,诠释着团队,更演绎着智慧。Wenger可能永远不会取得和Sir.Fergy一样多的冠军,也有种说法叫“历史只记得成功者”,但是我想说的是:冠军?见鬼去吧,win in arsenal’s style才是最重要的。我觉得我是有信仰的,因为从12、3岁开始整个人生都开始涂上阿森纳的红色,但是说我信仰Arsenal,不如说是信仰Arsene。虽然我只了解Arsene在足球场上的一面,但我能从中感受到他的态度:公平竞争、团队足球、功利面前保持的个性和温情、责任感。法国队报的采访说Wenger只恨一个人,马赛队的前老板,一个用钱收买裁判球员获得冠军的痞子。其实我想说,每个人喜欢的东西总是代表了对现实的情绪。前天cooper让我看了阮一峰的《为何我喜欢编程》(大概是这个题目),里面提到代码的世界是公平的,我想我的态度也大概如此,在球场上,在一定的规矩内公平竞争,如同人生,只有公平再好不过。

隐约记得,当初不是想写这些的。好了,6点了,睡2个小时该上班了。突然发现国庆回来后,上班时打了鸡血的状态没得了。。。岁月催人懒啊

on October 21st, 2009 | 4 Comments »

Linux wifi driver for Thinkpad T400

My girlfriend bought a new Thinkpad T400 R29 half month ago. ThinkWiki just told the wireless hardware must be Intel 5100/5300 or Intel WiMax 5150/5350. However, I failed to setup the driver.

Windows just told it’s a realtek on it. But I didn’t believe. I don’t believe because it’s too crazy for a Thinkpad to not use the Intel wireless chip.

Then things on the easy way.

  1. Google, then confirm that there’s no native driver for linux.
  2. copy the inf and sys files from windows xp. rtl8191se
  3. ndiswrapper. HOWTO in Chinese
  4. no wlan found
  5. remove the installed modules, revert all changes
  6. realtek driver for windows 2k, which is suggested by the archlinux wiki
  7. ndisgtk (GUI for ndiswrapper)
  8. found wlan0 without restart computer

on September 27th, 2009 | No Comments »

SQL note (1) – INSERT

Today, I tried to update the structure of database and learnt something new about insert statement. Share it with abstract example.

Now we got a `interest_rate` table in database contains these columns: `currency`, `interest_rate`. It indicated the relation of interest rate and currency. One day, we find that the interest rate is only relate to currency but we want also `period` for the fixed deposit.

It is a database and contains so many data, so we can’t change it manually. We just want to write a SQL script and run it.

Surely, this process works.

  1. change the structure of database and insert data if necessary;
  2. do the code improvement;
  3. then the system should act exactly the same as before;
  4. use the background control panel to modify the data when necessary.

It’s nice because it just need one hour to upgrade and then the whole system works fine. It’s online without database export, changing, and import, which are very danger and result a quite long downtime.

Then let’s focus on how to upgrade database structure and insert records for this example. I’m using MySQL and assume that there’s a table contains all possible `period` values.

# create a temporary table
CREATE TABLE `interest_rates_tmp` (
  `currency` VARCHAR(3) NOT NULL,
  `rate` INT NOT NULL
);

# and copy data
INSERT INTO `interest_rates_tmp`
  SELECT * FROM `interest_rates`;

# truncate the old table
TRUNCATE TABLE `interest_rates`;

# add a new column to the table
ALTER TABLE `interest_rates`
  ADD `period` INT NOT NULL AFTER `currency`;
ALTER TABLE `interest_rates`
  DROP INDEX `currency`,
  ADD UNIQUE `currency_period` (`currency`, `period`);

# insert the cartesian product of the `interest_rates`
# table and the `periods` table
INSERT INTO `interest_rates` (`currency`, `period`, `rate`)
  SELECT `currency`, `periods`.`value` AS `period`, `rate`
  FROM `interest_rates_tmp`, `periods`;

# drop the temporary table
DROP TABLE `interest_rates_tmp`;

I got little knowledge about the INSERT statement with SELECT subquery before. Even I didn’t know it’s a valid SQL script or not. So save it here.

on September 15th, 2009 | No Comments »