标签存档: bash

Dash与Bash的语法区别

如今Debian和Ubuntu中,/bin/sh默认已经指向dash,这是一个不同于bash的shell,它主要是为了执行脚本而出现,而不是交互,它速度更快,但功能相比bash要少很多,语法严格遵守POSIX标准,下面简要列举下从bash迁移到dash一般需要注意的问题

1.定义函数

bash: function在bash中为关键字

igi@gentoo ~ $ foo(){ echo $0;}
igi@gentoo ~ $ foo
/bin/bash
igi@gentoo ~ $ function foo2(){ echo $0;}
igi@gentoo ~ $ foo2
/bin/bash

dash: dash中没有function这个关键字

$ foo(){ echo $0;}
$ foo
dash
$ function foo2(){ echo $0;}
dash: Syntax error: "(" unexpected

2.select var in list; do command; done

bash:支持

igi@gentoo ~ $ select input in A B
> do
>   case $input in
>     A)
>        echo 'Input:A'
>        break
>        ;;
>     B)
>        echo 'Input:B'
>        break
>        ;;
>   esac
> done
1) A
2) B
#? 1
Input:A
igi@gentoo ~ $ echo $0
/bin/bash

dash:不支持, 替代方法:采用while+read+case来实现

menu(){ echo -n "1)A;\n2)B\n>";}
menu
while read input
do
    case $input in
      1)
         echo 'A'
         break
         ;;
      2)
         echo 'B'
         break
         ;;
      *)
         menu
         continue
         ;;
    esac
done

3. echo {0..10}

bash:支持{n..m}展开

igi@gentoo ~ $ echo $0
/bin/bash
igi@gentoo ~ $ echo {0..10}
0 1 2 3 4 5 6 7 8 9 10

dash:不支持,替代方法, 采用seq外部命令

$ echo $0
dash
$ echo {0..10}
{0..10}
$ echo `seq 0 10`
0 1 2 3 4 5 6 7 8 9 10

4. here string

bash:支持here string

igi@gentoo ~ $ cat <<<"string"
string
igi@gentoo ~ $ echo $0
/bin/bash

dash:不支持, 替代方法:可采用here documents

$ echo $0
dash
$ cat <<<"string"
dash: Syntax error: redirection unexpected
$ cat <<EOF
> string
> EOF
string

5. >&word重定向标准输出和标准错误

bash: 当word为非数字时,>&word变成重定向标准错误和标准输出到文件word, 常见用法>&/dev/null

igi@gentoo ~/test $ ls
a
igi@gentoo ~/test $ ls a b
ls: cannot access b: No such file or directory
a
igi@gentoo ~/test $ ls a b >&/dev/null
igi@gentoo ~/test $ ls a b >/dev/null 2>&1
igi@gentoo ~/test $ echo $0
/bin/bash

dash: >&word, word不支持非数字, 替代方法: >word 2>&1; 常见用法 >/dev/null 2>&1

$ echo $0
dash
$ ls a
a
$ ls a b
ls: cannot access b: No such file or directory
a
$ ls a b >&/dev/null
dash: Syntax error: Bad fd number
$ ls a b >/dev/null 2>&1
$

6. 数组

bash: 支持数组, bash4支持关联数组

igi@gentoo ~/test $ echo $0
/bin/bash
igi@gentoo ~/test $ array=( a b c )
igi@gentoo ~/test $ echo ${array[2]}
c

dash: 不支持数组,替代方法, 采用变量名+序号来实现类似的效果

$ for i in a b c
> do
> id=$((${id:=-1}+1))
> eval array_$id=$i
> done
$ echo ${array_1}
b
$ echo $0
dash

很蛋疼的方法,非不得以不建议这么用

7. 子字符串扩展

bash: 支持${parameter:offset:length},${parameter:offset}

igi@gentoo ~/test $ string='hello'
igi@gentoo ~/test $ echo ${string:1:3}
ell
igi@gentoo ~/test $ echo ${string:1}
ello
igi@gentoo ~/test $ echo $0
/bin/bash

dash: 不支持, 替代方法:采用expr或cut外部命令代替

$ string='hello'
$ expr substr "$string" 2 3
ell
$ echo "$string" | cut -c2-4
ell
$ expr substr "$string" 2 "${#string}"
ello
$ echo "$string" | cut -c2-
ello
$ echo $0
dash
$

8. 大小写转换

bash: 支持${parameter^pattern},${parameter^^pattern},${parameter,pattern},${parameter,,pattern}

igi@gentoo ~/test $ string="abcABC"
igi@gentoo ~/test $ echo ${string^^}
ABCABC
igi@gentoo ~/test $ echo ${string,,}
abcabc
igi@gentoo ~/test $ echo ${string^^b}
aBcABC
igi@gentoo ~/test $ echo $0
/bin/bash

dash: 不支持,替代方法:采用tr/sed/awk等外部命令转换

$ string='abcABC'
$ echo "$string" | tr '[a-z]' '[A-Z]'
ABCABC
$ echo "$string" | tr '[A-Z]' '[a-z]'
abcabc
$ echo "$string" | sed 's/b/\U&/g'
aBcABC
$

9. 进程替换<(command), >(command)

bash: 支持进程替换

