Unix shell 使用 Bash 中的 globstart 选项使用教程
发布:smiling 来源: PHP粉丝网 添加日期:2015-04-21 13:05:41 浏览: 评论:0
我们可以用一个神奇的通配符“**”(两个星号)在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的说明提到只两次,说的都是同一件事情,代码如下:
- Pathname Expansion
- ......
- * Matches any string, including the null string. When the globstar shell option is enabled, and * is
- used in a pathname expansion context, two adjacent *s used as a single pattern will match all files
- and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only
- directories and subdirectories.
- ......
- globstar
- If set, the pattern ** used in a pathname expansion context will match a files and zero or more directories
- and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.
写了个测试和学习globstar的shell脚本如下:
- #!/bin/bash
- <pre lang="Bash">
- function show()
- {
- for i in **
- do
- echo $i
- done
- } --phpfensi.com
- cd /root/jay/
- echo "------------------------"
- echo "disable globstar option:"
- # globstar is disabled by default
- shopt -u globstar
- show
- echo "------------------------"
- echo "enable globstar option:"
- shopt -s globstar
- show
执行上面测试globstar的shell脚本,看它的输出结果,就很容易理解globstar了,如下:
- [root@smilejay jay]# ./test_globstar.sh
- ------------------------
- disable globstar option:
- dir1
- dir2
- file1
- file2
- index.html
- test_shopt.sh
- ------------------------
- enable globstar option:
- dir1
- dir1/file3
- dir2
- dir2/file4
- file1
- file2
- index.html
- test_shopt.sh
Tags: Unix shell globstart
相关文章
- ·详解Unix和Linux操作系统中Cron的用法(2014-02-28)
- ·学习linux/unix编程方法的建议,学习Linux的四个步骤(2014-03-06)
- ·Linux shell学习之:unix/linux shell的发展历程(2014-03-07)
- ·UNIX和Linux Shell正则表达式语法介绍(2014-03-08)
- ·突破极限 解决大硬盘上安装Sco Unix新思路(2014-03-09)
- ·介绍UNIX系统的五种关机方法(2014-03-09)
- ·ODU在Linux和Unix平台下的使用(2014-03-11)
- ·Unix平台下如何卸载gdb调试工具?(2015-04-22)
- ·Unix sed流编辑器简介 带你入门sed(2015-04-22)
- ·Unix sed如何向文件中增加一行(2015-04-22)
- ·Unix sed编辑器如何替换文件内容(2015-04-22)
- ·Unix sed编辑器如何读写文件操作(2015-04-22)

推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)