網頁

2013年9月3日 星期二

stderr and stdout to file (sh / bash / csh / tcsh)



stderr and stdout to file

sh: (time date) > wa.txt 2>&1
bash: (time date) >& wa.txt or (time date) &> wa.txt or (time date) > wa.txt 2>&1
csh/tcsh: (time date) >& wa.txt

There are 3 file descriptors, stdin(0), stdout(1) and stderr(2) (std=standard).

2013年6月7日 星期五

在Javascript中更好的計時器,取代Date,用來計算執行時間等應用

一般在javascript中要計算某段程式的執行時間,可以使用Date物件。
用法是Date.now() 或 new Date().getTime(),用以取得microsecond(毫秒)的目前時間數值。
在執行函數的前後取得並相減,以取得執行時間,例如:
var ticks_start = Date.now();
RunTestFunction();
var ticks_end = Date.now();
alert(ticks_end - ticks_start);

但一些指令的時間小於毫秒時,根本結果不是0就是1,看不出所以然。
從這篇文章“A better timer for JavaScript”得到了更小單位時間的方法。
window.performance = window.performance || {};
performance.now = (function() {
  return performance.now       ||
         performance.mozNow    ||
         performance.msNow     ||
         performance.oNow      ||
         performance.webkitNow ||
         function() { return new Date().getTime(); };
})();

再將程式改為以下,即可測得0.x毫秒的執行時間:
var ticks_start = performance.now();
RunTestFunction();
var ticks_end = performance.now();
alert(ticks_end - ticks_start);

2013年6月6日 星期四

Ubuntu 安裝 Sublime Text 2與中文輸入問題

2013/12/04註: Sublime Text 3 beta在2013/06/27已開放給未註冊使用者下載。

為了要能垂直編輯多行文字,在特定時候,只留下或編輯多行文字中的值,所以要裝sublime來用看看。
像是以下資料:
  * PID: 7234
  * PID: 6821  
  * PID: 7108    
可以按Ctrl-Alt + 上下左右,選取範圍後,删除或編輯。
變成像是:

  * 7234
  * 6821
  * 7108

即使位置不對等或不是連續,例如:
style="width: 500px;"
style="height: 100px; width: 50px;"
style="width: 80px;"

可以按Ctrl +滑鼠左鍵,點選要插入資料的位置,就可加入相同的內容到各個地方。
style="width: 500px; color: red;"
style="height: 100px; width: 50px; color: red;"
style="width: 80px; color: red;"

目前無法使用apt-get 來安裝,安裝方式如下:
  1. 先下載Linux 32或64於http://www.sublimetext.com/2.
  2. 開啟Terminal.
  3. cd ~/下載
  4. tar -xf "Sublime Text 2.0.1 x64.tar.bz2"  (檔名依下載版本修改)
  5. sudo mv "Sublime Text 2" /usr/lib
  6. sudo ln -s "/usr/lib/Sublime Text 2/sublime_text" /usr/bin/st2
啟動方式一:
  • Terminal中輸入st2
    • # st2
啟動方式二:
  • 加入左邊的啟動器選單中
    • 先在dash中找“主選單” ,接著按“新增項目”,加入/usr/bin/st2.
    • 然後在dash中就可找到 Sublime Text 2,並拉到啟動器。

參考:http://www.arthurtoday.com/2012/09/ubuntu-install-sublime-text-2.html

2013/06/20補充,無法輸入中文的解決方法:
在windows和linux(搭配ibus; scim;fcitx)都有不同狀況的無法輸入中文,
這裡只針對環境ubuntu+ibus,可以安裝plugin來解決。
安裝一個sublime text 2的插件InputHelper,用於輸入中文,安裝如下:
cd ~/.config/sublime-text-2/Packages 
git clone https:// github.com/xgenvn/Input Helper.git
重新啟動之後按Ctrl+Shift+Z可跳出一個中文輸入框(不好用但加減用)。

2013年4月26日 星期五

Copy a file to multiple directory.

Copy a file to multiple folder. Can use find command and type directory (-type d). 
Then use -exec run cp command copy to target {}.

e.g.
find . -name "folder_name" -type d -exec cp /xxx/filename.txt {} \;

2013年3月29日 星期五

Set user shell using ssh script to linux base system and sudo problem.

I using ruby ssh library to linux server. then run sudo command.
like "sudo usermod -s /sbin/nologin Chien-Ming-Wang".


centos:
  Set user shell: sudo chsh -s /sbin/nologin [login name]
  When error: "sudo: sorry, you must have a tty to run sudo"
    vi /etc/sudoers
    mark line: "Defaults    requiretty"


Debian & Ubuntu:
  Set user shell: usermod -s /usr/sbin/nologin [login name]
  When error: "sudo: no tty present and no askpass program specified"
    vi /etc/sudoers
    Add line: [login name] ALL=(ALL) NOPASSWD: ALL

FreeBSD:
  Set user shell: sudo pw usermod [login name] -s /usr/sbin/nologin
  When error: "sudo: no tty present and no askpass program specified"
    vi /usr/local/etc/sudoers
    Add line: [login name] ALL=(ALL) NOPASSWD: ALL

2013年3月18日 星期一

用ssh tunnel來突破公司的防火牆

用ssh tunnel來突破公司的防火牆

外部電腦能連至Server,但Server不能連至公司內電腦,但公司內電腦能連至Server,此時運用反向Tunnel。

  • 公司內電腦建立反向Tunnel連至Server: ssh -NfR 2222:127.0.0.1:22 server_ip_or_domain
  • 外部電腦建tunnel連至Server: ssh -NfL 4444:localhost:2222  server_ip_or_domain
  • 外部電腦 即可連至公司內電腦:  ssh -p4444 localhost


外部電腦不能連至公司內電腦,但內部電腦能連至外部電腦,此時運用反向Tunnel。

  • 公司內電腦建立反向Tunnel連至外部電腦 ssh -NfR 2222:127.0.0.1:22 external_ip_or_domain
  • 外部電腦 即可連至公司內電腦:  ssh -p2222 localhost

參數說明
  • -L [bind_address:]port:host:hostport
    • Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.
  • -R [bind_address:]port:host:hostport
    • Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side.
  • -N      Do not execute a remote command. 
  • -f      Requests ssh to go to background just before command execution.



2012年4月6日 星期五

Working with Unicode in Ruby

ruby的irb中要顯示中文
輸入
irb> $KCODE = 'u'
就可以了

其它處理utf8字串的有
irb> require 'jcode'
irb> require 'rubygems'
irb> require 'unicode'

詳細參考 http://ideaharbor.org/notes/technical/working-with-unicode-in-ruby