Bash星号的那点事

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 ]]例外)
在模式匹配中*匹配了所有,?匹配了单个字符,.号没有特殊意义,还是.号
所以,第二个命令才是正确

Bash空格的那点事

空格,一个看不见的字符,很不起眼,也正由于不起眼,很多人经常忽略它,导致代码出错,却还找不着北。这里,我们来聊聊bash中空格的那点事。
先了解下bash中什么时候该用空格,什么时候不该用。

    1. 等号赋值两边不能有空格
    2. 命令与选项之间需要空格
    3. 管道两边空格可有可无

我们来看看常见的问题
1. 赋值时等号两边或者只有左边多了空格

1
2
3
4
5
6
7
8
9
10
11
12
igi@gentoo ~ $ var1 = test
bash : var1: command not found
igi@gentoo ~ $ echo ${var1:?error}
bash : var1: error
igi@gentoo ~ $ echo ${var1?error}
bash : var1: error
igi@gentoo ~ $ var2 = test
bash : var2: command not found
igi@gentoo ~ $ echo ${var2:?error}
bash : var2: error
igi@gentoo ~ $ echo ${var2?error}
bash : var2: error

这里我用了bash的变量扩展,${var1:?error}当var1为unset或null(未定义或空)时, 报指定错误; ${var1?error}当var1为unset时,报指定错误 。从执行结果来看,如果等号左边有空格,则变量名当成命令执行,结果报command not found,变量没有被赋值

2. 赋值时等号左边没有空格,右边有空格(这种情况有点特别,你会发现两种情况)

1
2
3
igi@gentoo ~ $ var= test
igi@gentoo ~ $ var= nocmd
bash : nocmd: command not found

同样是等号右边有空格,第一条命令没报错,而第二条报错了。
这是因为shell中有这么一种执行命令的方式: var=string command
命令command将得到变量var的值(至于在命令执行后,变量var的值是否保留下来,bash4中没有保留,但我在dash中发现时保留下来的,不同的shell对这个的处理不同), 由于test是个命令,而nocmd不是,所以报了command not found.

1
2
3
igi@gentoo ~ $ var=newtest eval echo \$var
newtest
igi@gentoo ~ $ echo $var

注意: 这里我使用了eval, 是想避免在第一次解析时$var被替换成空字符串, 不然就会出现下面的情况(下面是错误的测试方法,在echo还没执行时,$var已经被替换成空字符串)

1
2
3
igi@gentoo ~ $ var=newtest echo $var
 
igi@gentoo ~ $ echo $var

到这里,相信大家都明白了吧, 对于等号赋值,左右两边不可以有空格,虽然右边有空格不一定报错,但那绝对不是你想要的结果。

