effective linux.1.(commandline)

29
Effective Linux (1) Effective Linux (1) commandline commandline hongjiang 2012.2.29 hongjiang 2012.2.29

Upload: wang-hongjiang

Post on 08-Jul-2015

707 views

Category:

Technology


1 download

DESCRIPTION

dancing in commandline

TRANSCRIPT

Page 1: Effective linux.1.(commandline)

Effective Linux (1)Effective Linux (1)commandlinecommandline

hongjiang 2012.2.29hongjiang 2012.2.29

Page 2: Effective linux.1.(commandline)

Dancing in commandlineDancing in commandline

►1) getting to know your shell1) getting to know your shell►2) shortcut keys2) shortcut keys►3) command history3) command history►4) tips (bash & zsh)4) tips (bash & zsh)

Page 3: Effective linux.1.(commandline)

我的终端环境我的终端环境

► xubuntu(11.10) + zsh(4.3.11) + gnome-xubuntu(11.10) + zsh(4.3.11) + gnome-terminal(3.0.1)terminal(3.0.1)

► zshzsh 的语法与的语法与 bashbash 接近,两者相互借鉴接近,两者相互借鉴

► 有关有关 zshzsh 的技巧,可参考我的技巧,可参考我 deliciousdelicious 收录收录

的的

http://delicious.com/w.hongjiang/zshhttp://delicious.com/w.hongjiang/zsh

Page 4: Effective linux.1.(commandline)

了解你所用的了解你所用的 shellshell

$ echo $SHELL $ echo $SHELL 看到的结果可靠吗?看到的结果可靠吗?

假设默认是假设默认是 BashBash ,执行下面的,结果是什么结果,执行下面的,结果是什么结果

$ ksh $ ksh $ echo $SHELL$ echo $SHELL

那么 那么 $SHELL $SHELL 变量是从哪里来的?变量是从哪里来的?

$ cat /etc/passwd | grep $UID$ cat /etc/passwd | grep $UID或 或

$awk -F: '/'"$UID"'/{print $NF}' /etc/passwd$awk -F: '/'"$UID"'/{print $NF}' /etc/passwd

另:另: $SHELL $SHELL 是可被改写的是可被改写的

Page 5: Effective linux.1.(commandline)

了解你所用的了解你所用的 shellshell

正确的方式:正确的方式:

$ ps –fp $$ $ ps –fp $$ UID PID PPID C STIME TTY TIME CMDUID PID PPID C STIME TTY TIME CMD1000 2454 2449 0 22:53 pts/1 00:00:00 /usr/bin/zsh1000 2454 2449 0 22:53 pts/1 00:00:00 /usr/bin/zsh

$$ $$ 是大多是大多 shellshell 内置变量,表示当前进程内置变量,表示当前进程 IDID$$ $$ 变量不可被改变变量不可被改变

Page 6: Effective linux.1.(commandline)

了解你所用的了解你所用的 shellshell

► 脚本首行声明为 脚本首行声明为 #!/bin/sh #!/bin/sh 但你能确定它以那种但你能确定它以那种 shellshell 执行么? 执行么?

► 为什么为什么 jettyjetty 的脚本里不那么写,而写为 的脚本里不那么写,而写为

#!/usr/bin/env bash#!/usr/bin/env bash

Page 7: Effective linux.1.(commandline)

了解你所用的了解你所用的 shellshell

► Debian/UbuntuDebian/Ubuntu 下下 /bin/sh/bin/sh 是是 /bin/dash/bin/dash 的一个软连的一个软连接接

► Redhat/CentOS Redhat/CentOS 下 下 /bin/sh/bin/sh 是是 /bin/bash/bin/bash 的软连接的软连接