igi@gentoo ~ $ diff <(seq 3) <(seq 4)
3a4
> 4
igi@gentoo ~ $ echo $0
/bin/bash

dash: 不支持, 替代方法, 通过临时文件中转

$ diff <(seq 3) <(seq 4)
dash: Syntax error: "(" unexpected
$ seq 3 >tmp1
$ seq 4 >tmp2
$ diff tmp1 tmp2
3a4
> 4
$ echo $0
dash
$

10. [ string1 = string2 ] 和 [ string1 == string2 ]

bash: 支持两者

igi@gentoo ~ $ [ 'a' = 'a' ] && echo 'equal'
equal
igi@gentoo ~ $ [ 'a' == 'a' ] && echo 'equal'
equal
igi@gentoo ~ $ echo $0
/bin/bash

dash: 只支持=

$ [ 'a' = 'a' ] && echo 'equal'
equal
$ [ 'a' == 'a' ] && echo 'equal'
[: 2: a: unexpected operator
$ echo $0
dash
$

11. [[ 加强版test

bash: 支持[[ ]], 可实现正则匹配等强大功能

igi@gentoo ~ $ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal'
equal
igi@gentoo ~ $ echo $0
/bin/bash

dash: 不支持[[ ]], 替代方法,采用外部命令

$ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal'
dash: [[: not found
$ echo 'xyz123' | grep -q 'xyz[0-9]\+' && echo 'equal'
equal
$ echo $0
dash
$

12. for (( expr1 ; expr2 ; expr3 )) ; do list ; done

bash: 支持C语言格式的for循环

igi@gentoo ~ $ for((i=0;i<=3;i++));do echo "$i";done
0
1
2
3
igi@gentoo ~ $ echo $0
/bin/bash

dash: 不支持该格式的for, 替代方法,用while+$((expression))实现

$ i=0
$ while [ "$i" -le 3 ]
> do
> echo "$i"
> i=$((i+1))
> done
0
1
2
3
$ echo $0
dash
$

13. let命令和((expression))

bash: 有内置命令let, 也支持((expression))方式

igi@gentoo ~ $ i=0
igi@gentoo ~ $ let i++
igi@gentoo ~ $ echo $i
1
igi@gentoo ~ $ ((i++))
igi@gentoo ~ $ echo $i
2
igi@gentoo ~ $ echo $0
/bin/bash

dash: 不支持,替代方法,采用$((expression))或者外部命令做计算

$ i=0
$ i=$((i+1))
$ echo $i
1
$ echo $0
dash
$

14. $((expression))

bash: 支持id++,id--,++id,--id这样到表达式

igi@gentoo ~ $ i=0
igi@gentoo ~ $ echo $((i++))
0
igi@gentoo ~ $ echo $i
1
igi@gentoo ~ $ echo $((++i))
2
igi@gentoo ~ $ echo $i
2
igi@gentoo ~ $ echo $0
/bin/bash

dash: 不支持++,--, 替代方法:id+=1,id-=1, id=id+1,id=id-1

$ i=0
$ echo $((i++))
dash: arithmetic expression: expecting primary: "i++"
$ echo $i;i=$((i+1))
0
$ echo $i
1
$ echo $((i+=1))
2
$ echo $i
2
$ echo $0
dash
$

以上列举的都是常见容易混淆的地方,更多区别可以查看manpage

Bash星号的那点事

星号,这是个神奇的符号,在bash中,星号也充满魔力,它是通配符之一。
简单列举下星号的功能,被称为万能字符不是没有理由的.
1)文件名匹配: 默认情况下匹配所有非隐藏文件(即非.开头到文件)
2)字符串匹配: 匹配任意字符
3)$*匹配所有参数
4)${array[*]},表示了所有数组元素
5)乘法运算
6)** 冪运算
7)** bash4中提供的更牛b的文件名匹配,包含递归功能
接下我们来见识下它强大到魔力:
1)文件名匹配:

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开关

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

如果只是匹配隐藏文件呢?可别想得太复杂了哦

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语句、字符串截取中

igi@gentoo ~ $ case "abc" in
> a)
>   echo 'a'
>   ;;
> a*)
>   echo 'a*'
>   ;;
> esac
a*
igi@gentoo ~ $ var='abc123'
igi@gentoo ~ $ echo "${var%c*}"
ab

可以看到*号匹配了任意字符,在这里,先提醒一下,这里用的是模式匹配,而不是正则(正则与模式匹配不同,以后到文章中,将会仔细对比这两者到区别)

3)$*表示所有参数

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″ …(注意这里到双引号,双引号中的字符串是一个整体),似乎有点不明白,看看下面的例子

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[*]}表示所有数组元素

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)*号乘法运算, **冪运算

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的通配符**

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法?

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? 非也,来看看

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)没有考虑星号匹配不到任何文件的情况
如果指定到目录下没有任何文件时,使用星号匹配,会有啥现象?

igi@gentoo ~ $ rm -rf *
igi@gentoo ~ $ ls
igi@gentoo ~ $ echo *
*

看到了吧,如果星号匹配不到任何文件时,它变回了自己原本到意思(就是字符*)

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, 利用它,我们可以直接达到我们要到效果。

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)混淆模式匹配和正则
先申明,这两者是不同到,这里不打算详细阐述,只列举些常见错误做法

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

第 1 页,共 3 页123