北京SEO

Unix shell 使用 Bash 中的 globstart 选项使用教程

2019/10/10/17:46:42  阅读:2452  来源:谷歌SEO算法  标签: 友情链接作弊

我们可以用一个神奇的通配符“**”(两个星号)在ls命令方便的遍历所有的目录和文件,本文我们来告诉你如何设置Bash的globstar选项后,使用这个神奇的通配符.

在使用一些命令时(如:ls、git),刚好遇到一些需求是想很方便地遍历所有的目录和文件,后来经过搜索,终于找到了一个“神奇”的通配符 “**”(两个星号),在设置了Bash的globstar选项后,**就可以匹配任当前何目录(包括子目录)以及其中的文件,所以,了解了一下 globstar这个选项,当未设置globstar时,**通配符的作用和*是相同的,而设置了globstar后,**的匹配范围不同了(更广一些),注意:globstar是Bash 4.0才引入的选项,之前的老版本是不支持的,使用“bash –version”可产看当前使用的Bash的版本.

关于glob这个词,我也觉得好奇,中文不好解释,大致就是“对通配符展开”的意思,如下的英文吧:

In shell-speak, globbing is what the shell does when you use a wildcard in a command (e.g. * or ?). Globbing is matching the wildcard pattern and returning the file and directory names that match and then replacing the wildcard pattern in the command with the matched items.

在bash的man page中,对globstar的说明提到只两次,说的都是同一件事情,代码如下:

  1. PathnameExpansion
  2. ......
  3. *Matchesanystring,includingthenullstring.Whentheglobstarshelloptionisenabled,and*is
  4. usedinapathnameexpansioncontext,twoadjacent*susedasasinglepatternwillmatchallfiles
  5. andzeroormoredirectoriesandsubdirectories.Iffollowedbya/,twoadjacent*swillmatchonly
  6. directoriesandsubdirectories.
  7. ......
  8. globstar
  9. Ifset,thepattern**usedinapathnameexpansioncontextwillmatchafilesandzeroormoredirectories
  10. andsubdirectories.Ifthepatternisfollowedbya/,onlydirectoriesandsubdirectoriesmatch.

写了个测试和学习globstar的shell脚本如下:

  1. #!/bin/bash
  2. <prelang="Bash">
  3. functionshow()
  4. {
  5. foriin**
  6. do
  7. echo$i
  8. done
  9. }--phpfensi.com
  10. cd/root/jay/
  11. echo"------------------------"
  12. echo"disableglobstaroption:"
  13. #globstarisdisabledbydefault
  14. shopt-uglobstar
  15. show
  16. echo"------------------------"
  17. echo"enableglobstaroption:"
  18. shopt-sglobstar
  19. show

执行上面测试globstar的shell脚本,看它的输出结果,就很容易理解globstar了,如下:

  1. [root@smilejayjay]#./test_globstar.sh
  2. ------------------------
  3. disableglobstaroption:
  4. dir1
  5. dir2
  6. file1
  7. file2
  8. index.html
  9. test_shopt.sh
  10. ------------------------
  11. enableglobstaroption:
  12. dir1
  13. dir1/file3
  14. dir2
  15. dir2/file4
  16. file1
  17. file2
  18. index.html
  19. test_shopt.sh

广告内容

Unix shell 使用 Bash 中的 globstart 选项使用教程 Unix shell 使用 Bash 中的 globstart 选项使用教程 Unix shell 使用 Bash 中的 globstart 选项使用教程

相关阅读

热门评论

sunshine技术博客 sunshine技术博客

sunshine技术博客

总篇数164

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法