Shell编程之通配符

简介:

   Bash Shell本身不支持正则表达式,使用正则表达式的是Shell命令和工具,如grep、sed、awk

等。但是,Bash Shell可以使用正则表达式中的一些元字符实现通配(Globbing)功能。

   通配是把一个包含通配符的非具体文件名扩展存储在计算机、服务器或者网络上的一批具体文件

名的过程。最常用的通配符包括正则表达式元字符:?、*、[]、{}、^等。这些元字符在通配中的意义

与正则表达式中的意义不完全一致,*符号不再表示其前面字符的重复,而是表示任意位的任意字

符,?字符表示一个任意字符,^符号在通配中不代表行首,而是代表取反。


例如,如果一个用户不知道在一个扩展名为.conf的文件名中前缀是如何拼写的,是 prelink还

是prilink。这时用户可以输入:

 
1
pr *link.conf


下面举几个例子来说明通配的使用和通配元字符的意义,这些例子都用ls命令进行通配,ls 命令是Linux 下最常用的命令之一,它用于列出目录下的文件,它可以有很多选项,ls -l表示列出文件的详细信息,ll命令等价于ls -l命令。/usr/local/zabbix/etc 目录下的所有文件如下所示:

 
1
2
3
4
5
6
7
8
9
10
[root@zabbix etc] # pwd
/usr/local/zabbix/etc
[root@zabbix etc] # ll
总计 36
-rw-r--r-- 1 root root  1601 11-06 12:18 zabbix_agent.conf
drwxr-xr-x 2 root root  4096 11-06 12:18 zabbix_agent.conf.d
-rw-r--r-- 1 root root  7191 11-14 11:46 zabbix_agentd.conf
drwxr-xr-x 2 root root  4096 11-06 12:18 zabbix_agentd.conf.d
-rw-r--r-- 1 root root 10534 11-06 18:19 zabbix_server.conf
drwxr-xr-x 2 root root  4096 11-06 12:18 zabbix_server.conf.d


/usr/local/zabbix/etc 目录下包含三个子目录,子目录的详细信息以d 开头,d 表示directory的意思,其他以横杠(-)开头的都是文件。

如果我们仅需要列出/usr/local/zabbix/etc 目录下以.conf 结尾的文件,就可以使用*.conf 匹配所有以.conf结尾的文件,如下所示:

 
1
2
3
4
5
#列出以.conf结尾的文件的详细信息
[root@zabbix etc] # ll *.conf
-rw-r--r-- 1 root root  1601 11-06 12:18 zabbix_agent.conf
-rw-r--r-- 1 root root  7191 11-14 11:46 zabbix_agentd.conf
-rw-r--r-- 1 root root 10534 11-06 18:19 zabbix_server.conf


如果我们需列出以zabbix_s开头、后面跟5 个字符且以.conf 为后缀的文件,可以使用zabbix_s?????.conf来匹配这些文件,如下面所示。

 
1
2
3
#列出以zabbix_s开头后跟5个字母,以conf结尾的文件
[root@zabbix etc] # ls -l zabbix_s?????.conf
-rw-r--r-- 1 root root 10534 11-06 18:19 zabbix_server.conf




下面举一个用[]符号进行通配的例子,若我们需列出在l~n范围内以字母开头并以.conf结尾的文件,我们可以用[l-n]*.conf来匹配这些文件,如下面所示。


目录/etc 下有248个文件及目录:

 
1
2
3
4
[root@zabbix etc] # pwd
/etc
[root@zabbix etc] # ll | wc -l
248


列出以l~n范围内字母开头,且以.conf结尾的文件的详细信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@zabbix etc] # ll [l-n]*.conf
-rw-r--r-- 1 root root  9111 10-30 15:58 ldap.conf
-rw-r--r-- 1 root root   124 11-06 15:31 ld.so.conf
-rw-r--r-- 1 root root  3544 2013-01-09 lftp.conf
-rw-r----- 1 root root   191 2011-10-27 libaudit.conf
-rw-r--r-- 1 root root  2506 10-30 15:58 libuser.conf
-rw-r--r-- 1 root root   619 2012-06-04 logrotate.conf
-rw-r--r-- 1 root root 10814 2006-02-21 ltrace.conf
-rwxr-xr-x 1 root root  2242 2011-05-19 mcelog.conf
-rw-r--r-- 1 root root   330 2013-05-09 mke2fs.conf
-rw-r--r-- 1 root root   315 10-30 15:59 modprobe.conf
-rw-r--r-- 1 root root  1983 2007-01-07  mtools .conf
-rw-r--r-- 1 root root  2706 10-02 06:18 multipath.conf
-rw-r--r-- 1 root root  1895 10-01 18:22 nscd.conf
-rw-r--r-- 1 root root  1717 10-30 15:39 nsswitch.conf
-rw-r--r-- 1 root root  1839 2011-11-18 ntp.conf


