免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2174 | 回复: 6
打印 上一主题 下一主题

来一点儿不水的:轻松玩儿转 Bash History -- By Examples [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-17 13:12 |只看该作者 |倒序浏览
这个玩意儿看一下 man bash 其实就全有了。这里只是把它真正的用起来


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 的安全性呢?
  1. 3 : 2020 : 14:01:01 : ~/FreeMind.html_files
  2. dove@bash-4.1$ ls freemind2html.css *.png
  3. freemind2html.css  hide.png  ilink.png  image.png  leaf.png  show.png

  4. 3 : 2021 : 14:01:12 : ~/FreeMind.html_files
  5. dove@bash-4.1$ rm !*
  6. rm freemind2html.css *.png

  7. 3 : 2022 : 14:01:19 : ~/FreeMind.html_files
  8. dove@bash-4.1$ ls
  9. icons

  10. 3 : 2023 : 14:01:25 : ~/FreeMind.html_files
  11. 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/
  1. [root@tivg05 OSAgent]# bin/itmcmd agent -o tivg05 start m6
  2. bin/itmcmd agent -o tivg05 start m6
  3. Starting ... Manager Agent for  ...
  4. ... Manager Agent for started
  5. [root@tivg05 OSAgent]# bin/cinfo -r
  6. bin/cinfo -r

  7. *********** Tue Sep 14 11:40:22 EDT 2010 ******************
  8. User: root Groups: root bin daemon sys adm disk wheel mqm
  9. Host name : tivg05     Installer Lvl:06.22.02.00
  10. CandleHome: /opt/IBM/OSAgent
  11. ***********************************************************
  12. Host    Prod  PID    Owner  Start  ID      ..Status  
  13. tivg05  lz    1473   root   Sep02  None    ...running
  14. tivg05  m6    11749  root   11:39  tivg05  ...running
  15. [root@tivg05 OSAgent]# !-2:s/tivg05/hpi03/
  16. !-2:s/tivg05/hpi03/
  17. bin/itmcmd agent -o hpi03 start m6
  18. Starting... Manager Agent for  ...
  19. ... Manager Agent for started
  20. [root@tivg05 OSAgent]#
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-09-17 13:14 |只看该作者
!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? 的用处就是在你不愿意去数命令的时候,可以使用所包含的特征字符串来定位历史命令。


  1. [root@tivg05 OSAgent]# bin/itmcmd agent -o tivg05 stop m6
  2. bin/itmcmd agent -o tivg05 stop m6
  3. Stopping ... Manager Agent for  ...
  4. Product ... Manager Agent for ... was stopped gracefully.
  5. Agent stopped...
  6. [root@tivg05 OSAgent]# bin/cinfo -r
  7. bin/cinfo -r
  8. *********** Tue Sep 14 12:30:21 EDT 2010 ******************
  9. User: root Groups: root bin daemon sys adm disk wheel mqm
  10. Host name : tivg05     Installer Lvl:06.22.02.00
  11. CandleHome: /opt/IBM/OSAgent
  12. ***********************************************************
  13. Host    Prod  PID    Owner  Start  ID     ..Status  
  14. tivg05  lz    1473   root   Sep02  None   ...running
  15. tivg05  m6    16210  root   11:41  hpi03  ...running
  16. [root@tivg05 OSAgent]# !?itmcmd?:s/tivg05/hpi03/
  17. !?itmcmd?:s/tivg05/hpi03/
  18. bin/itmcmd agent -o hpi03 stop m6
  19. Stopping ... Manager Agent for  ...
  20. Product ... Manager Agent for ... was stopped gracefully.
  21. Agent stopped...
  22. [root@tivg05 OSAgent]#
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-09-17 13:16 |只看该作者
Word Designators

x-

    Abbreviates x-$ like x*, but omits the last word.
x- 是一个比较有意思的东西。前面说过, !$ 是所有命令里面最常用的一个,可是偏偏就有这么一种情况,需要变化的碰巧就是最后一个参数,除此之外,其他全都不变。这个时候就是 x- 发挥作用的时候了。下面这个例子篇幅比较长,所以把他折叠起来了
  1. -bash-3.00# bin/itmcmd agent start ux
  2. Starting Monitoring Agent for UNIX OS ...
  3. Monitoring Agent for UNIX OS started
  4. -bash-3.00# !:0- hd
  5. bin/itmcmd agent start hd
  6. Starting Warehouse Proxy ...
  7. Warehouse Proxy started
  8. -bash-3.00# !:0- sy
  9. bin/itmcmd agent start sy
  10. Starting Summarization and Pruning Agent ...
  11. Summarization and Pruning Agent started
  12. -bash-3.00# ^start^stop
  13. bin/itmcmd agent stop sy
  14. Stopping Summarization and Pruning Agent ...
  15. Product Summarization and Pruning Agent was stopped gracefully.
  16. Agent stopped...
  17. -bash-3.00# !:0- hd
  18. bin/itmcmd agent stop hd
  19. Stopping Warehouse Proxy ...
  20. Product Warehouse Proxy was stopped gracefully.
  21. Agent stopped...
  22. -bash-3.00# !:0- ux
  23. bin/itmcmd agent stop ux
  24. Stopping Watchdog process...
  25. Watchdog process was stopped.
  26. Stopping Monitoring Agent for UNIX OS ...
  27. Product Monitoring Agent for UNIX OS was stopped gracefully.
  28. Agent stopped...
  29. -bash-3.00#
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-09-17 13:18 |只看该作者
!#

    The entire command line typed so far.

!# 玩儿起来就更加有意思了。 !# 引用的是当前正在编写的命令。被历史命令引用,并不必须非得成为过去

  1. bash-3.00# bin/itmcmd server stop `hostname -s` && !#:0-1 start !#:3
  2. bin/itmcmd server stop `hostname -s` && bin/itmcmd server start `hostname -s`
  3. Stopping TEMS...
  4. It can take up to ten minutes.
  5. TEMS stopped...
  6. Starting TEMS...
  7. It can take up to ten minutes.
  8. TEMS started...
  9. 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里面了。当你执行完“别的”命令,只需要叫回这条注释,替换掉 # 号就可以执行了。

  1. 2 : 2038 : 11:36:22 : ~
  2. dove@bash-4.1$ #mv ~/tivx06_m6_tivvm241_km6agent_4c733691-01.log.zip

  3. 2 : 2039 : 11:36:28 : ~
  4. dove@bash-4.1$ mkdir /tmp/ThinkPad-Emergency

  5. 2 : 2040 : 11:36:37 : ~
  6. dove@bash-4.1$ !-2:s/#// !$
  7. mv ~/tivx06_m6_tivvm241_km6agent_4c733691-01.log.zip /tmp/ThinkPad-Emergency

  8. 2 : 2041 : 11:36:47 : ~
  9. dove@bash-4.1$
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-09-17 13:20 |只看该作者
Modifiers
After the optional word designator, there may appear a sequence ofone or more of the following modifiers, each preceded by a `:'.

Modifiers 是一些更进一步的编辑命令,通常和上面的命令结合使用。但是鉴于替换的规则不甚直观,个人认为实用价值比较有限。hRemove a trailing file name component, leaving only the head.tRemove all leading file name components, leaving the tail.rRemove a trailing suffix of the form .xxx, leaving thebasename.eRemove all but the trailing suffix.
  1. tivp5flp2.cn.ibm.com|/opt/IBM # KM6REM/bin/cinfo -r

  2. *********** Wed Sep 15 14:00:34 GMT+08:00 2010 ******************
  3. User: root Groups: system bin sys security cron audit lp mqm
  4. Host name : tivp5flp2    Installer Lvl:06.22.02.00
  5. CandleHome: /opt/IBM/KM6REM
  6. ***********************************************************
  7. Host       Prod  PID     Owner  Start  ID   ..Status  
  8. tivp5flp2  m6    794860  root   Aug    ma1  ...running
  9. tivp5flp2.cn.ibm.com|/opt/IBM # !:h/itmcmd agent -o ma1 stop m6
  10. KM6REM/bin/itmcmd agent -o ma1 stop m6
  11. Stopping ... Manager Agent for  ...
  12. Product ... Manager Agent for ... was stopped gracefully.
  13. Agent stopped...
  14. tivp5flp2.cn.ibm.com|/opt/IBM #
复制代码

  1. 3 : 2007 : 12:51:05 : ~/org
  2. dove@bash-4.1$ ls /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log.zip
  3. /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log.zip

  4. 3 : 2008 : 12:51:16 : ~/org
  5. dove@bash-4.1$ echo !:$:r
  6. echo /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log
  7. /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log

  8. 3 : 2009 : 12:51:30 : ~/org
  9. dove@bash-4.1$ pwd
  10. /home/dove/org

  11. 3 : 2010 : 12:51:56 : ~/org
  12. dove@bash-4.1$ echo !-2:$:r
  13. echo /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01
  14. /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01

  15. 3 : 2011 : 12:52:12 : ~/org
  16. dove@bash-4.1$ echo !-4:$:r
  17. echo /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log
  18. /tmp/ThinkPad-Emergency/tivx06_m6_tivvm241_km6agent_4c733691-01.log

  19. 3 : 2012 : 12:52:36 : ~/org
  20. dove@bash-4.1$
复制代码

论坛徽章:
0
6 [报告]
发表于 2010-09-17 13:21 |只看该作者
  1. 0 : 2009 : 22:11:27 : ~
  2. dove@bash-4.1$ ls -a Shell/config/emacs.el/.abbrev_defs
  3. Shell/config/emacs.el/.abbrev_defs

  4. 0 : 2010 : 22:11:36 : ~
  5. dove@bash-4.1$ cat !$
  6. cat Shell/config/emacs.el/.abbrev_defs
  7. *** output flushed ***

  8. dove@bash-4.1$ diff !$ !#:t
  9. diff Shell/config/emacs.el/.abbrev_defs .abbrev_defs
  10. *** output flushed ***

  11. dove@bash-4.1$
复制代码

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
7 [报告]
发表于 2010-09-17 13:25 |只看该作者
路过,友情帮顶
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP