仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 892|回复: 6
打印 上一主题 下一主题

[shell编程] 带来一篇shell中利用Bash中的globstart选项

[复制链接]
透明 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-14 20:17:32 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
如果您觉得本篇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-3-10 01:41:11 | 显示全部楼层
直到学习Linux这门课以后,我才知道,原来我错了。?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-13 23:57

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表