Search Suggest

Shell - Basic Operators


1 Toán tử số học (Arithmetic Operators)
Giả sử có hai biến có giá trị như sau:
 a=10  
b=20


Operator Description Example
+ Addition - Adds values on either side of the operator `expr $a + $b` will give 30
- Subtraction - Subtracts right hand operand from left hand operand `expr $a - $b` will give -10
* Multiplication - Multiplies values on either side of the operator `expr $a \* $b` will give 200
/ Division - Divides left hand operand by right hand operand `expr $b / $a` will give 2
% Modulus - Divides left hand operand by right hand operand and returns remainder `expr $b % $a` will give 0
= Assignment - Assign right operand in left operand a=$b would assign value of b into a
== Equality - Compares two numbers, if both are same then returns true. [ $a == $b ] would return false.
!= Not Equality - Compares two numbers, if both are different then returns true. [ $a != $b ] would return true.

 #!/bin/sh  
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 is equal to b"
fi

if [ $a != $b ]
then
echo "a is not equal to b"
fi

 $ ./test.sh  
a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a is not equal to b


2 Toán tử quan hệ (Relational
 Operators)
Giả sử có hai biến có các giá trị như sau:
 a=10  
b=20


Operator Description Example
-eq Checks if the value of two operands are equal or not, if yes then condition becomes true. [ $a -eq $b ] is not true.
-ne Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. [ $a -ne $b ] is true.
-gt Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. [ $a -gt $b ] is not true.
-lt Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. [ $a -lt $b ] is true.
-ge Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. [ $a -ge $b ] is not true.
-le Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. [ $a -le $b ] is true.

Ex:
 #!/bin/sh  
a=10
b=20

if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi

if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi

if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi

if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi

if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b"
else
echo "$a -ge $b: a is not greater or equal to b"
fi

if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi


 $ ./test.sh   
10 -eq 20: a is not equal to b
10 -ne 20: a is not equal to b
10 -gt 20: a is not greater than b
10 -lt 20: a is less than b
10 -ge 20: a is not greater or equal to b
10 -le 20: a is less or equal to b


3 Toán tử  logic (Boolean Operators)
Giả sử có hai biến có các giá trị như sau:
 a=10  
b=20


Operator Description Example
! This is logical negation. This inverts a true condition into false and vice versa. [ ! false ] is true.
-o This is logical OR. If one of the operands is true then condition would be true. [ $a -lt 20 -o $b -gt 100 ] is true.
-a This is logical AND. If both the operands are true then condition would be true otherwise it would be false. [ $a -lt 20 -a $b -gt 100 ] is false.

Ex:
 #!/bin/sh  

a=10
b=20

if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi

if [ $a -lt 100 -a $b -gt 15 ]
then
echo "$a -lt 100 -a $b -gt 15 : returns true"
else
echo "$a -lt 100 -a $b -gt 15 : returns false"
fi

if [ $a -lt 100 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

if [ $a -lt 5 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

 $./test.sh  
10 != 20 : a is not equal to b
10 -lt 100 -a 20 -gt 15 : returns true
10 -lt 100 -o 20 -gt 100 : returns true
10 -lt 5 -o 20 -gt 100 : returns false

4 Toán tử chuỗi (String Operators)
Toán tử chuỗi dùng để so sánh hai chuỗi ký tự.

Giả sử có hai biến chứa các chuối như sau:
 a="abc"  
b="efg"


Operator Description Example
= Checks if the value of two operands are equal or not, if yes then condition becomes true. [ $a = $b ] is not true.
!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. [ $a != $b ] is true.
-z Checks if the given string operand size is zero. If it is zero length then it returns true. [ -z $a ] is not true.
-n Checks if the given string operand size is non-zero. If it is non-zero length then it returns true. [ -z $a ] is not false.
str Check if str is not the empty string. If it is empty then it returns false. [ $a ] is not false.

Ex:
 #!/bin/sh  
a="abc"
b="efg"

if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
fi

if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi

if [ -z $a ]
then
echo "-z $a : string length is zero"
else
echo "-z $a : string length is not zero"
fi

if [ -n $a ]
then
echo "-n $a : string length is not zero"
else
echo "-n $a : string length is zero"
fi

if [ $a ]
then
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi


 $ ./test.sh   
abc = efg: a is not equal to b
abc != efg : a is not equal to b
-z abc : string length is not zero
-n abc : string length is not zero
abc : string is not empty


5 Toán tử tệp (File Operators)
Toán tử tệp dùng để kiểm tra các thuộc tính của tệp.


Operator Description Example
-b file Checks if file is a block special file if yes then condition becomes true.
-c file Checks if file is a character special file if yes then condition becomes true.
-d file Check if file is a directory if yes then condition becomes true.
-f file Check if file is an ordinary file as opposed to a directory or special file if yes then condition becomes true.
-g file Checks if file has its set group ID (SGID) bit set if yes then condition becomes true.
-k file Checks if file has its sticky bit set if yes then condition becomes true.
-p file Checks if file is a named pipe if yes then condition becomes true.
-t file Checks if file descriptor is open and associated with a terminal if yes then condition becomes true.
-u file Checks if file has its set user id (SUID) bit set if yes then condition becomes true.
-r file Checks if file is readable if yes then condition becomes true.
-w file Check if file is writable if yes then condition becomes true.
-x file Check if file is execute if yes then condition becomes true.
-s file Check if file has size greater than 0 if yes then condition becomes true.
-e file Check if file exists. Is true even if file is a directory but exists.

Ex:
 #!/bin/sh  

file="/usr/bin/sh"

if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi

if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi

if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi

if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi

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

if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi

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


 $ ./test.sh   
File has read access
File does not have write permission
File has execute permission
File is an ordinary file
This is not a directory
File size is zero
File exists



Đăng nhận xét