慎用删除

当变量未定义时,rm很可能会意外删掉你不想删掉的文件,-f 选项很危险

使用引号

使用变量时最好使用引号包起来,避免单词分割和路径匹配等行为带来的问题

赋值语句

shell中=两边不能有空格,和编程语言不一样

[root@zhujipeng test]# HAHA = haha
bash: HAHA: command not found

条件测试

[和空格

[ 和表达式之间要有空格

[root@zhujipeng test]# [ -n haha ] && echo yes || echo no
yes
[root@zhujipeng test]# [-n haha ] && echo yes || echo no
bash: [-n: command not found
no
[root@zhujipeng test]# [ -n haha] && echo yes || echo no
bash: [: missing `]'
no

[ 中的比较符号

[root@zhujipeng test]# [ 1 -gt 2 ] && echo yes || echo no
no
[root@zhujipeng test]# ll
total 0
[root@zhujipeng test]# [ 1 > 2 ] && echo yes || echo no
yes
[root@zhujipeng test]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 23 09:07 2

[[ 中的比较符号

[root@zhujipeng test]# [[ 1 -gt 2 ]] && echo yes || echo no
no
[root@zhujipeng test]# [[ 1 > 2 ]] && echo yes || echo no
no

[ 中的逻辑符号

[root@zhujipeng test]# [ -n haha -a -n hehe ] && echo yes || echo no
yes
[root@zhujipeng test]# [ -n haha && -n hehe ] && echo yes || echo no
bash: [: missing `]'
no

[[ 中的逻辑符号

[root@zhujipeng test]# [[ -n haha -a -n hehe ]] && echo yes || echo no
bash: syntax error in conditional expression
bash: syntax error near `-a'
[root@zhujipeng test]# [[ -n haha && -n hehe ]] && echo yes || echo no
yes

变量未定义

-n-z 的值需要用双引号扩起来,否则变量未定义时就会失败

[root@zhujipeng test]# unset HAHA
[root@zhujipeng test]# set -u : $HAHA
bash: HAHA: unbound variable
[root@zhujipeng test]# HAHA=haha
[root@zhujipeng test]# set -u : $HAHA
[root@zhujipeng test]# unset HAHA
[root@zhujipeng test]# set -u : $HAHA
bash: HAHA: unbound variable
[root@zhujipeng test]# set +u
[root@zhujipeng test]# [ -n $HAHA ] && echo yes || echo no
yes
[root@zhujipeng test]# [ -n "$HAHA" ] && echo yes || echo no
no
[root@zhujipeng test]# export HAHA=
[root@zhujipeng test]# [ -n "$HAHA" ] && echo yes || echo no
no
[root@zhujipeng test]# unset HAHA



参考

shell 语句出错自动退出
linux shell 中判断字符串为空的正确方法
Bash空格的那点事
Bash引号的那点事
shell中的括号(小括号,中括号,大括号)及单引号、 双引号,反引号

Copyright © zhujipeng 2017 all right reserved,powered by Gitbook 该文件修订时间: 2017-11-04 14:57:24

results matching ""

    No results matching ""