shell script—运算符

本文主要介绍shell script中的算数运算符、比较运算符、逻辑运算符、赋值运算符、位运算符和条件运算符

1.算数运算符

在shell脚本中,可以使用一下算术运算符:

运算符 描述
+ 加法
- 减法
* 乘法
/ 除法
% 取余数
++ 自增
- - 自减

示例

#!/bin/bash

a=20
b=10
c=$((a+b))
echo $c  # 输出 30

d=$((a-b))
echo $d

e=$((a*b))
echo $e

f=$((a/b))
echo $f

a=13
g=$((a%b))
echo $g

输出


30
10
200
2
3

除此之外,还可以用let命令或者是(( ))这样的双括号来进行算术运算,例如:

#!/bin/bash

a=5
b=10
let c=a+b
echo $c  # 输出 15

let d=a*b
echo $d  # 输出 50

(( a++ ))
echo $a  # 输出 6

(( --a ))
echo $a  # 输出 5

(( b-- ))
echo $b  # 输出 9

(( ++b ))
echo $b  # 输出 10

2.比较运算符

在shell脚本中,可以使用以下比较运算符(整数类型):

运算符 描述 说明
-eq 是否等于 equal
-ne 是否不等于 not equal
-gt 是否大于 greater than
-ge 是否大于等于 greater than or equal to
-lt 是否小于 less than
-le 是否小于等于 less than or equal to

示例

#!/bin/bash

a=10
b=20
if [ $a -eq $b ]
then
  echo 'a equals b'
else
  echo 'a not equals b'
fi

a=5
b=10
if [ $a -lt $b ]
then
  echo 'a is less than b'
else
  echo 'a is not less than b'
fi

str1='hello'
str2='world'
if [ $str1 == $str2 ]
then
  echo 'Strings are equal.'
else
  echo 'Strings are not equal.'
fi

输出

a not equals b
a is less than b
Strings are not equal.

从上面的示例可以看出字符串有单独的比较运算

运算符 描述
= / == 字符串是否相等
!= 字符串是否不相等
-z 字符串长度是否为0
-n 字符串长度是否不为0
str1 < str2 比较两个字符串的字典顺序
str1 > str2 比较两个字符串的字典顺序

示例

#!/bin/bash

str1="hello"
str2="world"
if [ $str1 = $str2 ]
then
  echo "str1 equals str2"
else
  echo "str1 is not equal to str2"
fi

if [ $str1 == $str2 ]
then
  echo "str1 equals str2"
else
  echo "str1 is not equal to str2"
fi

if [ $str1 > $str2 ]
then
  echo "str1 great than to str2"
else
  echo "str1 less than or equal to str2"
fi

str=""
if [ -z $str ]
then
  echo "The string is empty."
else
  echo "The string is not empty."
fi

str="hello"
if [ -n $str ]
then
  echo "The string is not empty."
else
  echo "The string is empty."
fi

输出

str1 is not equal to str2
str1 is not equal to str2
str1 great than to str2
The string is empty.
The string is not empty.

3.逻辑运算符

在Shell中,有三种逻辑运算符,分别是“&&”、“||”和“!”。

&&逻辑运算符

“&&”逻辑运算符表示“与”,用于连接两个条件语句,形成一个新的条件语句。当且仅当两个条件语句都为真时,该新条件语句才为真。

示例

if [ -f /etc/passwd ] && [ -r /etc/passwd ]
then
  echo "The file /etc/passwd exists and is readable."
else
  echo "The file /etc/passwd does not exist or is not readable."
fi

示例

a=1
b=2
c=3

if [ $a -eq $c ]&& [ $b -eq $c ]
then
	echo "ok"
else 
	echo "not ok"
fi

在上面的例子中,我们使用“&&”逻辑运算符连接了两个条件语句,分别判断 /etc/passwd 文件是否存在和是否可读。只有当两个条件语句都为真时,才会输出“The file /etc/passwd exists and is readable.”。

||逻辑运算符

“||”逻辑运算符表示“或”,用于连接两个条件语句,形成一个新的条件语句。当两个条件语句中有至少一个为真时,该新条件语句就为真。

示例

if [ -f /etc/passwd ] || [ -f /etc/shadow ]
then
  echo "The file /etc/passwd or /etc/shadow exists."
else
  echo "The file /etc/passwd and /etc/shadow do not exist."
fi

在上面的例子中,我们使用“||”逻辑运算符连接了两个条件语句,分别判断 /etc/passwd 文件和 /etc/shadow 文件是否存在。只有当两个条件语句中至少有一个为真时,才会输出“The file /etc/passwd or /etc/shadow exists.”。

!逻辑运算符

“!”逻辑运算符表示“非”,用于将一个条件语句的结果取反。例如:

示例

if [ ! -f /etc/passwd ]
then
  echo "The file /etc/passwd does not exist."