► $ apt-cache show dash$ apt-cache show dashDebian Almquist Shell (dash)Debian Almquist Shell (dash) 是一款兼容 是一款兼容 POSIX POSIX 的 的 shellshell ,衍生自 ,衍生自 ashash 。由于它执行脚本比 。由于它执行脚本比 bash bash 快,依赖的快,依赖的库少库少 (( 使得它更稳健,受软硬件故障影响少使得它更稳健,受软硬件故障影响少 )) , , Debian Debian 使使用它作为默认的系统 用它作为默认的系统 shellshell 。。

► Dash Dash 与 与 Bash Bash 的差异?的差异?

http://www.igigo.net/archives/169http://www.igigo.net/archives/169

Page 8: Effective linux.1.(commandline)

了解你所用的了解你所用的 shellshell

► shellshell 配置的执行顺序配置的执行顺序

1: /etc/profile1: /etc/profile 2: ~/.bash_profile 2: ~/.bash_profile 如果有则执行如果有则执行

3: ~/.bash_rc 3: ~/.bash_rc 如果有则执行如果有则执行

4: ~/.bash_login 4: ~/.bash_login 如果有则执行如果有则执行

5: ~/.profile 5: ~/.profile 如果有则执行如果有则执行

注:注: redhatredhat 里 里 .bash_rc .bash_rc 是由 是由 .bash_profile.bash_profile 去调用去调用

的 的

Page 9: Effective linux.1.(commandline)

了解你所用的了解你所用的 shellshell

► bash bash 下通过 下通过 set set 和 和 shopt shopt 命令设置命令设置

eg: set –eeg: set –e默认情况下默认情况下 shellshell 脚本中某一行出错,后续的代码仍脚本中某一行出错,后续的代码仍会执行的。当设置了会执行的。当设置了 -e-e 参数,每行命令若以非零值参数,每行命令若以非零值返回返回 ,, 则立即退出。则立即退出。

Every script you write should include set -e Every script you write should include set -e at the top. at the top.

► zshzsh 下通过 下通过 set set 和 和 setopt setopt 命令设置命令设置

Page 10: Effective linux.1.(commandline)

了解你所用的了解你所用的 shellshell► 常见重定向 常见重定向

cmd > file 2>file cmd > file 2>file 与 与 cmd > file 2>&1 cmd > file 2>&1 的差异的差异

command > file 2>file command > file 2>file 的意思是将命令所产生的标准输出的意思是将命令所产生的标准输出信息信息 ,, 和错误的输出信息送到和错误的输出信息送到 file file 中中 ..command > file 2>file command > file 2>file 这样的写法这样的写法 ,stdout,stdout 和和 stderrstderr 都直都直接送到接送到 filefile 中中 , file, file 会被打开两次会被打开两次 ,, 这样这样 stdoutstdout 和和 stderrstderr 会互会互相覆盖相覆盖 ,, 这样写相当使用了这样写相当使用了 FD1FD1 和和 FD2FD2 两个同时去抢占两个同时去抢占 file file 的管道。的管道。

而而 command >file 2>&1 command >file 2>&1 这条命令就将这条命令就将 stdoutstdout 直接送向直接送向file, stderr file, stderr 继承了继承了 FD1FD1 管道后管道后 ,, 再被送往再被送往 file,file, 此时此时 ,file ,file 只只被打开了一次被打开了一次 ,, 也只使用了一个管道也只使用了一个管道 FD1,FD1, 它包括了它包括了 stdoutstdout和和 stderrstderr 的内容。的内容。

Page 11: Effective linux.1.(commandline)

了解你所用的了解你所用的 shellshell

► zsh zsh 中的重定向,花样更多一些中的重定向,花样更多一些

http://www.cs.elte.hu/zsh-manual/zsh_7.htmlhttp://www.cs.elte.hu/zsh-manual/zsh_7.html

或见 或见 zsh-lovers zsh-lovers 中的中的 Redirection-ExamplesRedirection-Examples

Page 12: Effective linux.1.(commandline)

快捷键快捷键

先解决冲突先解决冲突

