[Shell] Common operators in shell scripts. Addition, subtraction, multiplication, division, remainder, greater than, less than, not equal to, &&, ||, determine whether the file exists

Shell basic operators

The shell supports a variety of operators, including:

  • arithmetic operators
  • relational operator
  • boolean operator
  • string operator
  • file test operator

Native bash does not support simple mathematical operations, but can be achieved through other commands, such as awk and expr, expr is the most commonly used.

expr is an expression evaluation tool that can be used to evaluate expressions.

For example, adding two numbers (note the use of backticks ` instead of single quotes'):

#!/bin/bash

val=`expr 2 + 3`
echo "两数之和为 : $val"

Execute the script and the output is as follows:

两数之和为 : 5

Note here:

There should be spaces between expressions and operators, for example 2+2 is wrong, it must be written as 2 + 2, which is different from most programming languages ​​we are familiar with.

The complete expression should be enclosed by ` `, note that this character is not the usual single quote, below the Esc key.

1. Arithmetic operators

The following table lists common arithmetic operators, assuming that variable a is 10 and variable b is 20:

operator illustrate Example
+ addition `expr $a + $b` results in 30.
- subtraction `expr $a - $b` results in -10.
* multiplication `expr $a \* $b` results in 200.
/ division `expr $b / $a` results in 2.
% remainder `expr $b % $a` returns 0.
= assign a=$b assigns the value of variable b to a.
== equal. Used to compare two numbers, returns true if they are the same. [ $a == $b ] returns false.
!= not equal. Used to compare two numbers, returns true if they are not the same. [ $a != $b ] returns true.

Note : Conditional expressions should be placed between square brackets and spaces. For example: [$a==$b] is wrong and must be written as [ $a == $b ] .

Example:

#!/bin/bash
a=10
b=20

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

if [ $a == $b ]
then
   echo "a 等于 b"
fi
if [ $a != $b ]
then
   echo "a 不等于 b"
fi

Execute the script and the output is as follows:

b % a : 0
a 不等于 b

Notice:

  • The multiplication sign (*) must be preceded by a backslash (\) to achieve multiplication;
  • if...then...fi is a conditional statement, which will be explained later.
  • The expr syntax of the shell in MAC is: $((expression)) , where the "*" in the expression does not need the escape symbol "\".

1.1 Extension

For arithmetic operations, we can also write it like this:

  1. use[]
#!/bin/bash
num1=10
num2=16

echo "num1+num2="$[num1+num2]
echo "num1-num2="$[num1-num2]
echo "num1*num2="$[num1*num2]
echo "num1/num2="$[num1/num2]
echo "num2/num1="$[num2/num1]

Execute the script and the output is as follows:

num1+num2=26
num1-num2=-6
num1*num2=160
num1/num2=0
num2/num1=1

Note: When doing division, only the integer part is displayed, and the fractional part is not displayed.

  1. use(())
#!/bin/bash
num1=10
num2=16

echo "num1+num2="$(($num1+$num2))
echo "num1-num2="$(($num1-$num2))
echo "num1*num2="$(($num1*$num2))
echo "num1/num2="$(($num1/num2))
echo "num2/num1="$(($num2/$num1))

Execute the script and the output is as follows:

num1+num2=26
num1-num2=-6
num1*num2=160
num1/num2=0
num2/num1=1

Note: When doing division, only the integer part is displayed, and the fractional part is not displayed.

  1. decimal operation

scale=n; defines the decimal precision, in addition, you can directly run the bc command for input operations.

#!/bin/bash
num1=1.8
num2=2.2
echo "scale=3;$num1+$num2"|bc
echo "scale=2;$num1-$num2"|bc
echo "scale=2;$num2-$num1"|bc
echo "scale=2;$num1*$num2"|bc
echo "scale=2;$num1/$num2"|bc
echo "scale=2;$num2/$num1"|bc
echo "scale=3;$num1/$num2"|bc

Execute the script and the output is as follows:

4.0
-.4   # -0.4
.4    # 0.4
3.96
.81   # 0.81
1.22
.818  # 0.818

Note the change in decimal precision. And the integer part is 0 will be omitted. At the same time, the fractional part is truncated and not rounded off.

2. Relational Operators

Relational operators only support numbers, not strings, unless the value of the string is a number.

The following table lists commonly used relational operators, assuming that variable a is 10 and variable b is 20:

operator illustrate Example
-eq Checks whether two numbers are equal, and returns true if they are equal. [ $a -eq ​$b ] returns false.
-born Check if two numbers are not equal, return true if they are not equal. [ $a -ne ​$b ] returns true.
-gt Checks whether the number on the left is greater than the number on the right, and if so, returns true. [ $a -gt $b ] returns false.
-lt Checks whether the number on the left is less than the number on the right, and if so, returns true. [ $a -lt ​$b ] returns true.
-give Check if the number on the left is greater than or equal to the number on the right, and if so, return true. [ $a -ge $b ] returns false.
-the Checks whether the number on the left is less than or equal to the number on the right, and if so, returns true. [ $a -le $b ] returns true.

3. Boolean Operators

The following table lists common Boolean operators, assuming variable a is 10 and variable b is 20:

operator illustrate Example
! NOT operation, returns false if the expression is true, otherwise returns true. [ ! false ] returns true.
-O OR operation, returns true if one of the expressions is true. [ $a -lt 20 -o $b -gt 100 ] returns true.
-a AND operation returns true when both expressions are true. [ $a -lt 20 -a $b -gt 100 ] returns false.

4. Logical Operators

The following describes Shell's logical operators, assuming that variable a is 10 and variable b is 20:

operator illustrate Example
&& logical AND [[ $a -lt 100 && $b -gt 100 ]] 返回 false
|| logical OR [[ $a -lt 100 || $b -gt 100 ]] returns true

Example:

#!/bin/bash

a=10
b=20

if [[ $a -lt 100 && $b -gt 100 ]]
then
   echo "返回 true"
else
   echo "返回 false"
fi

if [[ $a -lt 100 || $b -gt 100 ]]
then
   echo "返回 true"
else
   echo "返回 false"
fi

Execute the script and the output is as follows:

返回 false
返回 true

Different from contact :

Difference between [] and [[ ]]:

  • [[ ]] logical combination can use && || notation
  • And the logical combination in [] can use -a -o

Ternary operator :

[ condition ] && echo 1 || echo 2 : If the condition is satisfied, execute the statement after &&; if the condition is not satisfied, execute the statement after ||

5. String operators

The following table lists common string operators, assuming variable a is "abc" and variable b is "efg":

operator illustrate Example
= Checks if two strings are equal, returns true for equality. [ $a = $b ] returns false.
!= Checks if two strings are not equal, returns true if they are not equal. [ $a != $b ] returns true.
-with Checks whether the string length is 0, and returns true if it is 0. [ -z $a ] returns false.
-n Check if the string length is not 0, return true if not. [ -n "$a" ] returns true.
$ Check if the string is empty, return true if it is not empty. [ $a ] returns true.

6. File Test Operator

File test operators are used to test various attributes of Unix files.

Attribute detection is described as follows:

operator illustrate Example
-b file Checks if the file is a block device file, and returns true if it is. [ -b $file ] returns false.
-c file Checks if the file is a character device file, and returns true if it is. [ -c $file ] returns 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

其他检查符:

  • -S: 判断某文件是否 socket。
  • -L: 检测文件是否存在并且是一个符号链接。

Guess you like

Origin blog.csdn.net/weixin_45842494/article/details/123513595