else
  echo "The file /etc/passwd exists."
fi
#!/bin/bash

a=1
b=2
c=3

if [ ! $a -eq $c ] || [ $b -eq $c ]
then
	echo "ok"
else 
	echo "not ok"
fi

#输出ok

在上面的例子中,我们使用“-f”检查 /etc/passwd 文件是否存在,并且使用“!”逻辑运算符将其结果取反。如果文件不存在,则输出“The file /etc/passwd does not exist.”。

4.赋值运算符

在Shell中,有多种赋值运算符,主要包括“=、+=、-=、*=、/=、%=、<<=、>>=、&=、^=、|=”。下面逐一介绍这些运算符的用法:

= 赋值运算符

“= ”赋值运算符用于将右边的值赋值给左边的变量。例如:

msg="Hello World"
echo $msg

上面的代码将字符串 “Hello World” 赋值给变量 msg,并将其打印出来。

+= 赋值运算符

“+=”赋值运算符用于将右边的值追加到左边变量的值的末尾。例如:

msg="Hello "
msg+="World"
echo $msg

上面的代码将字符串 "Hello " 赋值给变量 msg,然后使用“+=”将字符串 “World” 追加到 msg 变量的末尾,最终输出 “Hello World”。

-=、*=、/=、%= 赋值运算符

“-=、*=、/=、%=”赋值运算符分别表示减、乘、除和求余的赋值运算符,用于将右边的值与左边的变量的值进行对应的运算,然后将结果赋值给左边的变量。例如:

a=10
a-=5
echo $a  # 输出5

b=5
b*=3
echo $b  # 输出15

c=100
c/=10
echo $c  # 输出10

d=20
d%=3
echo $d  # 输出2

<<=、>>=、&=、^=、|= 赋值运算符

“<<=、>>=、&=、^=、|=”赋值运算符表示按位左移、按位右移、按位与、按位异或和按位或的赋值运算符,用于将右边的值与左边变量的值进行相应的位运算,然后将结果赋值给左边的变量。例如:

a=100      # 二进制为 01100100
a<<=2      # 相当于 a=a<<2,即二进制为 10010000,十进制为 144
echo $a   # 输出144

b=100      # 二进制为 01100100
b>>=2      # 相当于 b=b>>2,即二进制为 00011001,十进制为 25
echo $b   # 输出25

c=0b1011   # 二进制为 1011,十进制为 11
c&=0b1110  # 二进制为 1010,十进制为 10
echo $c   # 输出10

d=0b1011   # 二进制为 1011,十进制为 11
d^=0b1100  # 二进制为 0111,十进制为 7
echo $d   # 输出7

e=0b1011   # 二进制为 1011,十进制为 11
e|=0b1100  # 二进制为 1111,十进制为 15
echo $e   # 输出15

5.位运算符

在Shell中,有多种位运算符,可以用于在二进制位上执行位运算。主要包括“&、|、^、~、<<、>>”。

& 位运算符

“&”位运算符表示按位与运算符,用于将两个数的每个二进制位进行“与”运算,例如:

a=0b1010
b=0b0110
c=$((a & b))   # c的值为0b0010,即十进制的 2
echo $c

| 位运算符

“|”位运算符表示按位或运算符,用于将两个数的每个二进制位进行“或”运算,例如:

a=0b1010
b=0b0110
c=$((a | b))   # c的值为0b1110,即十进制的 14
echo $c

^ 位运算符

“^”位运算符表示按位异或运算符,用于将两个数的每个二进制位进行“异或”运算,例如:

a=0b1010
b=0b0110
c=$((a ^ b))   # c的值为0b1100,即十进制的 12
echo $c

~ 位运算符

“~ ”位运算符表示按位取反运算符,用于将一个数的每个二进制位进行“非”运算,例如:

a=0b1010
b=$((~a))   # b的值为0b0101,即十进制的 -11
echo $b

<< 位运算符

“<<”位运算符表示按位左移运算符,用于将一个数的各二进制位全部左移若干位,例如:

a=10    # 二进制为 1010
b=$((a << 2))    # b的值为0b101000,即十进制的 40
echo $b

>> 位运算符

“>>”位运算符表示按位右移运算符,用于将一个数的各二进制位全部右移若干位。例如:

a=10    # 二进制为 1010
b=$((a >> 2))    # b的值为0b10,即十进制的 2
echo $b

6.条件运算符

“trinary operator”条件运算符用于在一行代码中执行条件判断,其基本语法如下:

condition ? value1 : value2

在上面的语法中,condition是一个条件语句,当condition为真时,该条件运算符返回value1,否则返回value2。例如:

a=10
b=5
max=$((a > b ? a : b))
echo $max

在上面的代码中,我们使用条件运算符判断 a 是否大于 b,如果是,则将 a 的值赋。

Guess you like

Origin blog.csdn.net/shouhu010/article/details/131399850