Linuxif
- –b当file存在并且是块文件时返回真
- -c当file存在并且是字符文件时返回真
- -d当pathname存在并且是一个目录时返回真
- -e当pathname指定的文件或目录存在时返回真
- -f当file存在并且是正规文件时返回真
- -g当由pathname指定的文件或目录存在并且设置了SGID位时返回为真
- -h当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效
- -k当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真
- -p当file存在并且是命令管道时返回为真
- -r当由pathname指定的文件或目录存在并且可读时返回为真
- -s当file存在文件大小大于0时返回真
- -u当由pathname指定的文件或目录存在并且设置了SUID位时返回真
- -w当由pathname指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。
- -o当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。
UNIX Shell里面比较字符写法:
- -eq
等于 - -ne
不等于 - -gt
大于 - -lt
小于 - -le
小于等于 - -ge
大于等于 - -z
空串 - =
两个字符相等 - !=
两个字符不等 - -n
非空串
1.字符串测试
string1 =string2 string1等于string2(=两侧必须有空格)
string1 !=string2 string1不等于string2(!=两侧必须有空格)
string(string不为空)
-z string(string的长度为0)
-n string(string的长度不为0)
例子:
test -n$word
test tom =sun
3.逻辑测试:
expr1 -a expr2 逻辑与
expr1 -o expr2 逻辑或
!expr 逻辑非
expr1 -o expr2 逻辑或
!expr
字符串比较
~$ if [ "r" = "a" ]; then echo "aa"; else echo "bb"; fi
bb
多行输入 “;”换成 “回车”
~$ if [ "r" = "a" ]
> then
> echo "aa"
> else
> echo "bb"
> fi
bb
整数比较
~$ if [ "r" = "a" ]; then echo "aa"; else echo "bb"; fi
bb
多行输入 “;”换成 “回车”
~$ if [ "r" = "a" ]
> then
> echo "aa"
> else
> echo "bb"
> fi
bb
整数比较
~$ if [ 1 -eq 2 ]
> then
> echo "aa"
> else
> echo "bb"
> fi
bb
> then
> echo "aa"
> else
> echo "bb"
> fi
bb