Alt + F Alt + F 会先被应用程序菜单快捷键所捕获的问题会先被应用程序菜单快捷键所捕获的问题 ::

隐藏隐藏 gnome-terminalgnome-terminal 的的 menubarmenubar$ gconftool --type boolean --set /apps/gnome-$ gconftool --type boolean --set /apps/gnome-terminal/profiles/Default/default_show_menubar falseterminal/profiles/Default/default_show_menubar false

若要想显示,鼠标右键有显示菜单栏的选项。若要想显示,鼠标右键有显示菜单栏的选项。

Page 13: Effective linux.1.(commandline)

快捷键快捷键

► Ctrl + u Ctrl + u 删除整行删除整行► Ctrl + k Ctrl + k 删除光标到行尾删除光标到行尾 (Ctrl +a , Ctrl + k (Ctrl +a , Ctrl + k 相当于 相当于 Ctrl + u)Ctrl + u)

► Ctrl + w Ctrl + w 删除前一个单词 删除前一个单词 Alt + Backspace Alt + Backspace 也是类似效果也是类似效果► Alt + d Alt + d 删除后一个单词删除后一个单词

► Ctrl + h Ctrl + h 向前删除一个字符向前删除一个字符► Ctrl + d Ctrl + d 向后删除一个字符向后删除一个字符

► Ctrl + y Ctrl + y 粘贴所删除的粘贴所删除的

► Ctrl + f Ctrl + f 向右移动 向右移动 Alt + f Alt + f 向右移动一个单词向右移动一个单词► Ctrl + b Ctrl + b 向左移动 向左移动 Alt + b Alt + b 向左移动一个单词向左移动一个单词

Page 14: Effective linux.1.(commandline)

快捷键快捷键

►__ 表示光标所在位置:表示光标所在位置:

►$ command arg1 _ arg2 arg3$ command arg1 _ arg2 arg3

C+A C+E

C+FC+B

Alt+FAlt+B

Page 15: Effective linux.1.(commandline)

快捷键快捷键

► gnome-terminalgnome-terminal 下滚动屏幕的快捷键 下滚动屏幕的快捷键

bashbash 和和 zsh zsh 默认是 默认是 Shift+PgUp Shift+PgUp 和 和 Shift+PgDn Shift+PgDn 用来翻屏用来翻屏。。

Ctrl+Shift+Ctrl+Shift+ 上 上 / Ctrl+Shift+/ Ctrl+Shift+ 下 ,则是向上和向下滚动一下 ,则是向上和向下滚动一行。行。

see: see: http://library.gnome.org/users/gnome-terminal/stable/gnome-terminalhttp://library.gnome.org/users/gnome-terminal/stable/gnome-terminal

Ctrl+PgUp/PgDn Ctrl+PgUp/PgDn 相当于 相当于 Alt+1/2/3 Alt+1/2/3 切换到下一个切换到下一个 tabtabCtrl+Shit+PgUp/PgDn Ctrl+Shit+PgUp/PgDn 则将当前则将当前 tabtab 左移或者右移左移或者右移

注:解决注:解决 screenscreen 中屏幕滚动的问题,中屏幕滚动的问题, see my screenrcsee my screenrc

Page 16: Effective linux.1.(commandline)

快捷键快捷键

► 命令行输入了一半后,发现需要先执行某个命令命令行输入了一半后,发现需要先执行某个命令

比如比如 , cd /tmp/dd , cd /tmp/dd ,还没有输入完,发现需要先创建这个,还没有输入完,发现需要先创建这个目录。以往的做法是 目录。以往的做法是 Ctrl+a Ctrl+a 到命令的前头,输入 到命令的前头,输入

$ mkdir /tmp/dd && cd /tmp/dd$ mkdir /tmp/dd && cd /tmp/dd

简单的方式是,简单的方式是, Ctrl+uCtrl+u 删除此行命令,然后执行其他命令删除此行命令,然后执行其他命令之后,再 之后,再 Ctrl+yCtrl+y 粘贴之前的命令粘贴之前的命令

