2016年3月6日 星期日

[Linux 文章收集] Unix Shell Programming - Shell Basic Operators

Source From Here 
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 
* Arithmetic Operators.
* Relational Operators.
* Boolean Operators.
* String Operators.
* File Test Operators.

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: 
  1. #!/bin/sh  
  2.   
  3. val=`expr 2 + 2`  
  4. echo "Total value : $val"  
This would produce following result: 
Total value : 4



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

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...