仓酷云

标题: 带来一篇shell中利用Bash中的globstart选项 [打印本页]

作者: 透明    时间: 2015-1-14 20:17
标题: 带来一篇shell中利用Bash中的globstart选项
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!在利用一些下令时(如:ls、git),恰好碰到一些需求是想很便利地遍历一切的目次和文件,厥后经由搜刮,终究找到了一个“奇妙”的通配符“**”(两个星号),在设置了Bash的globstar选项后,**就能够婚配任以后何目次(包含子目次)和个中的文件。以是,懂得了一下globstar这个选项,当未设置globstar时,**通配符的感化和*是不异的,而设置了globstar后,**的婚配局限分歧了(更广一些)。注重:globstar是Bash4.0才引进的选项,之前的老版本是不撑持的,利用“bashCversion”可产看以后利用的Bash的版本。
关于glob这个词,我也以为猎奇,中文欠好注释,大抵就是“对通配符睁开”的意义,以下的英文吧:
Inshell-speak,globbingiswhattheshelldoeswhenyouuseawildcardinacommand(e.g.*or?).Globbingismatchingthewildcardpatternandreturningthefileanddirectorynamesthatmatchandthenreplacingthewildcardpatterninthecommandwiththematcheditems.

在bash的manpage中,对globstar的申明提到只两次,说的都是统一件事变,以下:
  1. PathnameExpansion......*Matchesanystring,includingthenullstring.Whentheglobstarshelloptionisenabled,and*isusedinapathnameexpansioncontext,twoadjacent*susedasasinglepatternwillmatchallfilesandzeroormoredirectoriesandsubdirectories.Iffollowedbya/,twoadjacent*swillmatchonlydirectoriesandsubdirectories.......globstarIfset,thepattern**usedinapathnameexpansioncontextwillmatchafilesandzeroormoredirectoriesandsubdirectories.Ifthepatternisfollowedbya/,onlydirectoriesandsubdirectoriesmatch.
复制代码
写了个测试和进修globstar的shell剧本以下:
  1. #!/bin/bash<prelang="Bash">functionshow(){foriin**doecho$idone}cd/root/jay/echo"------------------------"echo"disableglobstaroption:"#globstarisdisabledbydefaultshopt-uglobstarshowecho"------------------------"echo"enableglobstaroption:"shopt-sglobstarshow
复制代码
实行下面测试globstar的shell剧本,看它的输入了局,就很简单了解globstar了,以下:
  1. [root@smilejayjay]#./test_globstar.sh------------------------disableglobstaroption:dir1dir2file1file2index.htmltest_shopt.sh------------------------enableglobstaroption:dir1dir1/file3dir2dir2/file4file1file2index.htmltest_shopt.sh
复制代码
参考材料:
http://www.linuxjournal.com/content/globstar-new-bash-globbing-option
bash4引进的新feature:http://wiki.bash-hackers.org/bash4
http://wiki.bash-hackers.org/syntax/expansion/globs


如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!
作者: 金色的骷髅    时间: 2015-1-15 15:26
标题: 带来一篇shell中利用Bash中的globstart选项
顶顶更健康
作者: 仓酷云    时间: 2015-1-25 20:42
我想即使Linux高手也很难快速准确精练的回答你。
作者: 分手快乐    时间: 2015-2-4 12:04
编程学习及开发,Linux是免费,开源的操作系统,并且可开发工具相当多,如果您支持自由软件,一定要同广大热爱自由软件人士一同为其不懈努力。
作者: 简单生活    时间: 2015-2-9 22:34
linux鸟哥的私房菜,第三版,基础篇,网上有pdf下的,看它的目录和每章的介绍就行了,这个绝对原创!
作者: 透明    时间: 2015-3-10 01:41
直到学习Linux这门课以后,我才知道,原来我错了。?
作者: 乐观    时间: 2015-3-24 05:50
随着实验课程的结束,理论课也该结束了,说实话教OS的这两位老师是我们遇到过的不错的老师(这话放这可能不太恰当).




欢迎光临 仓酷云 (http://www.ckuyun.com/) Powered by Discuz! X3.2