3. 命令和选项之间必须有空格
这个似乎大家都明白,为何我还这么罗嗦呢?说到这里,不得不提一下一个非常特别的命令: [ 命令(你没看错,是[ ), 也就是test命令(当然bash中,这是个内置命令,但在这里不影响
我们的理解)。或许你会觉得[命令眼熟,没错,我保证你见过它,来看看下面的例子

1
2
3
4
5
igi@gentoo ~ $ if [ "abc" = "abc" ]; then echo ‘they are the same’; fi
they are the same
igi@gentoo ~ $ type -a [
[ is a shell builtin
[ is /usr/bin/ [

想起来了吧?[命令经常用到if判断中,当然也有人喜欢这么写

1
2
3
4
5
igi@gentoo ~ $ [ "abc" = "cba" ] || echo ‘they are not the same’
they are not the same
igi@gentoo ~ $ type -a [
[ is a shell builtin
[ is /usr/bin/ [

[ 命令正名叫test命令,它们两者几乎一样,为什么不是完全一样?来看看这个

1
2
3
4
5
6
igi@gentoo ~ $ [ "abc" = "cba"
bash : [: missing `]‘
igi@gentoo ~ $ [ "abc" = "cba" ]
igi@gentoo ~ $ test "abc" = "cba" ]
bash : test : too many arguments
igi@gentoo ~ $ test "abc" = "cba"

清晰了吧,用[命令时,你必须给它个尾巴], 用test命令时,就不能加个尾巴。尾巴]是[最后一个参数,不可缺少的参数, 代表[命令的结束

扯了这么多,那到底这个和空格有毛关系?说这些,是先让大家明白: [在shell中是个命令,它左右必须有空格!]是[的最后不可缺少的参数,它两边也需要空格(虽然有些命令的参数能连一起,例如ps, 但[命令不行,它的参数之间必须有空格)。让我们看看关于[常见的错误

a. if 与 [ 之间缺少空格

1
2
3
4
5
6
7
8
igi@gentoo ~ $ if [ "$HOME" = "/home/igi" ]; then echo 'equal' ; fi
bash : syntax error near unexpected token ` then '
igi@gentoo ~ $ if [ "$HOME" = "/home/igi" ]; then echo 'equal' ; fi
bash : syntax error near unexpected token ` then '
igi@gentoo ~ $ if [ "$HOME" = "/home/igi" ]; then echo 'equal' ; fi
bash : syntax error near unexpected token ` then '
igi@gentoo ~ $ if [ "$HOME" = "/home/igi" ]; then echo 'equal' ; fi
bash : syntax error near unexpected token ` then '

语法分析错误,很明显,if[ 对于bash来说,不知道是什么鬼东西

b. [与后面的参数之间缺少空格

1
2
3
4
igi@gentoo ~ $ if [ "$HOME" = "/home/igi" ]; then echo 'equal' ; fi
bash : [ /home/igi : No such file or directory
igi@gentoo ~ $ if [ "$HOME" = "/home/igi" ]; then echo 'equal' ; fi
bash : [ /home/igi : No such file or directory

["$HOME" 对于bash来说,也不知道是什么鬼东西

c. [ ] 之间的参数之间缺少空格

1
2
3
4
igi@gentoo ~ $ if [ "abc" = "abc" ]; then echo 'equal' ; fi
equal
igi@gentoo ~ $ if [ "abc" = "cba" ]; then echo 'equal' ; fi
equal

第一条命令似乎是对的(实际上是正巧而已),看看第二条命令"abc" 和 "cba"明显不同,但却判断为相同。这是因为参数之间缺少了空格,被[命令认为内部是个值而已。看看下面的命令,你就会释然

1
2
3
4
5
6
igi@gentoo ~ $ if [ 0 ]; then echo 'equal' ; fi
equal
igi@gentoo ~ $ if [ "1" ]; then echo 'equal' ; fi
equal
igi@gentoo ~ $ if [ "" ]; then echo 'equal' ; fi
igi@gentoo ~ $ if [ ]; then echo 'equal' ; fi

在[ ] 内部,如果只有一个值(那些因为缺少了空格而连一起的也算),不是空字符串就为真。所以在[ ] 之间的参数,也要两边有空格,而不能堆一起

d. 参数和尾巴]之间缺少空格
这个就不罗嗦了,尾巴]也是[命令的参数,如同上面所讲,参数之间必须有空格

扯了这么多[命令与空格的事,但有些时候,缺了空格却能正确运行, 当然这只是你好运, 一起来看看

1
2
3
4
5
igi@gentoo ~ $ var= ' abc'
igi@gentoo ~ $ if [$var = "abc" ]; then echo 'equal' ; fi
equal
igi@gentoo ~ $ if [ "$var" = "abc" ]; then echo 'equal' ; fi
bash : [ abc: command not found

之前Bash引号那点事提到过,双引号包围起来的是一个整体,而没双引号的时候,字符串前后的空格或制表符都被切开。如果恰巧你遇到了或者你故意要丢弃字符串前后的空格或制表符,那也不是不可能, 但非常不建议你这么写,你的代码将是非常脆弱的。

或者你该加的空格都加了,但还是报错,这也可能和缺少双引号有关。这样的情况很普遍,最后再看看

1
2
3
4
5
6
7
8
9
igi@gentoo ~ $ var= ''
igi@gentoo ~ $ if [ "$var" = "abc" ]; then echo 'equal' ; fi
igi@gentoo ~ $ if [ $var = "abc" ]; then echo 'equal' ; fi
bash : [: =: unary operator expected
igi@gentoo ~ $ dvar= 'a b c'
igi@gentoo ~ $ if [ $dvar = "a b c" ]; then echo 'equal' ; fi
bash : [: too many arguments
igi@gentoo ~ $ if [ "$dvar" = "a b c" ]; then echo 'equal' ; fi
equal

我再罗嗦一次,不要轻易省略双引号。很清楚了吧?如果你还不明白,
请读读Bash引号那点事

最后,对于管道两边可有可无的空格,就不扯淡了,因为没遇到有人对此有疑惑.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值