Linux --------------------------Shell basic operators

(1) Summary

Shell, like other programming languages, supports a variety of operators, including:

  • arithmetic operators
  • Relational operators
  • boolean operator
  • String operators
  • file test operator

       Native bash does not support simple mathematical operations, but it can be achieved through other commands, such as awk and expr, expr is the most commonly used. expr is an expression calculation tool that can be used to evaluate expressions. For details, please see:

Linux————Arithmetic commands-CSDN blog

(2) Arithmetic operators

arithmetic operators

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

operator illustrate Example
+ addition `expr $a + $b` results in 70.
- Subtraction `expr $a - $b` results in 40.
* multiplication `expr $a \* $b` results in 1200.
/ division `expr $b / $a` results in 3.
% Take remainder `expr $b % $a` results in 0.
= Assignment a=$b assigns the value of variable b to a.
== equal. Used to compare two numbers and return true if they are the same. [ $a == $b ] returns false.
!= not equal. Used to compare two numbers and return true if they are not the same. [ $a != $b ] returns true.

Note: The conditional expression must be placed between square brackets and there must be spaces, for example: [$a==$b ] is wrong and must be written as [ $a == $b ].

Notice:

Notice:

  • The multiplication sign (*) must be preceded by a backslash (\) to perform multiplication;
  • The expr syntax of shell in MAC is:$((expression)), here the "*" in the expression ; No need for escape symbols "\" 

Show results:

Code:

#!/bin/bash

a=60
b=20

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

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

val=`expr $a \* $b`
echo "a * b : $val"

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

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

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

(3) 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 variable a is 10 and variable b is 20:

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

[ $a -le $b ] returns true.

text:

#!/bin/bash

a=10
b=

if [ "$a" -eq "$b" ]; then
    echo "$a -eq $b : a 等于 b"
else
    echo "$a -eq $b : a 不等于 b"
fi

if [ "$a" -ne "$b" ]; then
    echo "$a -ne $b : a 不等于 b"
else
    echo "$a -ne $b : a 等于 b"
fi

if [ "$a" -gt "$b" ]; then
    echo "$a -gt $b : a 大于 b"
else
    echo "$a -gt $b : a 不大于 b"
fi

if [ "$a" -lt "$b" ]; then
    echo "$a -lt $b : a 小于 b"
else
    echo "$a -lt $b : a 不小于 b"
fi

if [ "$a" -ge "$b" ]; then
    echo "$a -ge $b : a 大于或等于 b"
else
    echo "$a -ge $b : a 小于 b"
fi

if [ "$a" -le "$b" ]; then
    echo "$a -le $b : a 小于或等于 b"
else
    echo "$a -le $b : a 大于 b"
fi
 

 

(4) Boolean operators

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

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

Note: Boolean operators must be placed in [] or used with the test command to be effective.

(5) String operators

The following table lists commonly used string operators, assuming that variable a is "abc" and variable b is "abg":

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

(6) File test operator

File test operators are used to detect various properties of Unix files.

Property detection is described as follows:

Operator illustrate Example
-b file Checks if the file is a block device file and returns true if so. [ -b $file ] returns false.
-c file Checks if the file is a character device file and returns true if so. [ -c $file ] returns false.
-d file Checks if the file is a directory and returns true if so. [ -d $file ] returns false.
-f file Checks if the file is a normal file (neither a directory nor a device file) and returns true if so. [ -f $file ] returns true.
-g file Checks if the file has the SGID bit set and returns true if so. [ -g $file ] returns false.
-k file Check whether the file has the sticky bit set (Sticky Bit), if so, return true. [ -k $file ] returns false.
-p file Checks whether the file is a named pipe, and returns true if so. [ -p $file ] returns false.
-u file Checks if the file has the SUID bit set and returns true if so. [ -u $file ] return false.
-r file Checks if the file is readable and returns true if so. [ -r $file ] returns true.
-w file Checks if the file is writable and returns true if so. [ -w $file ] returns true.
-x file Checks if the file is executable and returns true if so. [ -x $file ] returns true.
-s file Check whether the file is empty (whether the file size is greater than 0), and return true if it is not empty. [ -s $file ] returns true.
-e files Checks whether a file (including a directory) exists and returns true if so. [ -e $file ] returns true.

Other checkers:

  • -S: Determine whether a file is a socket.
  • -L: Check if the file exists and is a symbolic link

#!/bin/bash

file="/var/www/runoob/test.sh"

if [ -r "$file" ]; then
    echo "File readable"
else
    echo "The file is not readable"
fi

if [ -w "$file" ]; then
    echo "File writable"
else
    echo "File cannot be written"
fi

if [ -x "$file" ]; then
    echo "File executable"
else
    echo "The file is not executable"
fi

if [ -f "$file" ]; then
    echo "The file is an ordinary file"
else< a i=3> echo "The file is a special file" fi

if [ -d "$file" ]; then
    echo "The file is a directory"
else
    echo "The file is not a directory"
fi

if [ -s "$file" ]; then
    echo "The file is not empty"
else< a i=3> echo "The file is empty" fi

if [ -e "$file" ]; then
    echo "File exists"
else
    echo "The file does not exist"
fi
 

(7) Logical operators

The following introduces the logical operators of Shell, assuming that variable a is 10 and variable b is 20:

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

 

Guess you like

Origin blog.csdn.net/qq_63976098/article/details/134153235