► zshzsh 下, 用下, 用 alt+q alt+q 可以替代上面的两步 可以替代上面的两步 ctrl+u , ctrl + yctrl+u , ctrl + y

Page 17: Effective linux.1.(commandline)

历史命令历史命令

► 快速执行历史的第快速执行历史的第 nn 个命令个命令

$ history$ history$ !100$ !100

► 快速的执行上一个命令快速的执行上一个命令

$ !!$ !!

► 执行一个命令后,发现权限不足执行一个命令后,发现权限不足

$ sudo !!$ sudo !!

Page 18: Effective linux.1.(commandline)

历史命令历史命令

► 调用上一个命令中的参数调用上一个命令中的参数

eg: eg: 创建了一个路径很长的目录创建了一个路径很长的目录

$ mkdir /verylongpath/dir$ mkdir /verylongpath/dir$ cd $ cd 到刚才创建的目录,要再敲一边?到刚才创建的目录,要再敲一边?

Alt + . Alt + . (或 (或 Esc + .Esc + .)) 可以调出上个命令 可以调出上个命令

的参数的参数

Page 19: Effective linux.1.(commandline)

历史命令历史命令

$ echo $ echo !* !* 上个命令中的所有参数上个命令中的所有参数

$ echo $ echo !^!^ 上个命令中的第一个参数上个命令中的第一个参数

$ echo $ echo !$!$ 上个命令中的最后一个参数上个命令中的最后一个参数

$ echo $ echo !:n!:n 上个命令中的第上个命令中的第 nn 个参数个参数

$ $ !!:p!!:p 显示上个命令,但不执行显示上个命令,但不执行

Page 20: Effective linux.1.(commandline)

历史命令历史命令

►在多个终端中共享历史命令在多个终端中共享历史命令

zsh zsh 在在 .zshrc.zshrc 中增加一行: 中增加一行:

setopt histignorealldups sharehistorysetopt histignorealldups sharehistory

bashbash$ echo "shopt -s histappend" >> ~/.bashrc$ echo "shopt -s histappend" >> ~/.bashrc$ echo "PROMPT_COMMAND=\"history -a; \$PROMPT_COMMAND\"" $ echo "PROMPT_COMMAND=\"history -a; \$PROMPT_COMMAND\"" >> ~/.bashrc>> ~/.bashrc

Page 21: Effective linux.1.(commandline)

历史命令历史命令► 对对 ctrl-r ctrl-r 的增强的增强

bash bash 中绑定 中绑定 up up 和 和 down down 在匹配的条件中选择在匹配的条件中选择

bind '"\e[A": history-search-backward'bind '"\e[A": history-search-backward'bind '"\e[B": history-search-forward'bind '"\e[B": history-search-forward'

zshzsh 中的绑定:中的绑定:

bindkey "^[[A" history-search-backwardbindkey "^[[A" history-search-backwardbindkey "^[[B" history-search-forwardbindkey "^[[B" history-search-forward

这样 这样 ctrl-r ctrl-r 后搜索 后搜索 mvn mvn 然后可以用 然后可以用 up/down up/down 选择所有选择所有mvnmvn 的历史命令的历史命令

或输入 或输入 mvn mvn 然后用然后用 up/down up/down 选择所有历史命令中以选择所有历史命令中以 mvnmvn开头的开头的

Page 22: Effective linux.1.(commandline)

tipstips

► 终端退出后,进程也被中止了,如何避免?终端退出后,进程也被中止了,如何避免?

1) 1) 是是 DaemonDaemon 程序程序 : nohup , disown: nohup , disown

2) 2) 是交互操作:是交互操作: screenscreen

►思考: 思考: java xxx & java xxx & 关闭终端,关闭终端, javajava 进程是否还会进程是否还会

