Shell script from entry to complex five (basic operators)

The shell supports a variety of operators, including:

arithmetic operators
relational operator
boolean operator
string operator
file test operator


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

expr is an expression evaluation tool that can be used to evaluate expressions.

For example, to add two numbers (note the use of backticks ` instead of single quotes'):


# vi test.sh


#!/bin/bash

sum=`expr 1 + 3`

echo "Sum of two numbers: $sum"


output:

# sh test.sh

Sum of two numbers: 4


Notice:

There should be spaces between expressions and operators, for example 2+2 is wrong, it must be written as 2 + 2, which is different from most programming languages ​​we are familiar with.

The complete expression should be enclosed by ` `, note that this character is not the usual single quote, below the Esc key.


arithmetic operators

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

operator illustrate Example
+ addition `expr $a + $b` results in 30
- subtraction `expr $a - $b` results in -10
* multiplication  `expr $a \* $b` results in 200
/ division `expr $b / $a` results in 2
% remainder `expr $b % $a` results in 0
= assign a=$b will assign the value of variable b to a
== equal. Used to compare two numbers, return true if they are the same [ $a == $b ] returns false
!= equal. Used to compare two numbers, return true if they are not the same [ $a != $b ] returns true

                       

Notice:

Conditional expressions should be placed between square brackets and spaces, for example: [$a==$b] is wrong and must be written as [ $a == $b ].

The multiplication sign (*) must be preceded by a backslash (\) to perform multiplication.


relational operator

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


operator illustrate Example
-eq Check if two numbers are equal, return true if they are equal [ $a -eq $b ] returns false
-born Check if two numbers are not equal, return true if they are not equal [ $a -ne $b ] returns true
-gt Check if the number on the left is greater than the number on the right, if so, return true [ $a -gt $b ] returns false
-lt Check if the number on the left is less than the number on the right, if so, return true [ $a -lt $b ] returns true
-give Check if 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
-the Check if 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



boolean operator

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

operator illustrateExample
NOT operation, returns false if the expression is true, otherwise returns true.[ ! false ] returns true
-O  OR operation, returns true if one of the expressions is true.[ $a -lt 20 -o $b -gt 100 ] returns true
-a AND operation returns true when both expressions are true.[ $a -lt 20 -a $b -gt 100 ] returns false


Logical Operators

The following describes Shell's logical operators, assuming that variable a is 10 and variable b is 20:


operator     descriptionExample

&& logical AND[[ $a -lt 100 && $b -gt 100 ]] 返回 false

||       logical OR[[ $a -lt 100 || $b -gt 100 ]] returns true


string operator

The following table lists common string operators, assuming that variable a is "abc" and variable b is "efg":

operator illustrateExample
Checks if two strings are equal, returns true for equality.[ $a = $b ] returns false
!= Checks whether two strings are equal, and returns true if they are not equal.[ $a != $b ] returns true
-with Checks whether the string length is 0, and returns true if it is 0.[ -z $a ] returns false
-n  Check if the string length is 0, if not return true.[ -n $a ] returns true
str Check if the string is empty, return true if it is not empty.[ $a ] returns true

 

file test operator

File test operators are used to test various attributes of Unix files.

Attribute detection is described as follows:

operator illustrate Example
-b file Check if the file is a block device file, if so, return true [ -b $file ] returns false
-c file   Check if the file is a character device file, if so, return true [ -c $file ] returns false
-d file Check if the file is a directory, if so, return true [ -d $file ] returns false
-f file  Check if the file is a normal file (neither a directory nor a device file), if so, return true [ -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 if the file has the sticky bit set, and if so, return true [ -k $file ] returns false
-p file Check if the file is a named pipe, if so, return true [ -p $file ] returns false
-in the file Checks if the file has the SUID bit set, and returns true if so [-u $ file] Return false
-r file Check if the file is readable, if so, return true [ -r $file ] returns true
-w file Check if the file is writable, if so, return true [ -w $file ] returns true
-x file Check if the file is executable, if it is, return true [ -x $file ] returns true
-s file Check if the file is empty (whether the file size is greater than 0), return true if it is not empty [ -s $file ] returns true
-e file Check if file (including directory) exists, if so, return true [ -e $file ] returns true

Guess you like

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