shell 编程 (5)基本运算符

25 篇文章 0 订阅
15 篇文章 0 订阅

一、算术运算

1. expr支持算术运算

使用反引号``, 表达式与运算符之间必须有空格。`expr 2 + 3`中的2、3与+之间必须有空格。

[root@k8s-master ~]# val=`expr 2 + 3`
[root@k8s-master ~]# echo $val
5

+:加

-:减

*:乘 (符号前必须加转义符反余杠/)

/:除

%:取余

=:赋值

==:相等(比较数字)

!=:不相等(比较数字)

条件表达式要放在[]之间,必须要有空格,[$a==$b]错, [ $a == $b ]这样才行。

eg:

[root@k8s-master test1]# ./test0.sh 
a+b: 3
a-b: -1
a*b: 2
a/b: 0
a%b: 1
a != b
[root@k8s-master test1]# cat test0.sh 
#!/bin/bash

a=1
b=2

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 = b"
fi

if [ $a != $b ]
then
  echo "a != b"
fi
[root@k8s-master test1]# 

二、关系运算符

只支持数字,不支持字符串

-eq: 两个数是否相等

-ne: 是否不相等

-gt: 是否大于

-lt: 是否小于

-ge: 是否大于等于

-le: 是否小于等于

eg:

[root@k8s-master test1]# ./test1.sh 
1 -eq 2: a!=b
1 -ne 2: a!=b
1 -gt 2: a!>b
1 -lt 2: a<b
1 -ge 2: a<b
1 -le 2: a<=b
[root@k8s-master test1]# cat test1.sh 
#!/bin/bash
a=1
b=2

if [ $a -eq $b ]
then
  echo "$a -eq $b : a = b"
else
  echo "$a -eq $b: a!=b"
fi

if [ $a -ne $b ]
then
  echo "$a -ne $b: a!=b"
else
  echo "$a -ne $b: a = b"
fi

if [ $a -gt $b ]
then
  echo "$a -gt $b: a>b"
else
  echo "$a -gt $b: a!>b"
fi

if [ $a -lt $b ]
then
  echo "$a -lt $b: a<b"
else
  echo "$a -lt $b: a>=b"
fi

if [ $a -ge $b ]
then
  echo "$a -ge $b: a >= b"
else
  echo "$a -ge $b: a<b"
fi

if [ $a -le $b ]
then
  echo "$a -le $b: a<=b"
else
  echo "$a -le $b: a>b"
fi


[root@k8s-master test1]# 

三、布尔运算符

!:非

-o: 或

-a: 与

eg:

[root@k8s-master test1]# ./t2.sh 
1 != 2: a!=b
 1 -lt 3 -a 2 -gt 1: true
 1 -lt 0 -o 2 -gt 1 : true
[root@k8s-master test1]# cat t2.sh 
#!/bin/bash

a=1
b=2

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

if [ $a -lt 3 -a $b -gt 1 ]
then
  echo " $a -lt 3 -a $b -gt 1: true"
else
  echo " $a -lt 3 -a $b -gt 1: false"
fi

if [ $a -lt 0 -o $b -gt 1 ]
then
  echo " $a -lt 0 -o $b -gt 1 : true"
else
  echo " $a -lt 0 -o $b -gt 1 : false"
fi


[root@k8s-master test1]# 

四、逻辑运算

&&: AND

||: OR

[root@k8s-master test1]# ./t3.sh 
1 -lt 10 && 2 -gt 20: false
1 -lt 10 || 2 -gt 20: true
[root@k8s-master test1]# cat t3.sh 
#!/bin/bash

a=1
b=2

if [[ $a -lt 10 && $b -gt 20 ]]
then
  echo "$a -lt 10 && $b -gt 20: true"
else
  echo "$a -lt 10 && $b -gt 20: false"
fi

if [[ $a -lt 10 || $b -gt 20 ]]
then
  echo "$a -lt 10 || $b -gt 20: true"
else
  echo "$a -lt 10 || $b -gt 20: false"
fi
[root@k8s-master test1]# 

 

五、字符串运算符

=:字符串相等为true

!=:字符串不相等为true

-z: 长度为0为true

-n: 长度不为0为true

$:空串为true

eg:

[root@k8s-master test1]# ./t4.sh 
 abc = def: false
abc != def: true
-z abc: false
-n abc: true
 abc: not null
[root@k8s-master test1]# cat t4.sh 
#!/bin/bash

a="abc"
b="def"

if [ $a = $b ]
then
  echo " $a = $b: true"
else
  echo " $a = $b: false"
fi

if [ $a != $b ]
then
  echo "$a != $b: true"
else
  echo "$a != $b: false"
fi

if [ -z $a ]
then
  echo "-z $a: true"
else
  echo "-z $a: false"
fi

if [ -n $a ]
then
  echo "-n $a: true"
else
  echo "-n $a: false"
fi

if [ $a ]
then
  echo " $a: not null"
else
  echo " $a: null"
fi

[root@k8s-master test1]# 

六、文件检测

-c: 块设备

-c: 字符设备

-d: 目录

-f: 普通文件,不是目录及设备

-g: sgid位

-k: 粘着位(sticky bit)

-p: 有名管道

-u: suid位

-r: 可读

-w: 可写

-x: 可执行

-s: 不为空,大小大于0

-e: 文件或目录是否存在

-S: socket

-L: 符号链接

eg:

[root@k8s-master test1]# ls -l /root/test/sh-test/test1/t4.sh
-rwxr-xr-x. 1 root root 393 3月  21 00:37 /root/test/sh-test/test1/t4.sh
[root@k8s-master test1]# ./t5.sh 
/root/test/sh-test/test1/t4.sh : read
/root/test/sh-test/test1/t4.sh : write
/root/test/sh-test/test1/t4.sh : execute
/root/test/sh-test/test1/t4.sh: ordinary
/root/test/sh-test/test1/t4.sh: not dir
/root/test/sh-test/test1/t4.sh: not empty
/root/test/sh-test/test1/t4.sh: exist
[root@k8s-master test1]# cat t5.sh 
#!/bin/bash

f="/root/test/sh-test/test1/t4.sh"

if [ -r $f ]
then
  echo "$f : read"
else
  echo "$f: no read"
fi

if [ -w $f ]
then
  echo "$f : write"
else
  echo "$f: not write"
fi

if [ -x $f ]
then
  echo "$f : execute"
else
  echo "$f: not execute"
fi

if [ -f $f ]
then
  echo "$f: ordinary"
else
  echo "$f: not ordinary"
fi

if [ -d $f ]
then
  echo "$f: dir"
else
  echo "$f: not dir"
fi

if [ -s $f ]
then
  echo "$f: not empty"
else
  echo "$f: empty"
fi

if [ -e $f ]
then
  echo "$f: exist"
else
  echo "$f: not exist"
fi

[root@k8s-master test1]# 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值