zoukankan      html  css  js  c++  java
  • shell中的运算符

    运算符

    Bash 支持很多运算符,包括算数运算符、关系运算符、布尔运算符、字符串运算符和文件测试运算符。

    原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。

    expr 是一款表达式计算工具,使用它能完成表达式的求值操作。

    例如,两个数相加:

    #!/bin/bash
    val=`expr 2 + 2`
    echo "Total value : $val"
    

    运行脚本输出:

    Total value : 4
    

    两点注意:

    • 表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。
    • 完整的表达式要被 包含,注意这个字符不是常用的单引号,在 Esc 键下边。

    算术运算符

    先来看一个使用算术运算符的例子:

    #7、+、-、*、/、%:取余数
    $[]
    $(())
    expr
    let 
    
    bc  #计算小数
    
    # 示例
    [root@Centos7 test]# n=10
    [root@Centos7 test]# echo $[$n+1]
    [root@Centos7 test]# echo $[$n + 1]
    11
    [root@Centos7 test]# echo $[$n-1]
    9
    [root@Centos7 test]# echo $[$n/3]		#忽略小数
    3
    [root@Centos7 test]# echo $[$n%3]		#取余数
    1
    [root@Centos7 test]# echo $[$n*3]
    30
    [root@Centos7 test]# echo $(($n+1))
    11
    [root@Centos7 test]# echo $(($n-1))
    9
    [root@Centos7 test]# echo $(($n/3))
    3
    [root@web01 mnt]# echo $(($n*3))
    30
    [root@Centos7 test]# echo $(($n%3))
    1
    [root@Centos7 test]# expr $n + 1		#必须加上空格
    11
    [root@Centos7 test]# expr $n / 3
    3
    [root@web01 mnt]# expr $n * 3			#必须加上转义符
    30
    [root@Centos7 test]# expr $n+1  #  一定记得在运算符左右两侧加空格 = echo
    10+1
    
    # let运算符
    [root@Centos7 test]# age=18
    [root@Centos7 test]# age=$[$age+1]
    [root@Centos7 test]# echo $age
    19
    
    [root@Centos7 test]# let age=age+1		#let支持不调用,直接计算
    [root@Centos7 test]# echo $age
    20
    [root@Centos7 test]# let age+=1  # 等于 age=age+1
    [root@Centos7 test]# echo $age
    21
    
    [root@Centos7 test]# unset m		#取消变量定义
    [root@Centos7 test]# unset n
    [root@Centos7 test]# unset x
    [root@Centos7 test]# unset y
    [root@Centos7 test]# let x=m++		#赋值
    [root@Centos7 test]# let y=++n		#先自增,后赋值
    [root@Centos7 test]# echo $x
    0
    [root@Centos7 test]# echo $y
    1
    
    #小数运算
    [root@web01 mnt]# echo "scale=2;33/100"|bc
    .33
    [root@Centos7 test]# echo $(echo "scale=2;33/100" | bc |cut -d. -f2)%
    33%
    
    运算符 说明 举例
    + 加法 expr $a + $b 结果为 30。
    - 减法 expr $a - $b 结果为 10。
    * 乘法 expr $a * $b 结果为 200。
    / 除法 expr $b / $a 结果为 2。
    % 取余 expr $b % $a 结果为 0。
    = 赋值 a=$b 将把变量 b 的值赋给 a。
    == 相等。用于比较两个数字,相同则返回 true。 [ $a == $b ] 返回 false。
    != 不相等。用于比较两个数字,不相同则返回 true。 [ $a != $b ] 返回 true。

    注意:条件表达式要放在方括号之间,并且要有空格,例如 [$a==$b] 是错误的,必须写成 [ $a == $b ]。

    浮点型

    #判断数值是否为浮点型
    [ 'echo 1.1 > 0.9 |bc' -eq 1 ]
    

    关系运算符

    关系运算符只支持数字,不支持字符串,除非字符串的值是数字。

    先来看一个关系运算符的例子:

    #!/bin/sh
    a=10
    b=20
    if [ $a -eq $b ]
    then   
    	echo "$a -eq $b : a is equal to b"
    else
    	echo "$a -eq $b: a is not equal to b"
    fi
    if [ $a -ne $b ]
    then   
    	echo "$a -ne $b: a is not equal to b"
    else   
    	echo "$a -ne $b : a is equal to b"
    fi
    if [ $a -gt $b ]
    then   
    	echo "$a -gt $b: a is greater than b"
    else   
    	echo "$a -gt $b: a is not greater than b"
    fi
    if [ $a -lt $b ]
    then   
    	echo "$a -lt $b: a is less than b"
    else   
    	echo "$a -lt $b: a is not less than b"
    fi
    if [ $a -ge $b ]
    then   
    	echo "$a -ge $b: a is greater or  equal to b"
    else   
    	echo "$a -ge $b: a is not greater or equal to b"
    fi
    if [ $a -le $b ]
    then   
    	echo "$a -le $b: a is less or  equal to b"
    else   
    	echo "$a -le $b: a is not less or equal to b"
    fi
    

    运行结果:

    10 -eq 20: a is not equal to b
    10 -ne 20: a is not equal to b
    10 -gt 20: a is not greater than b
    10 -lt 20: a is less than b
    10 -ge 20: a is not greater or equal to b
    10 -le 20: a is less or  equal to b
    
    运算符 说明 举例
    -eq 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] 返回 true。
    -ne 检测两个数是否相等,不相等返回 true。 [ $a -ne $b ] 返回 true。
    -gt 检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] 返回 false。
    -lt 检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] 返回 true。
    -ge 检测左边的数是否大等于右边的,如果是,则返回 true。 [ $a -ge $b ] 返回 false。
    -le 检测左边的数是否小于等于右边的,如果是,则返回 true。 [ $a -le $b ] 返回 true。

    布尔运算符

    先来看一个布尔运算符的例子:

    #!/bin/sh
    a=10
    b=20
    if [ $a != $b ]
    then   
    	echo "$a != $b : a is not equal to b"
    else   
    	echo "$a != $b: a is equal to b"
    fi
    if [ $a -lt 100 -a $b -gt 15 ]
    then   
    	echo "$a -lt 100 -a $b -gt 15 : returns true"
    else   
    	echo "$a -lt 100 -a $b -gt 15 : returns false"
    fi
    if [ $a -lt 100 -o $b -gt 100 ]
    then   
    	echo "$a -lt 100 -o $b -gt 100 : returns true"
    else   
    	echo "$a -lt 100 -o $b -gt 100 : returns false"
    fi
    if [ $a -lt 5 -o $b -gt 100 ]
    then   
    	echo "$a -lt 100 -o $b -gt 100 : returns true"
    else   
    	echo "$a -lt 100 -o $b -gt 100 : returns false"
    fi
    

    运行结果:

    10 != 20 : a is not equal to b
    10 -lt 100 -a 20 -gt 15 : returns true
    10 -lt 100 -o 20 -gt 100 : returns true
    10 -lt 5 -o 20 -gt 100 : returns false
    
    运算符 说明 举例
    ! 非运算,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。
    -o 或运算,有一个表达式为 true 则返回 true。|| [ $a -lt 20 -o $b -gt 100 ] 返回 true。
    -a 与运算,两个表达式都为 true 才返回 true。&& [ $a -lt 20 -a $b -gt 100 ] 返回 false。

    字符串运算符

    先来看一个例子:

    #!/bin/sh
    a="abc"
    b="efg"
    if [ $a = $b ]
    then   
    	echo "$a = $b : a is equal to b"
    else   
    	echo "$a = $b: a is not equal to b"
    fi
    if [ $a != $b ]
    then   
    	echo "$a != $b : a is not equal to b"
    else   
    	echo "$a != $b: a is equal to b"
    fi
    if [ -z $a ]
    then   
    	echo "-z $a : string length is zero"
    else   
    	echo "-z $a : string length is not zero"
    fi
    if [ -n $a ]then   
    	echo "-n $a : string length is not zero"
    else   
    	echo "-n $a : string length is zero"
    fi
    if [ $a ]then   #判断字符串是否为空
    	echo "$a : string is not empty"
    else   
    	echo "$a : string is empty"
    fi
    

    运行结果:

    abc = efg: a is not equal to b
    abc != efg : a is not equal to b
    -z abc : string length is not zero
    -n abc : string length is not zero
    abc : string is not empty
    
    运算符 说明 举例
    = 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
    != 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
    -z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。
    -n 检测字符串长度是否为0,不为0返回 true。 [ -z $a ] 返回 true。
    str 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。


    1.字符串测试,一定要加上" "后在比较,如[ -n "$myvar" ],不加的话有时候会报错

    2.= 和 != 两端一定要有空格

    文件测试运算符

    文件测试运算符用于检测 Unix 文件的各种属性。

    例如,变量 file 表示文件“/var/www/tutorialspoint/unix/test.sh”,它的大小为100字节,具有 rwx 权限。下面的代码,将检测该文件的各种属性:

    #!/bin/sh
    file="/var/www/tutorialspoint/unix/test.sh"
    if [ -r $file ]
    then   
    	echo "File has read access"else   
    	echo "File does not have read access"
    fi
    if [ -w $file ]
    then   
    	echo "File has write permission"
    else   
    	echo "File does not have write permission"
    fi
    if [ -x $file ]
    then   
    	echo "File has execute permission"
    else   
    	echo "File does not have execute permission"
    fi
    if [ -f $file ]
    then   
    	echo "File is an ordinary file"
    else   
    	echo "This is sepcial file"
    fi
    if [ -d $file ]
    then   
    	echo "File is a directory"
    else   
    	echo "This is not a directory"
    fi
    if [ -s $file ]
    then   
    	echo "File size is zero"
    else   
    	echo "File size is not zero"
    fi
    if [ -e $file ]
    then   
    	echo "File exists"
    else   
    	echo "File does not exist"
    fi
    

    运行结果:

    File has read access
    File has write permission
    File has execute permission
    File is an ordinary file
    This is not a directory
    File size is zero
    File exists
    
    操作符 说明 举例
    -b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
    -c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
    -d file 检测文件是否是目录或软链接,如果是,则返回 true。 [ -d $file ] 返回 false。
    -f file 检测文件是否是普通文件或者硬链接文件,如果是,则返回 true。 [ -f $file ] 返回 true。
    -g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
    -k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
    -p file 检测文件是否是管道文件,如果是,则返回 true。 [ -p $file ] 返回 false。
    -u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
    -r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
    -w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
    -x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
    -s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
    -e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。
    -L file 监测文件是否是软链接文件 [ -L $file ] 返回true

    总结

    1. test 测试表达式
    2. [ 测试表达式 ]
    3. [[ 测试表达式 ]]
    4. ((测试表达式))
    注:
    一:1、2、3方式中测试表达式两侧必须有至少一个'空格',4不用
    二:1、2等价,3是test的扩展,'支持正则',4常用于计算
    三:&&、||、<、>、应用于[[]]中,而不能在[]中,在[]中常用 -a 、-o 、-lt(用于整数)、-gt(用于整数)
    
    []、test中使用的符号                   (())、[[]]中使用            
    -eq                   :相等                    ==/=
    -ne                   :不相等                  !=
    -gt                   :大于                    >
    -ge                   :大于等于                >=
    -lt                   :小于                    <
    -le                   :小于等于                <=
    
    注:
    1.比较符号两端需要有空格
    2.若在[]中使用>、<时,需要转义,(一般不建议在[]中使用>、<)
    3.尽量使用[] ,若在[]不行时,才使用[[]],尽量使用[]加 -eq的写法
    

    判断字符串是否为整数

    参考文档

        1.去掉0-9后,看字符串长度是否为0,或字符串是否为空
        2.expr和一个整数相加,看返回值是否为0
        3.字符子串:将不是数字的删除,看字符串长度是否和原来的相等
    

    计算字符长度

    1:[root@myredhat ~]# expr length "$string"
      15
    2:[root@myredhat ~]# echo $string
      i am a good boy
      [root@myredhat ~]# echo ${string}|wc -L
      15
    3:[root@myredhat ~]# echo ${string} | awk '{print length($0)}'
      15
    4:[root@myredhat~]echo "${#string}"
    注:
    若是字符串,wc -L 则统计字符个数
    若是文本,wc -L 用于打印最长行的长度,而,wc -l 显示一共的行数
    
    

    判断字符长度是否为0

        1.-n 、-z判断
        2.字符子串 :IP=hello,[ ${#IP} -eq 0 ]
        3.expr length "$IP" -eq 0
        4.$(echo $IP | wc -L) -eq 0
        5.$(echo $IP | awk '{print length}') -eq 0
    

    shell中的test命令

    Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

    数值测试

    参数 说明
    -eq 等于则为真
    -ne 不等于则为真
    -gt 大于则为真
    -ge 大于等于则为真
    -lt 小于则为真
    -le 小于等于则为真

    例如:

    num1=100
    num2=100
    if test $[num1] -eq $[num2]
    then    
    	echo 'The two numbers are equal!'
    else    
    	echo 'The two numbers are not equal!'
    fi
    
    [] {} 都可以指定变量的界限
    test可以使用[]代替
    

    输出:
    The two numbers are equal!

    字符串测试

    参数 说明
    = 等于则为真
    != 不相等则为真
    -z 字符串 字符串长度伪则为真
    -n 字符串 字符串长度不伪则为真

    例如:

    num1="100"
    num2="100"
    if test num1=num2
    then    
    	echo 'The two strings are equal!'
    else    
    	echo 'The two strings are not equal!'
    fi	#输出:The two strings are equal!
    
    [root@hass-11 test]# vim 6.txt
    num1="100"
    num2="100"
    if test -z num1=num2
    then
            echo '1'
    else
            echo '2'
    fi	#输出为2
    
    [root@hass-11 test]# vim 6.txt
    num1="100"
    num2="100"
    if test -n num1=num2
    then
            echo '1'
    else
            echo '2'
    fi	#输出为1
    

    文件测试

    参数 说明
    -e 文件名 如果文件存在则为真
    -r 文件名 如果文件存在且可读则为真
    -w 文件名 如果文件存在且可写则为真
    -x 文件名 如果文件存在且可执行则为真
    -s 文件名 如果文件存在且至少有一个字符则为真
    -d 文件名 如果文件存在且为目录则为真
    -f 文件名 如果文件存在且为普通文件则为真
    -c 文件名 如果文件存在且为字符型特殊文件则为真
    -b 文件名 如果文件存在且为块特殊文件则为真

    例如:

    #判断文件或目录是否存在
    [root@hass-11 test]# vim 6.txt
    #!/bin/bash
    cd /bin
    if test -e ./bash
    then
            echo '1'
    else
            echo '2'
    fi
    
    #判断文件是否为空
    [root@hass-11 test]# vim 6.txt
    #!/bin/bash
    if test -s /script/test/7.txt
    then
            echo '1'
    else
            echo '2'
    fi	#7.txt文件为空,输出为2
    
    
    

    另外,Shell还提供了与( ! )、或( -o )、非( -a )三个逻辑操作符用于将测试条件连接起来,其优先级为:“!”最高,“-a”次之,“-o”最低。例如:

    [root@hass-11 test]# vim 6.txt
    #!/bin/bash
    if test -e ./a.txt -o ./b.txt -o ./3.txt -o ./4.txt -o ./bash
    then
            echo '1'
    else
            echo '2'
    fi	#只会输出1
    
    #多个 -a -o 的套用,可以使用()表示集合关系
    #-e后面不支持接多个文件
    
  • 相关阅读:
    Elementary Methods in Number Theory Exercise 1.2.25
    Elementary Methods in Number Theory Exercise 1.2.14
    图解欧几里德算法
    图解欧几里德算法
    Elementary Methods in Number Theory Exercise 1.2.14
    Android中的长度单位详解(dp、sp、px、in、pt、mm)
    分享下多年积累的对JAVA程序员成长之路的总结
    android异常之都是deamon惹的祸The connection to adb is down, and a severe error has occured.
    TomatoCartv1.1.8.2部署时报错
    JavaScript浏览器对象之二Document对象
  • 原文地址:https://www.cnblogs.com/syy1757528181/p/13603723.html
Copyright © 2011-2022 走看看