存在?存在? Java –serverJava –server模式呢?模式呢? ppid ppid 变成了谁?变成了谁?

Page 23: Effective linux.1.(commandline)

tipstips

► 正确的结束一个进程正确的结束一个进程

我们的我们的 web serverweb server 脚本里直接用脚本里直接用 kill -9kill -9 是否合适?是否合适?

若若 javajava 程序在程序在 exitexit 时执行了时执行了

hookhook ,, Runtime.getRuntime().addShutdownHookRuntime.getRuntime().addShutdownHook会如何? 会如何?

参考参考 jettyjetty 脚本里的结束方式。脚本里的结束方式。

先发送先发送 HUPHUP 信号信号 (1)(1) ,不灵再 发送,不灵再 发送 KILLKILL 信号信号 (9)(9)

Page 24: Effective linux.1.(commandline)

tipstips

► ScreenScreen 的配置的配置

我的我的 .screenrc.screenrcdefscrollback 30000defscrollback 30000hardstatus alwayslastline "%{=b}%{-b}%{R}[%{=b B}%-w%{=b hardstatus alwayslastline "%{=b}%{-b}%{R}[%{=b B}%-w%{=b BK}%>%n %t%{-}%+w%{-b}%< %=%{R}]%{G}[%Y-%m-%d BK}%>%n %t%{-}%+w%{-b}%< %=%{R}]%{G}[%Y-%m-%d %c]"%c]"termcapinfo xterm* ti@:te@termcapinfo xterm* ti@:te@

设置 设置 screenscreen 默认的默认的 shellshellalias screen='screen -s zsh'alias screen='screen -s zsh'

Page 25: Effective linux.1.(commandline)

tipstips

► bashbash 快速快速 cdcd 到上层任何指定路径到上层任何指定路径

$ upto() { cd "${PWD/\/$@\/*//$@}" }$ upto() { cd "${PWD/\/$@\/*//$@}" }$ pwd$ pwd/a/very/long/cumbersome/directory/a/very/long/cumbersome/directory$ upto very$ upto very$ pwd$ pwd/a/very/a/very

将将 uptoupto函数放入 函数放入 .bashrc .bashrc 里里

Page 26: Effective linux.1.(commandline)

tipstips

►bashbash 里转换大小写的函数里转换大小写的函数

commandfucommandfu 上面看到的:上面看到的:

注意,仅仅限于注意,仅仅限于 bashbash ,也就时说需严格用 ,也就时说需严格用

#!/bin/bash#!/bin/bash$ lower() { echo ${@,,}; }$ lower() { echo ${@,,}; }$ upper() { echo ${@^^}; }$ upper() { echo ${@^^}; }

Page 27: Effective linux.1.(commandline)

tipstips

比较两个比较两个 antx.propertiesantx.properties 不同之处不同之处

$ cat t1 t2 | sort -u | awk -F= '{a[$1]++;m[$1]=m[$1]" "$2}$ cat t1 t2 | sort -u | awk -F= '{a[$1]++;m[$1]=m[$1]" "$2}END{for(i in a) if(a[i] == 2)print i"= "m[i]}'END{for(i in a) if(a[i] == 2)print i"= "m[i]}'

Page 28: Effective linux.1.(commandline)

tipstips

► 20122012年有几个黑色星期五?苹果会不会再年有几个黑色星期五?苹果会不会再

打折?打折?

► $ export LANG=en_US.utf-8$ export LANG=en_US.utf-8► $ for i in {1..12};do ncal -m$i | grep -E "2012|Fr.*13" | $ for i in {1..12};do ncal -m$i | grep -E "2012|Fr.*13" |

grep -B1 "Fr" ; donegrep -B1 "Fr" ; done

Page 29: Effective linux.1.(commandline)

tipstips

► 最简单的通过最简单的通过 httphttp 方式共享当前目录方式共享当前目录

$ python –m SimpleHTTPServer$ python –m SimpleHTTPServer