#!/bin/sh
val=`expr 2 + 2`
echo "Total value : $val"
运算符和表达式之间必须有空格,例如2+2是不正确的,因为它应该写成2 + 2。
倒逗号之间应包含完整的表达
算数运算符
[ $a == $b ]返回False
[ $a != $b ] 返回True1、所有的条件式将放在方括号内,他们身边有一个空格,例如 [ $a == $b ]是正确的,为[$a==$b] 是不正确的。
2、所有的算术计算,使用长整数。关系运算符
等于 -eq [ $a -eq $b ] is True 等于返回True
不等于-nq [ $a -nq $b ] is not True 等于返回True
左边值>右边 -gt
左边<右边 -lt
左边>=右边 -ge
左边<=右边 -le
布尔运算
[ ! false ] is true —- 非运算
[ $a -lt 20 -o $b -gt 100 ] is true —– 或运算
[ $a -lt 20 -a $b -gt 100 ] is false —-AND运算
字符串运算符
[ -z $a ] is not true —–字符串长度为0否 -z
[ -n $a ] is not false—–字符串长度不为0 -n
[ $a ] is not false —-空字符串判断 str
文件测试操作
-b file —– a block special file —- [ -b $file ] is false.
-c file —– a character special file —- [ -b $file ] is false.
-d file —- a directory — [ -d $file ] is not true.
-f file—- an ordinary file —- [ -f $file ] is true.
-g file —- set group ID (SGID) — [ -g $file ] is false.
-k file — sticky bit set —- [ -k $file ] is false.
-p file —- a named pipe i —- [ -p $file ] is false.
-t file —- file descriptor is open and associated with a terminal —- [ -t $file ] is false.
-u file —- set user id (SUID) —- [ -u $file ] is false.
-r file —- 是否可读 —- [ -r $file ] is true.
-w file —- 是否可写 —- [ -w $file ] is true.
-x file —- is execute —- [ -x $file ] is true.
-s file —- 大小是否大于0 —- [ -s $file ] is true.
-e file —- 是否存在 —- [ -e $file ] is true.