📜  Shell 脚本中的基本运算符

📅  最后修改于: 2022-05-13 01:57:05.196000             🧑  作者: Mango

Shell 脚本中的基本运算符

bash/shell 脚本中有5 个基本运算符:

  • 算术运算符
  • 关系运算符
  • 布尔运算符
  • 位运算符
  • 文件测试运算符

1.算术运算符:这些运算符用于执行正常的算术/数学运算。有 7 个算术运算运算符:

  • 加法(+) :用于添加两个操作数的二元运算。
  • 减法 (-) :用于将两个操作数相减的二进制运算。
  • 乘法 (*) :用于将两个操作数相乘的二元运算。
  • 除法 (/) :用于将两个操作数相除的二进制运算。
  • 模数 (%) :用于查找两个操作数的余数的二元运算。
  • 增量运算符 (++) :用于将操作数的值加一的一元运算符。
  • 递减运算符 (- -) :用于将操作数的值减一的一元运算符
C
#!/bin/bash
  
#reading data from the user
read - p 'Enter a : ' a
           read
    - p 'Enter b : ' b
  
          add
    = $((a + b))
        echo Addition of a and b are $add
  
            sub
    = $((a - b))
        echo Subtraction of a and b are $sub
  
            mul
    = $((a * b))
        echo Multiplication of a and b are $mul
  
            div
    = $((a / b))
        echo division of a and b are $div
  
            mod
    = $((a % b))
          echo Modulus of a
      and b are $mod
  
      ((++a))
          echo Increment
          operator when applied on "a" results into a = $a
  
      ((--b))
          echo Decrement
          operator when applied on "b" results into b = $b


C
#!/bin/bash
   
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
   
if(( $a==$b ))
then
    echo a is equal to b.
else
    echo a is not equal to b.
fi
   
if(( $a!=$b ))
then
    echo a is not equal to b.
else
    echo a is equal to b.
fi
   
if(( $a<$b ))
then
    echo a is less than b.
else
    echo a is not less than b.
fi
   
if(( $a<=$b ))
then
    echo a is less than or equal to b.
else
    echo a is not less than or equal to b.
fi
   
if(( $a>$b ))
then
    echo a is greater than b.
else
    echo a is not greater than b.
fi
   
if(( $a>=$b ))
then
    echo a is greater than or equal to b.
else
    echo a is not greater than or equal to b.
fi


C
#!/bin/bash
  
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
  
if(($a == "true" & $b == "true" ))
then
    echo Both are true.
else
    echo Both are not true.
fi
  
if(($a == "true" || $b == "true" ))
then
    echo Atleast one of them is true.
else
    echo None of them is true.
fi
  
if(( ! $a == "true"  ))
then
    echo "a" was initially false.
else
     echo "a" was initially true.
 fi


C
#!/bin/bash
  
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
  
bitwiseAND=$(( a&b ))
echo Bitwise AND of a and b is $bitwiseAND
  
bitwiseOR=$(( a|b ))
echo Bitwise OR of a and b is $bitwiseOR
  
bitwiseXOR=$(( a^b ))
echo Bitwise XOR of a and b is $bitwiseXOR
  
bitiwiseComplement=$(( ~a ))
echo Bitwise Compliment of a is $bitiwiseComplement
  
leftshift=$(( a<<1 ))
echo Left Shift of a is $leftshift
  
rightshift=$(( b>>1 ))
echo Right Shift of b is $rightshift


C
#!/bin/bash
  
#reading data from the user
read -p 'Enter file name : ' FileName
  
if [ -e $FileName ]
then
    echo File Exist
else
    echo File doesnot exist
fi
  
if [ -s $FileName ]
then
    echo The given file is not empty.
else
    echo The given file is empty.
fi
  
if [ -r $FileName ]
then
    echo The given file has read access.
else
    echo The given file does not has read access.
fi
  
if [ -w $FileName ]
then
    echo The given file has write access.
else
    echo The given file does not has write access.
fi
  
if [ -x $FileName ]
then
    echo The given file has execute access.
else
    echo The given file does not has execute access.
fi


输出:

2.关系运算符:关系运算符运算符定义两个操作数之间关系的运算符。他们根据关系给出真或假。它们有 6 种类型:

  • '==' 运算符:双等于运算符比较两个操作数。如果它们相等,则返回 true,否则返回 false。
  • '!=' 运算符:如果两个操作数不相等,则不等于运算符返回 true,否则返回 false。
  • '<' 运算符:如果第一个操作数小于第二个操作数,则小于运算符返回 true,否则返回 false。
  • '<=' 运算符:如果第一个操作数小于或等于第二个操作数,则小于或等于运算符返回 true,否则返回 false
  • '>' 运算符:如果第一个操作数大于第二个操作数,则大于运算符返回 true,否则返回 false。
  • '>=' 运算符:如果第一个操作数大于或等于第二个操作数,则大于或等于运算符返回 true,否则返回 false