可见结果确实仅列出以l~n 范围内字母开头且以.conf 结尾的文件。

如果我们要列出以l~n范围内字母开头且句点后不是以.conf结尾的文件,可以使用[l-n]*.[^conf]*来匹配这些文件,句点后面方括号内使用"^"符号表示取反,即除去c、o、n和f这四个字母,而且最后一个*符号必不可少,否则句点后仅匹配一个字符,下面命令给出了[l-n]*.[^conf]*的匹配结果。


列出以l~n范围内字母开头,不以.conf结尾的文件


1
2
3
4
5
6
7
8
9
[root@zabbix etc] # ll [l-n]*.[^conf]*
-rw-r--r-- 1 root root 105305 11-06 16:19 ld.so.cache
-rw-r--r-- 1 root root    124 11-06 15:31 ld.so.conf
-rw-r--r-- 1 root root   1522 10-30 15:58 login.defs
-rw-r--r-- 1 root root    112 2007-01-07 mail.rc
-rw-r--r-- 1 root root  14100 2007-01-07 mime.types
-rw-r--r-- 1 root root   1112 2007-01-07 minicom. users
-rw-r--r-- 1 root root      0 2012-08-06 Muttrc. local
-rw-r--r-- 1 root root    441 2013-01-23 my.cnf.rpmnew
 


由上可知,[]符号的意义与正则表达式中[]符号的意义一样,那么通配中的花括号"{}"表示何种意义呢?正则表达式中只有在花括号前加上转义符的用法,即\{\},用于限制匹配字符的个数。但是,通配中的{}符号表示一组表达式的集合,如:

 
1
{[l-n]*.conf ,y?.conf}


上述通配表示满足[l-n]*.conf或y?.conf的所有文件,下面给出了这一通配的执行结果。

 

列出匹配[l-n]*.conf 或 y?.conf的所有文件  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@zabbix etc] # ll {[l-n]*.conf,y?.conf}
-rw-r--r-- 1 root root  9111 10-30 15:58 ldap.conf
-rw-r--r-- 1 root root   124 11-06 15:31 ld.so.conf
-rw-r--r-- 1 root root  3544 2013-01-09 lftp.conf
-rw-r----- 1 root root   191 2011-10-27 libaudit.conf
-rw-r--r-- 1 root root  2506 10-30 15:58 libuser.conf
-rw-r--r-- 1 root root   619 2012-06-04 logrotate.conf
-rw-r--r-- 1 root root 10814 2006-02-21 ltrace.conf
-rwxr-xr-x 1 root root  2242 2011-05-19 mcelog.conf
-rw-r--r-- 1 root root   330 2013-05-09 mke2fs.conf
-rw-r--r-- 1 root root   315 10-30 15:59 modprobe.conf
-rw-r--r-- 1 root root  1983 2007-01-07  mtools .conf
-rw-r--r-- 1 root root  2706 10-02 06:18 multipath.conf
-rw-r--r-- 1 root root  1895 10-01 18:22 nscd.conf
-rw-r--r-- 1 root root  1717 10-30 15:39 nsswitch.conf
-rw-r--r-- 1 root root  1839 2011-11-18 ntp.conf
-rw-r--r-- 1 root root   585 2011-06-30 yp.conf


注意: {}符号内的表达式是"或"的关系,即只要符合{}符号内的一个表达式的文件,就能被列出。




     本文转自marbury 51CTO博客,原文链接:http://blog.51cto.com/magic3/1353068,如需转载请自行联系原作者



相关文章
|
2月前
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
57 0
|
3月前
|
Shell Linux C++
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
38 0
|
3月前
|
Shell 数据安全/隐私保护
shell中的通配符 熟悉grep、cut、sort等小工具和shell中的通配符的使用
shell中的通配符 熟悉grep、cut、sort等小工具和shell中的通配符的使用
33 0
|
2天前
|
监控 Shell 开发工具
Shell编程
Shell编程
|
22天前
|
存储 Java Shell
bigdata-04-shell编程基础
bigdata-04-shell编程基础
10 0
|
24天前
|
Shell Linux C++
【Shell 编程设计】 编写自己的清理后台的Shell脚本
【Shell 编程设计】 编写自己的清理后台的Shell脚本
29 1
|
24天前
|
存储 Shell 数据安全/隐私保护
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
22 0
|
24天前
|
Shell C语言 C++
【Shell 编程指南】shell中的(),{}几种语法用法
【Shell 编程指南】shell中的(),{}几种语法用法
17 0
|
24天前
|
Shell 程序员 Linux
【Shell 编程指南】shell运算操作符之(())
【Shell 编程指南】shell运算操作符之(())
19 0
|
3月前
|
Shell
Shell 编程快速入门 之 函数基础知识
Shell 编程快速入门 之 函数基础知识
67 0
Shell 编程快速入门 之 函数基础知识