shell基本运算符

  #!/bin/sh
val=`expr 2 + 2`
echo "Total value : $val"
  • 运算符和表达式之间必须有空格,例如2+2是不正确的,因为它应该写成2 + 2。

  • 倒逗号之间应包含完整的表达

    算数运算符

    [ $a == $b ]返回False
    [ $a != $b ] 返回True

    1、所有的条件式将放在方括号内,他们身边有一个空格,例如 [ $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.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值