from: http://www.igigo.net/archives/category/bash%E9%82%A3%E7%82%B9%E4%BA%8B
星号,这是个神奇的符号,在bash中,星号也充满魔力,它是通配符之一。
简单列举下星号的功能,被称为万能字符不是没有理由的.
1)文件名匹配: 默认情况下匹配所有非隐藏文件(即非.开头到文件)
2)字符串匹配: 匹配任意字符
3)$*匹配所有参数
4)${array[*]},表示了所有数组元素
5)乘法运算
6)** 冪运算
7)** bash4中提供的更牛b的文件名匹配,包含递归功能
接下我们来见识下它强大到魔力:
1)文件名匹配:
1
2
3
4
5
6
|
igi@gentoo ~ $
ls
a b c d
igi@gentoo ~ $
ls
-A
a b c d .t .u .
v
.w .x .y
igi@gentoo ~ $
echo
*
a b c d
|
我们可以看到,*号匹配了所有非隐藏文件名,如果要匹配所有文件名(包括隐藏文件),可以打开dotglob开关
1
2
3
4
5
6
7
|
igi@gentoo ~ $
ls
-A
a b c d .t .u .
v
.w .x .y
igi@gentoo ~ $
echo
*
a b c d
igi@gentoo ~ $
shopt
-s dotglob
igi@gentoo ~ $
echo
*
a b c d .t .u .
v
.w .x .y
|
如果只是匹配隐藏文件呢?可别想得太复杂了哦
1
2
3
4
|
igi@gentoo ~ $
ls
-a
. .. a b c d .t .u .
v
.w .x .y
igi@gentoo ~ $
echo
.*
. .. .t .u .
v
.w .x .y
|
需要注意到是,.*会把当前目录下到.目录和..目录也匹配进去
2)字符串匹配:这一般用在case语句、字符串截取中
1
2
3
4
5
6
7
8
9
10
11
12
|
igi@gentoo ~ $
case
"abc"
in
> a)
>
echo
'a'
> ;;
> a*)
>
echo
'a*'
> ;;
> esac
a*
igi@gentoo ~ $ var=
'abc123'
igi@gentoo ~ $
echo
"${var%c*}"
ab
|
可以看到*号匹配了任意字符,在这里,先提醒一下,这里用的是模式匹配,而不是正则(正则与模式匹配不同,以后到文章中,将会仔细对比这两者到区别)
3)$*表示所有参数
1
2
3
4
5
6
7
8
9
|
igi@gentoo ~ $ foo() {
for
i
in
$*;
do
echo
"var: $i"
;
done
; }
igi@gentoo ~ $ foo a b
"c cc"
var: a
var: b
var: c
var: cc
igi@gentoo ~ $ foo() {
for
i
in
"$*"
;
do
echo
"var: $i"
;
done
; }
igi@gentoo ~ $ IFS=
"|"
foo a b
"c cc"
var: a|b|c cc
|
最后到例子中,我设置了新到IFS变量,只是为了让大家更清晰到看到,bash是如何对待”$*”的,bash用IFS变量,把所有参数拼成一个字符串,这就是”$*”
这里先说下$@和”$@”,在没有用双引号包围时,$@和$*一样表示了 $1 $2 $3 …
而”$@” 则与”$*”不同,”$@”表示了 “$1″ “$2″ “$3″ …(注意这里到双引号,双引号中的字符串是一个整体),似乎有点不明白,看看下面的例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
igi@gentoo ~ $ foo() {
for
i
in
$*;
do
echo
"var: $i"
;
done
;}
igi@gentoo ~ $ foo a b
"c cc"
var: a
var: b
var: c
var: cc
igi@gentoo ~ $ foo() {
for
i
in
$@;
do
echo
"var: $i"
;
done
;}
igi@gentoo ~ $ foo a b
"c cc"
var: a
var: b
var: c
var: cc
igi@gentoo ~ $ foo() {
for
i
in
"$*"
;
do
echo
"var: $i"
;
done
;}
igi@gentoo ~ $ foo a b
"c cc"
var: a b c cc
igi@gentoo ~ $ foo() {
for
i
in
"$@"
;
do
echo
"var: $i"
;
done
;}
igi@gentoo ~ $ foo a b
"c cc"
var: a
var: b
var: c cc
|
4)${array[*]}表示所有数组元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
igi@gentoo ~ $ array=( a b
"c cc"
)
igi@gentoo ~ $
for
i
in
${array[*]};
do
echo
"array: $i"
;
done
array: a
array: b
array: c
array: cc
igi@gentoo ~ $
for
i
in
${array[@]};
do
echo
"array: $i"
;
done
array: a
array: b
array: c
array: cc
igi@gentoo ~ $
for
i
in
"${array[*]}"
;
do
echo
"array: $i"
;
done
array: a b c cc
igi@gentoo ~ $
for
i
in
"${array[@]}"
;
do
echo
"array: $i"
;
done
array: a
array: b
array: c cc
|
细心的你应该不难看出,这和$*是一样的,我就不罗嗦了。
5、6)*号乘法运算, **冪运算
1
2
3
4
5
6
7
8
9
10
11
12
|
igi@gentoo ~ $ ((num=3*4))
igi@gentoo ~ $
echo
$num
12
igi@gentoo ~ $
let
num=3*3
igi@gentoo ~ $
echo
$num
9
igi@gentoo ~ $ ((num=2**4))
igi@gentoo ~ $
echo
$num
16
igi@gentoo ~ $
let
num=2**2
igi@gentoo ~ $
echo
$num
4
|
乘法运算和冪运算应该很容易理解吧。
7)bash4中更牛B的通配符**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
igi@gentoo ~
/test
$ tree
.
├── a
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ └── 5
└── c
├── 2.txt
├── 3.txt
├── 4.txt
└──
dir
3 directories, 8 files
igi@gentoo ~
/test
$
shopt
globstar
globstar off
igi@gentoo ~
/test
$
echo
**
a c
igi@gentoo ~
/test
$
echo
**/
a/ c/
igi@gentoo ~
/test
$
echo
*
a c
igi@gentoo ~
/test
$
echo
**
a c
igi@gentoo ~
/test
$
echo
*/
a/ c/
igi@gentoo ~
/test
$
echo
**/
a/ c/
|
默认情况下,globstar是关闭的,也就是**与*是一样的,我们来看看打开globstar后是怎么个牛b法?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
igi@gentoo ~
/test
$ tree
.
├── a
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ └── 5
└── c
├── 2.txt
├── 3.txt
├── 4.txt
└──
dir
3 directories, 8 files
igi@gentoo ~
/test
$
shopt
-s globstar
igi@gentoo ~
/test
$
shopt
globstar
globstar on
igi@gentoo ~
/test
$
echo
*
a c
igi@gentoo ~
/test
$
echo
**
a a
/1
a
/2
a
/3
a
/4
a
/5
c c
/2
.txt c
/3
.txt c
/4
.txt c
/dir
igi@gentoo ~
/test
$
echo
*/
a/ c/
igi@gentoo ~
/test
$
echo
**/
a/ c/ c
/dir/
|
可以看到打开globstar后,**递归的匹配了所有文件和目录, 如果**后面跟着/(即是**/),则只匹配目录。
问题来了,如果递归显示以.txt结尾到文件,是不是**.txt? 非也,来看看
1
2
3
4
5
6
7
8
9
10
11
12
|
igi@gentoo ~
/test
$
shopt
-s globstar
igi@gentoo ~
/test
$
shopt
globstar
globstar on
igi@gentoo ~
/test
$
find
. -name
'*.txt'
.
/a
.txt
.
/c/3
.txt
.
/c/2
.txt
.
/c/4
.txt
igi@gentoo ~
/test
$
echo
**.txt
a.txt
igi@gentoo ~
/test
$
echo
**/*.txt
a.txt c
/2
.txt c
/3
.txt c
/4
.txt
|
看到了吧,**.txt是无法递归的,而**/*.txt就可以了,同理, foo**这样也不行,**/foo*这样到才可以。
这个功能是bash4才有的哦,使用之前,先确认下你到bash版本。
接下来,我们来看看常见到错误
1)用单引号或双引号包围了星号
在’Bash引号的那点事‘中我讲过,单引号中到字符都只有字符的原本意义,而双引号中,*号也是它本身到字符意义,在单双引号中,*号将失去它到魔力,这里就不再罗嗦
如果在双引号中,$*将表示一个由所有参数拼接而成到字符串,上面已经提到过。
2)没有考虑星号匹配不到任何文件的情况
如果指定到目录下没有任何文件时,使用星号匹配,会有啥现象?
1
2
3
4
|
igi@gentoo ~ $
rm
-rf *
igi@gentoo ~ $
ls
igi@gentoo ~ $
echo
*
*
|
看到了吧,如果星号匹配不到任何文件时,它变回了自己原本到意思(就是字符*)
1
2
3
4
5
6
7
8
9
10
11
|
igi@gentoo ~ $
ls
a b c d
igi@gentoo ~ $
for
i
in
*;
do
echo
"file: $i"
;
done
file
: a
file
: b
file
: c
file
: d
igi@gentoo ~ $
rm
-rf *
igi@gentoo ~ $
ls
igi@gentoo ~ $
for
i
in
*;
do
echo
"file: $i"
;
done
file
: *
|
只是一个echo时,似乎没多大问题,但你想想,如果你在for中对文件做某些操作,如果匹配不到,变成对*号文件进行操作,我想结果肯定不是你要的,多数情况下,我们想要是,匹配不到则不进行任何操作,有没有办法?办法很多,例如你可以先做个判断,bash中,有个nullglob, 利用它,我们可以直接达到我们要到效果。
1
2
3
4
5
6
7
8
|
igi@gentoo ~ $
rm
-rf *
igi@gentoo ~ $
shopt
nullglob
nullglob off
igi@gentoo ~ $
for
i
in
*;
do
echo
"file: $i"
;
done
file
: *
igi@gentoo ~ $
shopt
-s nullglob
igi@gentoo ~ $
for
i
in
*;
do
echo
"file: $i"
;
done
igi@gentoo ~ $
|
当然你也可以设置failglob,使得匹配不到文件时报错,这里就不再罗嗦了。
3)混淆模式匹配和正则
先申明,这两者是不同到,这里不打算详细阐述,只列举些常见错误做法
1
2
3
4
5
6
|
igi@gentoo ~
/test
$
ls
foo-a-log foo-b-log foo-c-log zoo-a-log zoo-b-log zoo-c-log
igi@gentoo ~
/test
$
ls
foo.*
ls
: cannot access foo.*: No such
file
or directory
igi@gentoo ~
/test
$
ls
foo*
foo-a-log foo-b-log foo-c-log
|
.*在正则中匹配了所有,但请记住,shell中绝大多数用的是模式匹配([[ "$string" =~ RE ]]例外)
在模式匹配中*匹配了所有,?匹配了单个字符,.号没有特殊意义,还是.号
所以,第二个命令才是正确