The article directories

  • The operator
    • 1. Basic introduction
    • 2. Basic grammar
    • 3. Example oper.sh

The operator

1. Basic introduction

  • Learn how to perform various operations in the shell.

2. Basic grammar

  1. $(formula)“Or”$[expression]“Orexpr m + n/ / expression expression
  2. Pay attention toexprThere must be between the operatorsThe blank spaceIf desiredexprAssigns the result ofvariable, the use ofThe quotation marks
  3. expr m - n

    4) expr \*, /, %Multiply, divide, mod

3. Example oper.sh

  1. Case 1: Calculation2 plus 3 times 4The value of the
#! /bin/bash
# Example 1: Calculate the value of (2+3) * 4
# Use the first method
RES1=$(((2+3) *4))
echo "res1=$RES1"
# Use the second option, recommended
RES2=$[(2+3)*4]
echo "red2=$RES2"
# Use the third way expR
TEMP=`expr 2 + 3`
RES4=`expr $TEMP4 ` \ *echo "temp=$TEMP"
echo "res4=$RES4"
Copy the code

  1. Case 2: Request the sum of two [integer] arguments on the command line20 to 50
#! /bin/bash
# Case 2: Request the sum of two [integer] arguments on the command line 20, 50
SUM=$[The $1+$2]
echo "sum=$SUM"
Copy the code