轻松玩儿转 Bash History -- By Examples
Bash History Expansion 是Bash里面最常用的一个功能。这部分功能在Bash的手册当中分成了三个部分进行描述。分别是 Event Designators,Word Designators 和 Modifiers 。 Event Designators 帮助我们找到曾经用过的命令;Word Designators 可以引用命令当中的不同部分;Modifiers 则提供了对引用内容进行修改的功能。灵活的使用 Bash History Expansion 可以在日常工作中节省大量的键盘输入。
当然在实际使用过程中,这三个部分都是综合起来使用的。下面让我们用一些实例来演示一下history expansion究竟有多么的playful。
Event Designators
- $
- The last argument. It's very useful in safety delete something.
- *
- All of the words but the zeroth. This is a synonym for `1-$'. It is not an error to use * if there is just one word in the event; the empty string is returned in that case.
- !$ 和 !* 是最常用的啦。没有哪一个用法能够比这个更常用。但是你有没有想过用 !$ 和 !* 来增加 rm 的安全性呢?
5 : 2002 : 11:23:14 : ~
dove@bash-4.1$ ls /tmp/Thinkpad_Emergency/
5 : 2003 : 11:24:40 : ~
dove@bash-4.1$ rm -rf !$
rm -rf /tmp/Thinkpad_Emergency/
5 : 2004 : 11:24:49 : ~
dove@bash-4.1$
3 : 2020 : 14:01:01 : ~/FreeMind.html_files
dove@bash-4.1$ ls freemind2html.css *.png
freemind2html.css hide.png ilink.png image.png leaf.png show.png
3 : 2021 : 14:01:12 : ~/FreeMind.html_files
dove@bash-4.1$ rm !*
rm freemind2html.css *.png
3 : 2022 : 14:01:19 : ~/FreeMind.html_files
dove@bash-4.1$ ls
icons
3 : 2023 : 14:01:25 : ~/FreeMind.html_files
dove@bash-4.1$
- ^string1^string2^
- Quick substitution. Repeat the last command, replacingstring1withstring2.Equivalent to``!!:s/string1/string2/''(see Modifiers below).
s/old/new/
Substitute new for the first occurrence of old in the event line. Any delimiter can be used in place of /. The final delimiter is optional if it is the last character of the event line. The delimiter may be quoted in old and new with a single backslash. If & appears in new, it is replaced by old. A single backslash will quote the &. If old is null, it is set to the last old substituted, or, if no previous history substitutions took place,the last string in a !?string[?] search.
替换是另一个很常用的功能。历史总是惊人的相似,历史命令也是这样的。不同的部分通常只是一点点。把不同的替换掉,经常就是你想要的命令了。
s/old/new/ 通常用在 ^old^new^ 够不着了的时候。在进行多个替换的时候还可以用 gs/old/new/ 或者 Gs/old/new/
[root@tivg05 OSAgent]# bin/itmcmd agent -o tivg05 start m6
bin/itmcmd agent -o tivg05 start m6
Starting ... Manager Agent for ...
... Manager Agent for started
[root@tivg05 OSAgent]# bin/cinfo -r
bin/cinfo -r
*********** Tue Sep 14 11:40:22 EDT 2010 ******************
User: root Groups: root bin daemon sys adm disk wheel mqm
Host name : tivg05 Installer Lvl:06.22.02.00
CandleHome: /opt/IBM/OSAgent
***********************************************************
Host Prod PID Owner Start ID ..Status
tivg05 lz 1473 root Sep02 None ...running
tivg05 m6 11749 root 11:39 tivg05 ...running
[root@tivg05 OSAgent]# !-2:s/tivg05/hpi03/
!-2:s/tivg05/hpi03/
bin/itmcmd agent -o hpi03 start m6
Starting... Manager Agent for ...
... Manager Agent for started
[root@tivg05 OSAgent]#
!string
- Refer to the most recent command starting with string.
- !?string[?]
- Refer to the most recent command containingstring.The trailing ? may be omitted ifstringis followed immediately by a newline.
这个是什么?其实呢想一想通常的 !2019,!-4:3-5 等等等等, !string 和 !?string? 的用处就是在你不愿意去数命令的时候,可以使用所包含的特征字符串来定位历史命令。
[root@tivg05 OSAgent]# bin/itmcmd agent -o tivg05 stop m6
bin/itmcmd agent -o tivg05 stop m6
Stopping ... Manager Agent for ...
Product ... Manager Agent for ... was stopped gracefully.
Agent stopped...
[root@tivg05 OSAgent]# bin/cinfo -r
bin/cinfo -r
*********** Tue Sep 14 12:30:21 EDT 2010 ******************
User: root Groups: root bin daemon sys adm disk wheel mqm
Host name : tivg05 Installer Lvl:06.22.02.00
CandleHome: /opt/IBM/OSAgent
***********************************************************
Host Prod PID Owner Start ID ..Status
tivg05 lz 1473 root Sep02 None ...running
tivg05 m6 16210 root 11:41 hpi03 ...running
[root@tivg05 OSAgent]# !?itmcmd?:s/tivg05/hpi03/
!?itmcmd?:s/tivg05/hpi03/
bin/itmcmd agent -o hpi03 stop m6
Stopping ... Manager Agent for ...
Product ... Manager Agent for ... was stopped gracefully.
Agent stopped...
[root@tivg05 OSAgent]#
Word Designators
x-
- Abbreviates x-$ like x*, but omits the last word.
- x- 是一个比较有意思的东西。前面说过, !$ 是所有命令里面最常用的一个,可是偏偏就有这么一种情况,需要变化的碰巧就是最后一个参数,除此之外,其他全都不变。这个时候就是 x- 发挥作用的时候了。下面这个例子篇幅比较长,所以把他折叠起来了
-
Start different ITM agents and then stop them-bash-3.00# bin/itmcmd agent start ux
Starting Monitoring Agent for UNIX OS ...
Monitoring Agent for UNIX OS started
-bash-3.00# !:0- hd
bin/itmcmd agent start hd
Starting Warehouse Proxy ...
Warehouse Proxy started
-bash-3.00# !:0- sy
bin/itmcmd agent start sy
Starting Summarization and Pruning Agent ...
Summarization and Pruning Agent started
-bash-3.00# ^start^stop
bin/itmcmd agent stop sy
Stopping Summarization and Pruning Agent ...
Product Summarization and Pruning Agent was stopped gracefully.
Agent stopped...
-bash-3.00# !:0- hd
bin/itmcmd agent stop hd
Stopping Warehouse Proxy ...
Product Warehouse Proxy was stopped gracefully.
Agent stopped...
-bash-3.00# !:0- ux
bin/itmcmd agent stop ux
Stopping Watchdog process...
Watchdog process was stopped.
Stopping Monitoring Agent for UNIX OS ...
Product Monitoring Agent for UNIX OS was stopped gracefully.
Agent stopped...
-bash-3.00#
Refer to what you are typing on - to restart TEMS without typing twice
- !#
- The entire command line typed so far.
!# 玩儿起来就更加有意思了。 !# 引用的是当前正在编写的命令。被历史命令引用,并不必须非得成为过去。
bash-3.00# bin/itmcmd server stop `hostname -s` && !#:0-1 start !#:3
bin/itmcmd server stop `hostname -s` && bin/itmcmd server start `hostname -s`
Stopping TEMS...
It can take up to ten minutes.
TEMS stopped...
Starting TEMS...
It can take up to ten minutes.
TEMS started...
bash-3.00#
M-#
Comment out your current input, put it into history list, then get back to the prompt
It's very useful when you have to issue another command in advance while typing this one
M-# 是一个值得大书特书的功能。想一想吧,每个人都遇到过输入一条命令,输到一半的时候突然发现需要先去执行另外一条命令,哎呀那个尴尬啊。其实这个时候只要同时按下键盘上的 Alt和#键(实际上就是Alt Shift 3),这个半拉子命令就被注释掉了,并且还被加到了Bash的history list里面了。当你执行完“别的”命令,只需要叫回这条注释,替换掉 # 号就可以执行了。
2 : 2038 : 11:36:22 : ~
dove@bash-4.1$ #mv ~/tivx06_m6_tivvm241_km6agent_4c733691-01.log.zip
2 : 2039 : 11:36:28 : ~
dove@bash-4.1$ mkdir /tmp/ThinkPad-Emergency
2 : 2040 : 11:36:37 : ~
dove@bash-4.1$ !-2:s/#// !$
mv ~/tivx06_m6_tivvm241_km6agent_4c733691-01.log.zip /tmp/ThinkPad-Emergency
2 : 2041 : 11:36:47 : ~
dove@bash-4.1$
Modifiers
After the optional word designator, there may appear a sequence ofone or more of the following modifiers, each preceded by a `:'.
Modifiers 是一些更进一步的编辑命令,通常和上面的命令结合使用。但是鉴于替换的规则不甚直观,个人认为实用价值比较有限。
- h
- Remove a trailing file name component, leaving only the head.
- t
- Remove all leading file name components, leaving the tail.
- r
- Remove a trailing suffix of the form .xxx, leaving thebasename.
- e
- Remove all but the trailing suffix.
tivp5flp2.cn.ibm.com|/opt/IBM # KM6REM/bin/cinfo -r
*********** Wed Sep 15 14:00:34 GMT+08:00 2010 ******************
User: root Groups: system bin sys security cron audit lp mqm
Host name : tivp5flp2 Installer Lvl:06.22.02.00
CandleHome: /opt/IBM/KM6REM
***********************************************************
Host Prod PID Owner Start ID ..Status
tivp5flp2 m6 794860 root Aug ma1 ...running
tivp5flp2.cn.ibm.com|/opt/IBM # !:h/itmcmd agent -o ma1 stop m6
KM6REM/bin/itmcmd agent -o ma1 stop m6
Stopping ... Manager Agent for ...
Product ... Manager Agent for ... was stopped gracefully.
Agent stopped...
tivp5flp2.cn.ibm.com|/opt/IBM #
3 : 2007 : 12:51:05 : ~/org
dove@bash-4.1$ ls /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log.zip
/tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log.zip
3 : 2008 : 12:51:16 : ~/org
dove@bash-4.1$ echo !:$:r
echo /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log
/tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log
3 : 2009 : 12:51:30 : ~/org
dove@bash-4.1$ pwd
/home/dove/org
3 : 2010 : 12:51:56 : ~/org
dove@bash-4.1$ echo !-2:$:r
echo /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01
/tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01
3 : 2011 : 12:52:12 : ~/org
dove@bash-4.1$ echo !-4:$:r
echo /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log
/tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log
3 : 2012 : 12:52:36 : ~/org
dove@bash-4.1$
some
0 : 2009 : 22:11:27 : ~
dove@bash-4.1$ ls -a Shell/config/emacs.el/.abbrev_defs
Shell/config/emacs.el/.abbrev_defs
0 : 2010 : 22:11:36 : ~
dove@bash-4.1$ cat !$
cat Shell/config/emacs.el/.abbrev_defs
*** output flushed ***
dove@bash-4.1$ diff !$ !#:t
diff Shell/config/emacs.el/.abbrev_defs .abbrev_defs
*** output flushed ***
dove@bash-4.1$
-bash-3.2# cd /opt/IBM/WMQFTE/bin
cd /opt/IBM/WMQFTE/bin
-bash-3.2# history | grep -i Create
history | grep -i Create
119 for (( i=0; i<200; i++ )) ; do ./fteCreateTransfer -sa AGTCOD -da AGTB -dd /tmp/ -de overwrite tmv_cq_silent_config.txt.abc ; sleep 10 ; done
142 cd createBroker/
231 for (( i=0; i<200; i++ )) ; do ./fteCreateTransfer -sa AGTCOD -da AGTB -dd /tmp/ -de overwrite tmv_cq_silent_config.txt.abc ; sleep 10 ; done
233 for (( i=0; i<200; i++ )) ; do ./fteCreateTransfer -sa AGTCOD -da AGTB -dd /tmp/ -de overwrite tmv_cq_silent_config.txt.abc ; sleep 10 ; done
237 for (( i=0; i<200; i++ )) ; do ./fteCreateTransfer -sa AGTCOD -da AGTB -dd /tmp/ -de overwrite tmv_cq_silent_config.txt.abc ; sleep 10 ; done
240 for (( i=0; i<200; i++ )) ; do ./fteCreateTransfer -sa AGTCOD -da AGTB -dd /tmp/ -de overwrite tmv_cq_silent_config.txt.abc ; sleep 10 ; done
275 for (( i=0; i<100; i++ )) ; do ./fteCreateTransfer -sa AGTCOD -da AGTB -dd /tmp/ -de overwrite a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 ; sleep 10 ; done
380 for (( i=0; i<100; i++ )) ; do ./fteCreateTransfer -sa AGTCOD -da AGTB -dd /tmp/ -de overwrite a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 ; sleep 10 ; done
382 ./fteCreateTransfer -sa AGTB -da AGTCOD -dd /tmp -de overwrite -ss 16:20 -oi minutes -of 1 -oc 10 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
383 ./fteCreateTransfer -sa AGTB -da AGTCOD -dd /tmp -de overwrite -ss 16:21 -oi minutes -of 1 -oc 10 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
384 ./fteCreateTransfer -sa AGTB -da AGTCOD -dd /tmp -de overwrite -ss 16:21 -oi minutes -of 2 -oc 10 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
385 ./fteCreateTransfer -sa AGTB -da AGTCOD -dd /tmp -de overwrite -ss 16:23 -oi minutes -of 2 -oc 10 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
502 history | grep -i Create
-bash-3.2# date
date
Mon Sep 20 15:14:27 CST 2010
-bash-3.2# !384:s/16:21/14:30/
!384:s/16:21/14:30/
./fteCreateTransfer -sa AGTB -da AGTCOD -dd /tmp -de overwrite -ss 14:30 -oi minutes -of 2 -oc 10 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
5655-U80, 5724-R10 Copyright IBM Corp. 2008, 2009. ALL RIGHTS RESERVED
BFGCL0035I: Transfer request issued. The request ID is: 414d5120514d434f44202020202020204c95c4202004f17e
BFGCL0182I: The request is now waiting to be processed by the agent.
-bash-3.2#
the end
posted on 2010-09-14 14:41 David Young 杨博华 阅读(748) 评论(0) 编辑 收藏 举报