Chinaunix首页 | 论坛 | 博客
  • 博客访问: 390965
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-03 18:24
文章分类

全部博文(75)

文章存档

2019年(1)

2018年(20)

2017年(14)

2016年(10)

2015年(30)

分类: LINUX

2018-03-15 23:28:29

1、二元比较操作符

1.1、整数比较

-eq   等于 如:if [ “$a” -eq “$b” ]

-ne   不等于 如:if [ “$a” -ne “$b” ]

-gt   大于 如:if [ “$a” -gt “$b” ]

-ge   大于或等于 如:if [ “$a” -ge “$b” ]

-lt   小于或等于 如:if [ “$a” -lt “$b”]

 

实例:

#!/bin/bash

a=8

b=10

 

##

if [ "${a}" -eq "${b}" ];then

printf "$a is equal $b\n"

else

printf "$a is not equal $b \n"

fi

 

##

if [ "${a}" -gt "${b}" ];then

printf "$a is great than $b\n"

else

printf "$a is not great than $b\n"

fi

执行结果:

tay@tay:/mnt/hgfs/hzs/shell$ ./if.sh

8 is not equal 10

8 is not great than 10

 

 

1.2、字符串的比较

= 等于 如:if [ “$a” = “$b” ]

== 等于 如:if [ “$a” == “$b” ],=等价

 

例子:

#!/bin/bash

 

_STRING_A_="this is string a"

_STRING_B_="this is string b"

 

if [ "${_STRING_A_}" = "${_STRING_B_}" ];then

printf "_STRING_A_ is equal _STRING_B_\n"

else

printf "_STRING_A_ is not equal _STRING_B_\n"

fi

 

if [ "${_STRING_A_}" == "${_STRING_B_}" ];then

printf "_STRING_A_ is equal _STRING_B_\n"

else

printf "_STRING_A_ is not equal _STRING_B_\n"

fi

 

执行结果:

tay@tay:/mnt/hgfs/hzs/shell$ ./string_compare.sh

_STRING_A_ is not equal _STRING_B_

_STRING_A_ is not equal _STRING_B_

 

 

 

关键点:括号两边各有一个空格,这是unix shelll的要求

阅读(808) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~