Expressions and Operators

  Bash supports many operators, including arithmetic operators, relational operators, boolean operators, string operators, and file test operators.

1. Expression

  Bash expressions can be divided into two types: arithmetic expressions and logical expressions.

1. Arithmetic expressions

  Mathematical operations involve the evaluation of expressions. Bash itself does not support simple mathematical operations, but commands such as awk and expr can be used to implement mathematical operations. Among them, expr is the most commonly used, and it can be used to evaluate expressions. Example, adding two numbers and expressing the result of the operation

expr 5 + 3

  Note: There must be a space between the operand and the operator, otherwise expr will simply output it as a string.

  Of course, the computed value can be used as a variable, or the result of expr computation can be assigned to a variable.

n=1

m=5

expr $n + $m

val=`expr 2 + 2`

echo $val

 

  Note that the complete expression is enclosed in backticks "`".

  A simpler way is to use the $[] expression to do the math, e.g.

val=$[5+3]
echo $val

  

This form does not require a space between the operator and the operand.

  You can also use the let command to evaluate integer expressions, such as

n=1
m=5
let val=$n+$m
echo $val

  

2. Logical expressions

   Logical expressions are mainly used for conditional judgment. A value of true (or 0) indicates that the result is true, and a value of false indicates that the result is false.

  Usually use the test command to test the truth of the expression. The syntax format is as follows:

test logical expression

  For example the following statement is used to compare two strings for equality.

test "abc"="xyz"

  The test command is more often replaced with square brackets "[]". The syntax is as follows

[logical expression]

   When an opening square bracket is used instead of test, it must always be followed by a space, the logical expression to be evaluated, a space, and the closing square bracket, which marks the end of the expression to be evaluated. The spaces around the logical expression are required, which means that test is called, to distinguish it from the character, pattern-matching operations that also frequently use square brackets.

  A logical expression evaluates the result of the expression and then returns true or false. It is usually used in conjunction with the if, while, or until commands for conditional evaluation to provide extensive control over program flow.

2. Arithmetic operators

  Arithmetic operators are used for numerical calculations. The main arithmetic operators are listed as follows:

+: addition
-: Subtraction
*:multiplication
/:division
%; take the remainder
=: assignment

  Here is an example of using arithmetic operators

#!/bin/bash
a=1
b=2
val=`expr $a + $b`
echo "a + b : $val"
#Multiplication (*) must be preceded by an escape symbol backslash (\) to achieve multiplication
val=`expr $a \* $b`
echo "a*b:$val"

  

3. Integer relational operators

   Shell supports integer comparison, and also requires the use of integer relational operators. The list is as follows:

-eq: equal, check whether the two numbers are equal, return true if they are equal
-ne: not equal. Check if two numbers are equal, return true if they are not equal
-gt: Greater than. Check if the number on the left is greater than the number on the right, if so, return true
-lt: Less than. Check if the number on the left is less than the number on the right, if so, return true
-ge: greater than or equal to
-le: less than or equal to

   Here is an example of a relational operator

#!/bin/bash

a=10

b=20

if [ $a -eq $b ]

then

	echo "$a -eq $b :a equals b"

else

	echo "$a -eq $b: a is not equal to b"

be

  

4. String detection operator

  String operators are used to detect strings, the list is as follows:

=: Checks whether two strings are equal, and returns true if they are equal.
!=: Checks whether two strings are equal, and returns true if they are not equal.
-z: Check if the string length is 0, and return true if it is 0.
-n: Check whether the length of the string is 0, and return true if it is not 0.
str: Check if the string is empty, return true if it is not empty.

  An example is given below

#!/bin/bash
a="abc"
b="xyz"
if [ $a = $b ]
then
	echo "$a = $b : a equals b"
else
	echo "$a = $b : a is not equal to b"
be
	if [ -z $a ]
then
	echo "-z $a : string length is 0 "
else
	echo "-z $a string length is not 0"
be
	if [ $a ]
then
	echo "$a : string is not empty"
else
	 echo "$a : string is empty"
be

 

5. File Test Operator

  The file test operator is used to detect various properties of the file, taking the file name as a parameter

-b: Check if the file is a block device file, if so, return true.
-c: Check if the file is a character device file, and if so, return true.
-d : Check if the file is a directory, if so, return true.
-f: Check if the file is a normal file, if so, return true.
-g: Detects whether the file has the SGID bit set, and if so, returns true.
-k: Check if the file has the sticky bit set, and if so, return true.
-p: Check if the file is a named pipe, if so, return, true.
-u: Check whether the file has the SUID bit set, and if so, return true.
-r: Check if the file is ticked, if so, return true.
-w: Check if the file is writable, if so, return true.
-x : Check if the file is executable, if so, return true.
-s: Check if the file is empty, return true if it is not empty.
-e: Check if the file exists, if so, return true.

  For example, the following code will detect the read-write attribute of /bin/bash

#!/bin/bash
file="/bin/bash"
if [ -r $file ]
then
  echo "File has read permission"
else
  echo "File does not have read permission"
be
if [ -w $file ]
  then
  echo "File has write permission"
else
  echo "File does not have write permission"
be

6. Boolean operators

  Boolean operators are used to perform logical operations on one or more logical expressions, resulting in either true or false. Usually used to judge multiple conditions. There are 3 operators and, or, not.

  -a: AND operation. Returns true if both conditions are true.

  -o: OR operation. Returns true if one of the expressions is true.

  ! : NOT operation. Returns false if the expression is true, otherwise returns true.

Here is an example:

#!/bin/bash

a=5

b=10

if [ $a -lt 10 -a $b -gt 15 ]

   then

	echo "Both conditions are met"

else

	echo "Both conditions are not met"

be

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325255247&siteId=291194637