Latest web development tutorials

Shell basic operators

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

  • Arithmetic operators
  • Relational Operators
  • Boolean operators
  • String Operators
  • File Test Operators

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

expr is an expression calculation tool, use it to complete the expression evaluator operation.

For example, to add two numbers (note the use of the `anti-quotation marks instead of single quotation marks'):

#!/bin/bash

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

Running instance »

Execute the script, the output is as follows:

两数之和为 : 4

Two things to note:

  • Between expressions and operators must have a space, for example, 2 + 2 is wrong, you must write 2 + 2, which we are familiar with most programming languages ​​are not the same.
  • Complete expression to be `` contained, note that this character is not commonly used in single quotation marks, below the Esc key.

Arithmetic operators

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

Operators Explanation Examples
+ addition `Expr $ a + $ b` result is 30.
- Subtraction `Expr $ a - $ b` result is -10.
* multiplication `Expr $ a \ * $ b` results for 200.
/ division `Expr $ b / $ a` result is 2.
% Remainder `Expr $ b% $ a` result is 0.
= Assignment a = $ b variable b will assign the value of a.
== equal. Used to compare two numbers, same return true. [$ A == $ b] returns false.
! = not equal. Used to compare two numbers are not the same returns true. [$ A! = $ B] returns true.

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

Examples

Arithmetic operators examples are as follows:

#!/bin/bash
# author:本教程
# url:www.w3big.com

a=10
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

Execute the script, the output is as follows:

a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a 不等于 b

note:

  • Multiplication sign (*) must be added in front of a backslash (\) in order to achieve multiplication;
  • if ... then ... fi is conditional statements, the follow-up will be covered.
  • In the MAC shell of expr syntax is: $ ((expression)), where the expression "*" need to escape symbol "\."

Relational Operators

Relational operators only support digital, does not support the string, unless the value of the string is a number.

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

Operators Explanation Examples
-eq Detecting whether two numbers are equal, equal returns true. [$ A -eq $ b] returns false.
-ne Detecting whether two numbers are equal, not equal returns true. [$ A -ne $ b] returns true.
-gt The left is greater than the number detected on the right, and if so, it returns true. [$ A -gt $ b] returns false.
-lt Number detection is less than the right of the left, and if so, it returns true. [$ A -lt $ b] returns true.
-ge Detecting whether the number is equal to the right of the left side of the large, and if so, it returns true. [$ A -ge $ b] returns false.
-le Whether the number of detection of less than or equal to the right to the left, if it is, it returns true. [$ A -le $ b] returns true.

Examples

Relational operators examples are as follows:

#!/bin/bash
# author:本教程
# url:www.w3big.com

a=10
b=20

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

Execute the script, the output is as follows:

10 -eq 20: a 不等于 b
10 -ne 20: a 不等于 b
10 -gt 20: a 不大于 b
10 -lt 20: a 小于 b
10 -ge 20: a 小于 b
10 -le 20: a 小于或等于 b

Boolean operators

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

Operators Explanation Examples
! Non-operation, the expression is true returns false, otherwise it returns true. [! False] returns true.
-o OR operation, there is a true expression returns true. [$ A -lt 20 -o $ b -gt 100] returns true.
-a And operations, the two expressions are true before returning true. [$ A -lt 20 -a $ b -gt 100] returns false.

Examples

Boolean operators examples are as follows:

#!/bin/bash
# author:本教程
# url:www.w3big.com

a=10
b=20

if [ $a != $b ]
then
   echo "$a != $b : a 不等于 b"
else
   echo "$a != $b: a 等于 b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
   echo "$a -lt 100 -a $b -gt 15 : 返回 true"
else
   echo "$a -lt 100 -a $b -gt 15 : 返回 false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : 返回 true"
else
   echo "$a -lt 100 -o $b -gt 100 : 返回 false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : 返回 true"
else
   echo "$a -lt 100 -o $b -gt 100 : 返回 false"
fi

Execute the script, the output is as follows:

10 != 20 : a 不等于 b
10 -lt 100 -a 20 -gt 15 : 返回 true
10 -lt 100 -o 20 -gt 100 : 返回 true
10 -lt 100 -o 20 -gt 100 : 返回 false

Logical Operators

The following describes the logical operator Shell, assuming a 10 variable, the variable b is 20:

Operators Explanation Examples
&& Logical AND [[$ A -lt 100 && $ b -gt 100]] returns false
|| Logical OR [[$ A -lt 100 || $ b -gt 100]] Returns true

Examples

Examples of logical operators are as follows:

#!/bin/bash
# author:本教程
# url:www.w3big.com

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, the output is as follows:

返回 false
返回 true

String Operators

The following table lists commonly used string operators, assumed as a variable "abc", the variable b is "efg":

Operators Explanation Examples
= Detecting two strings are equal, equal returns true. [$ A = $ b] returns false.
! = Detecting whether two strings are equal, not equal returns true. [$ A! = $ B] returns true.
-z Detecting whether the string length is 0 for 0 returns true. [-z $ A] returns false.
-n Detecting whether the string length is 0, non-zero return true. [-n $ A] returns true.
str Detecting whether the string is empty, not empty return true. [$ A] returns true.

Examples

String Operators examples are as follows:

#!/bin/bash
# author:本教程
# url:www.w3big.com

a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a 等于 b"
else
   echo "$a = $b: a 不等于 b"
fi
if [ $a != $b ]
then
   echo "$a != $b : a 不等于 b"
else
   echo "$a != $b: a 等于 b"
fi
if [ -z $a ]
then
   echo "-z $a : 字符串长度为 0"
else
   echo "-z $a : 字符串长度不为 0"
fi
if [ -n $a ]
then
   echo "-n $a : 字符串长度不为 0"
else
   echo "-n $a : 字符串长度为 0"
fi
if [ $a ]
then
   echo "$a : 字符串不为空"
else
   echo "$a : 字符串为空"
fi

Execute the script, the output is as follows:

abc = efg: a 不等于 b
abc != efg : a 不等于 b
-z abc : 字符串长度不为 0
-n abc : 字符串长度不为 0
abc : 字符串不为空

File Test Operators

File test operator is used to detect a variety of Unix file attributes.

Attribute detection described as follows:

Operators Explanation Examples
-b file Detecting whether the file is a block device file, and if so, it returns true. [-b $ File] returns false.
-c file Detecting whether the file is a character device file, and if so, it returns true. [-c $ File] returns false.
-d file Detecting whether the file is a directory, and if so, it returns true. [-d $ File] returns false.
-f file Detecting whether the file is a regular file (neither directory or device file), and if so, it returns true. [-f $ File] returns true.
-g file Detecting whether a file has the SGID bit, and if so, it returns true. [-g $ File] returns false.
-k file Detecting whether a file has the sticky bit (Sticky Bit), and if so, it returns true. [-k $ File] returns false.
-p file Detecting whether the file is a named pipe, and if so, it returns true. [-p $ File] returns false.
-u file Detecting whether a file has the SUID bit, and if so, it returns true. [-u $ File] returns false.
-r file It detects whether a file is readable, and if so, it returns true. [-r $ File] returns true.
-w file Detecting whether the file can be written, and if so, it returns true. [-w $ File] returns true.
-x file Detecting whether the file is executable, and if so, it returns true. [-x $ File] returns true.
-s file Detecting whether the file is empty (the file size is greater than 0), not empty return true. [-s $ File] returns true.
-e file Detect file (including directory) exists, and if so, it returns true. [-e $ File] returns true.

Examples

Variable file indicates the file "/var/www/w3big/test.sh", its size is 100 bytes, with rwx permissions. The following code will detect various attributes of the file:

#!/bin/bash
# author:本教程
# url:www.w3big.com

file="/var/www/w3big/test.sh"
if [ -r $file ]
then
   echo "文件可读"
else
   echo "文件不可读"
fi
if [ -w $file ]
then
   echo "文件可写"
else
   echo "文件不可写"
fi
if [ -x $file ]
then
   echo "文件可执行"
else
   echo "文件不可执行"
fi
if [ -f $file ]
then
   echo "文件为普通文件"
else
   echo "文件为特殊文件"
fi
if [ -d $file ]
then
   echo "文件是个目录"
else
   echo "文件不是个目录"
fi
if [ -s $file ]
then
   echo "文件不为空"
else
   echo "文件为空"
fi
if [ -e $file ]
then
   echo "文件存在"
else
   echo "文件不存在"
fi

Execute the script, the output is as follows:

文件可读
文件可写
文件可执行
文件为普通文件
文件不是个目录
文件不为空
文件存在