shell脚本的简单使用:三—— 之运算符使用

简单算术运算符
expr表达式计算工具在shell中的使用

注意:
1.条件表达式要放在方括号之间,并且要有空格,例如 [$a==$b] 是错误的,必须写成 [ $a == $b ]。
2.乘号(*)前边必须加反斜杠(\)才能实现乘法运算;

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

关系运算符列表
-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。


布尔运算符
布尔运算符列表
!	非运算,表达式为 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。


字符串运算符
= 检测两个字符串是否相等,相等返回 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。

if [ $a != $b ]
then
 echo "$a != $b : is not equals"
else
 echo "$a != $b : is equals"
fi

if [ $a -lt 100 -a $b -gt 15 ]
then
 echo "a < 100 && b > 15 is true"
else
 echo "a < 100 && b > 15 is false"
fi

if [ $a -gt 100 -o $b -gt 15 ]
then
 echo "a > 100 && b > 15 is true"
else
 echo "a > 100 && b > 15 is false"
fi



#字符串运算符
str="abc"
str1="bcd"

echo "定义两个字符串变量str=abc,str1=bcd"
if [ $str = $str1 ]
then
 echo "str is equals to str1"
else
 echo "str is not equals to str1"
fi



if [ -z $str ]
then
 echo "str is zero"
else
 echo "str is not zero"
fi


unset str
if [ -z $str ]
then
 echo "unset str is zero"
else
 echo "unset str is not zero"
fi


if [ -n $str ]
then
 echo "unset str is not zero"
else
 echo "unset str is zero"
fi

if [ $str ]
then
 echo "str is not empty"
else
 echo "str is empty"
fi
"arithmetic.sh" 161L, 1841C written                                                                                                                                                                                       
[root@iZ28wn3bjxxZ test]# sh arithmetic.sh 
sum 2+2=4
a + b=30
a - b=-10
a * b=200
a / b=0
a % b=10
a is not equals to b
定义3个变量x=20,y=10,z=20
x not equals to y
x equals to z
x gt y
x not et z
x ge z
x le z
10 != 20 : is not equals
a < 100 && b > 15 is true
a > 100 && b > 15 is true
定义两个字符串变量str=abc,str1=bcd
str is not equals to str1
str is not zero
unset str is zero
unset str is not zero
str is empty
[root@iZ28wn3bjxxZ test]# cat arithmetic.sh 
#!/bin/bash

#shell中的计算
#simple example
var=`expr 2 + 2`
echo "sum 2+2=$var"



a=10
b=20
val=`expr $a + $b`
echo "a + b=$val"

val=`expr $a - $b`
echo "a - b=$val"

val=`expr $a \* $b`
echo "a * b=$val"

val=`expr $a / $b`
echo "a / b=$val"


val=`expr $a % $b`
echo "a % b=$val"

if [ $a == $b ]
then 
  echo "a is equals to b"
fi

if [ $a != $b ]
then 
 echo "a is not equals to b"
fi


#关系运算符在shell中不支持字符串,只支持数字或者是数字的字符串
x=20
y=10
z=20
echo "定义3个变量x=20,y=10,z=20"

if [ $x -eq $y ]
then 
 echo "x equals to y"
else
 echo "x not equals to y"
fi


if [ $x -ne $z ]
then 
 echo "x not equals to z"
else
 echo "x equals to z"
fi


if [ $x -gt $y ]
then 
 echo "x gt y"
else
 echo “x lt y”
fi


if [ $x -lt $z ]
then
 echo "x et z"
else
 echo "x not et z"
fi


if [ $x -ge $z ]
then 
 echo "x ge z"
else
 echo "x not ge z"
fi


if [ $x -le $z ]
then 
 echo "x le z"
else
 echo "x not le z"
fi


#布尔运算符

if [ $a != $b ]
then 
 echo "$a != $b : is not equals"
else
 echo "$a != $b : is equals"
fi

if [ $a -lt 100 -a $b -gt 15 ] 
then
 echo "a < 100 && b > 15 is true"
else
 echo "a < 100 && b > 15 is false"
fi

if [ $a -gt 100 -o $b -gt 15 ]
then
 echo "a > 100 && b > 15 is true"
else
 echo "a > 100 && b > 15 is false"
fi



#字符串运算符
str="abc"
str1="bcd"

echo "定义两个字符串变量str=abc,str1=bcd"
if [ $str = $str1 ]
then 
 echo "str is equals to str1"
else 
 echo "str is not equals to str1"
fi



if [ -z $str ]
then 
 echo "str is zero"
else 
 echo "str is not zero"
fi


unset str
if [ -z $str ]
then
 echo "unset str is zero"
else
 echo "unset str is not zero"
fi


if [ -n $str ]
then
 echo "unset str is not zero"
else
 echo "unset str is zero"
fi

if [ $str ]
then 
 echo "str is not empty"
else 
 echo "str is empty"
fi


执行结果
sum 2+2=4
a + b=30
a - b=-10
a * b=200
a / b=0
a % b=10
a is not equals to b
定义3个变量x=20,y=10,z=20
x not equals to y
x equals to z
x gt y
x not et z
x ge z
x le z
10 != 20 : is not equals
a < 100 && b > 15 is true
a > 100 && b > 15 is true
定义两个字符串变量str=abc,str1=bcd
str is not equals to str1
str is not zero
unset str is zero
unset str is not zero
str is empty


文件测试运算符
文件测试运算符列表
-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。
#!/bin/sh

file=/root/test/test_file.sh

if [ -r $file ]
then 
 echo "$file has read access"
else 
 echo "$file 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 excute permission"
else
 echo "$file does not have excute permission"
fi

if [ -f $file ]
then
 echo "$file is an ordinary file"
else
 echo "$file is specil file"
fi

执行后的结果
/root/test/file_test.sh has read access
/root/test/file_test.sh has write permission
/root/test/file_test.sh has excute permission
/root/test/file_test.sh is an ordinary file
/root/test/file_test.sh is exist

猜你喜欢

转载自janle.iteye.com/blog/2367784