Introduction
There are various operators supported by each shell. Our tutorial is based on default shell (Bourne) so we are going to cover all the important Bourne Shell operators in the tutorial. There are following operators which we are going to discuss
The Bourne shell didn't originally have any mechanism to perform simple arithmetic but it uses external programs, either awk or the must simpler program expr. Here is simple example to add two numbers:
- #!/bin/sh
- val=`expr 2 + 2`
- echo "Total value : $val"
Arithmetic Operators
There are following arithmetic operators supported by Bourne Shell. Assume variable a holds 10 and variable b holds 20 then:
It is very important to note here that all the conditional expressions would be put inside square braces with one spaces around them, for example [ $a == $b ] is correct where as [$a==$b] is incorrect. All the arithmetical calculations are done using long integers.
Relational Operators
Bourne Shell supports following relational operators which are specific to numeric values. These operators would not work for string values unless their value is numeric. For example, following operators would work to check a relation between 10 and 20 as well as in between "10" and "20" but not in between "ten" and "twenty". Assume variable a holds 10 and variable b holds 20 then:
Boolean Operators
There are following boolean operators supported by Bourne Shell. Assume variable a holds 10 and variable b holds 20 then:
String Operators
There are following string operators supported by Bourne Shell. Assume variable a holds "abc" and variable b holds "efg" then:
File Test Operators
There are following operators to test various properties associated with a Unix file. Assume a variable file holds an existing file name "test" whose size is 100 bytes and has read, write and execute permission on:
Supplement
* VBird - Linux basic - 第十二章、學習 Shell Scripts
沒有留言:
張貼留言