C

#!/bin/bash
   
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
   
if(( $a==$b ))
then
    echo a is equal to b.
else
    echo a is not equal to b.
fi
   
if(( $a!=$b ))
then
    echo a is not equal to b.
else
    echo a is equal to b.
fi
   
if(( $a<$b ))
then
    echo a is less than b.
else
    echo a is not less than b.
fi
   
if(( $a<=$b ))
then
    echo a is less than or equal to b.
else
    echo a is not less than or equal to b.
fi
   
if(( $a>$b ))
then
    echo a is greater than b.
else
    echo a is not greater than b.
fi
   
if(( $a>=$b ))
then
    echo a is greater than or equal to b.
else
    echo a is not greater than or equal to b.
fi

输出:

3. 逻辑运算符:它们也被称为布尔运算符。这些用于执行逻辑操作。它们有 3 种类型:

  • 逻辑与(&&) :这是一个二元运算符,如果两个操作数都为真,则返回真,否则返回假。
  • 逻辑或 (||) :这是一个二元运算符,如果其中一个操作数为真,或者两个操作数都为真,则返回真,如果都不为假,则返回假。
  • 不等于 (!) :这是一个一元运算运算符,如果操作数为假则返回真,如果操作数为真则返回假。

C

#!/bin/bash
  
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
  
if(($a == "true" & $b == "true" ))
then
    echo Both are true.
else
    echo Both are not true.
fi
  
if(($a == "true" || $b == "true" ))
then
    echo Atleast one of them is true.
else
    echo None of them is true.
fi
  
if(( ! $a == "true"  ))
then
    echo "a" was initially false.
else
     echo "a" was initially true.
 fi

输出:

4.位运算符:位运算运算符是用于对位模式执行按位运算的运算运算符。它们有 6 种类型:

  • 按位与 (&) :按位 &运算符对操作数逐位执行二进制与运算。
  • 按位或 (|) :按位 |运算符对操作数逐位执行二进制或运算。
  • 按位异或 (^) :按位 ^运算符对操作数逐位执行二进制异或运算。
  • 按位补码 (~) :按位 ~运算符对操作数逐位执行二进制 NOT 运算。
  • 左移 (<<) :此运算符将左操作数的位向左移动由右操作数指定的次数。
  • 右移 (>>) :此运算符将左操作数的位向右移动由右操作数指定的次数。

C

#!/bin/bash
  
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
  
bitwiseAND=$(( a&b ))
echo Bitwise AND of a and b is $bitwiseAND
  
bitwiseOR=$(( a|b ))
echo Bitwise OR of a and b is $bitwiseOR
  
bitwiseXOR=$(( a^b ))
echo Bitwise XOR of a and b is $bitwiseXOR
  
bitiwiseComplement=$(( ~a ))
echo Bitwise Compliment of a is $bitiwiseComplement
  
leftshift=$(( a<<1 ))
echo Left Shift of a is $leftshift
  
rightshift=$(( b>>1 ))
echo Right Shift of b is $rightshift

输出:

5. 文件测试运算符:这些运算符用于测试文件的特定属性。

  • -b运算符:此运算符检查文件是否为块特殊文件。如果文件是块特殊文件,则返回 true,否则返回 false。
  • -c运算符:此运算符检查文件是否为字符特殊文件。如果它是字符特殊文件,则返回 true,否则返回 false。
  • -d运算符:此运算符检查给定目录是否存在。如果存在,则运算符返回 true,否则返回 false。
  • -e运算符:此运算符检查给定文件是否存在。如果它退出,则此运算符返回 true,否则返回 false。
  • -r运算符:此运算符检查给定文件是否具有读取权限。如果它具有读取访问权限,则返回 true,否则返回 false。
  • -w运算符:此运算符检查给定文件是否具有写访问权限。如果它已写入,则返回 true,否则返回 false。
  • -x运算符:此运算符检查给定文件是否具有执行访问权限。如果它具有执行访问权限,则返回 true,否则返回 false。
  • -s运算符:此运算符检查给定文件的大小。如果给定文件的大小大于 0,则返回 true,否则返回 false。

C

#!/bin/bash
  
#reading data from the user
read -p 'Enter file name : ' FileName
  
if [ -e $FileName ]
then
    echo File Exist
else
    echo File doesnot exist
fi
  
if [ -s $FileName ]
then
    echo The given file is not empty.
else
    echo The given file is empty.
fi
  
if [ -r $FileName ]
then
    echo The given file has read access.
else
    echo The given file does not has read access.
fi
  
if [ -w $FileName ]
then
    echo The given file has write access.
else
    echo The given file does not has write access.
fi
  
if [ -x $FileName ]
then
    echo The given file has execute access.
else
    echo The given file does not has execute access.
fi

输出: