Gih's Blog

只言片语
Posts tagged as shell

很酷的一条命令:比较两个文件特定的行.

2011-06-20 by gihnius, tagged as shell

例如: 比较文件 file1, file2 的前 20行:

diff -u <(sed -n '1,20p' file1) <(sed -n '1,20p' file2) 
以往还折腾awk,perl, 真没想到 diff 能这样用! 从下面的例子看到, diff 是通过两个临时文件进行比较的.
$ diff -u <(sed -n '276,300p' tnrun.pl) <(sed -n '312,350p' tnrun_xml.pl)
--- /tmp/zsh0DiZxS      2011-06-20 17:00:11.000000000 +0800
+++ /tmp/zshFozNfJ      2011-06-20 17:00:11.000000000 +0800
@@ -1,3 +1,22 @@
+
+        (defined $infos[0])?($hosts[$n]->{ip} = $infos[0]):($hosts[$n]->{ip} = undef);
+        (defined $infos[1])?($hosts[$n]->{user} = $infos[1]):($hosts[$n]->{user} = undef);
+        (defined $infos[2])?($hosts[$n]->{pass} = $infos[2]):($hosts[$n]->{pass} = undef);
+        $n++;
+    }
+    close (HOSTS);
+    return @hosts;
+}
+
+## get cmd list in file
+sub get_cmd_from_file {
+    my @cmds;
+    open (CMDS, "<$_[0]") || die "failed to open cmd list file: $!";
+    while (my $cmd = <CMDS>) {
+        chomp ($cmd);
+        push (@cmds, $cmd);
+    }
+    close CMDS;
     return @cmds;
 }
 
@@ -18,8 +37,3 @@
     my $runh = 0;
 
     if ($by_hosts_file) {
-        @tn_ips = get_ip_from_file($hosts_file);
-    }
-    if ($by_cmd_file) {
-        @cmds = get_cmd_from_file($cmd_file);
-    }
$ 
这就是说, <() 可以建立临时文件, 试试这个命令
 cat <(cat .profile) 
你会看到cat输出的是 .profile的内容. 接着
 echo $_ 
会输出 /tmp/zshOuJC2d 类似的内容, 表示一个临时文件. 也就是说上一个命令等于执行
 cat /tmp/zshOuJC2d 

tmux 终端模拟器

2011-06-03 by gihnius, tagged as shell

不知是不是用FreeBSD多的原因, 把用了多年的 screen 甩掉了, 在个人

电脑和服务器上都装上 tmux. 不过用tmux那么久也没感觉到跟screen有什么区别,速度两者差不多,就是tmux用内存比 screen 少. tmux也很稳定.好像tmux的配置并不比 screen的配置方法高明. 不管怎么样,用上了还是挺喜欢的!下面是我的配置:.screenrc:

setenv DISPLAY ':0.0'

escape ^Zz

caption always "%{=u} %{.W}%-w%{.W}%{=b G}%n %t%{-}%+w %L= %=%{..B} %H %{..Y} %m/%d %c%a "

bindkey -k k2 next
bindkey -k k1 prev

termcapinfo xterm*|rxvt|urxvt|mrxvt|*term* 'hs:ts=\E]0;:fs=\007:ds=\E]0;\007'
termcapinfo xterm* OL=5000
termcapinfo xterm|xterms|xs|rxvt 'ti@:te@'
termcapinfo xterm* 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'

bell "%c bell -> %n%f %t^G"
defscrollback 5000
msgwait 2
startup_message off
shell zsh
vbell_msg "$$$$"
vbell off

screen -t COM 0
screen -t ZSH 1
screen -t X 2
screen -t KSH 3 #ksh93 -i -l
select 1

#altscreen on
attrcolor b ".I"
defbce "on"
defutf8 "on"
nonblock "on" 
tmux配置.tmux.conf
unbind C-b
set -g prefix ^O
bind o send-prefix

# Statusbar properties.
set -g display-time 3000
set -g status-right "%m/%d %a %H:%M" 
set -g status-bg default
set -g status-fg yellow
set -g set-titles on
set -g bell-action none
set -g history-limit 5000
set -g base-index 1
setw -g automatic-rename off
set -g status-utf8 on
setw -g utf8 on
set-window-option -g window-status-current-attr bright,reverse
set-window-option -g window-status-current-bg black
set-window-option -g window-status-current-fg green
set-window-option -g automatic-rename off
set-window-option -g utf8 on

# Bind function keys.
bind -n F1 prev
bind -n F2 next
bind-key -n C-left prev
bind-key -n C-right next
## switch pane
bind-key -n M-left select-pane -L
bind-key -n M-right select-pane -R
bind-key -n M-up select-pane -U
bind-key -n M-down select-pane -D
bind-key -n C-\ split-window
bind-key -n M-\ split-window -h

bind-key -n C-F12 lock

### KEY map
# F1: prev window
# F2: next window
# Ctrl+\: split window V
# Alt+\: split window H
# Ctrl+F12: lock session
# Ctrl+Z+Left: resize pane left
# Ctrl+Z+Right: resize pane right
# Ctrl+Z+Up: resize pane up
# Ctrl+Z+Down: resize pane down
# Ctrl+Z then Left: switch to left pane
# Ctrl+Z then Right: switch to right pane
# Ctrl+Z then Up: switch to up pane
# Ctrl+Z then Down: switch to down pane
# Ctrl+Z then x: kill current pane or window 
在tmux里,我把 Prefix 按键绑定到 Ctrl+O 以免跟emacs冲突.tmux启动脚本:
#!/bin/sh
tmux has-session -t gih >/dev/null 2>&1
if [ $? -eq 0 ] ; then
    tmux -2 -u attach-session -t gih
else
    tmux new-session -d -s gih
    tmux new-window -d 
    tmux new-window -d
    tmux new-window -d
    tmux new-window -d
    tmux rename-window -t 1 SYS
    tmux rename-window -t 2 X
    tmux rename-window -t 3 NET
    tmux rename-window -t 4 LISP
    tmux rename-window -t 5 TOP
    tmux select-window -t 2
    tmux -2 -u attach-session -t gih
fi 
tmux配置说明就不说了.后来找到一个链接:介绍tmux的使用http://wiki.freebsdchina.org/software/t/tmux