Wednesday 8 January 2014

Shell – evaluation




Evaluation is widely used in SHELL programing. There are three types of evaluation:

  • File attribution: evaluate if the file is a block device, existing , writable…
  • String compare: if the strings are equalled.
  • Value testing: if the two values are equal, greater than, less than.

Here we will talk about the evaluation in detail.

File attribution evaluation:

-b
(block)testing if the file is exisitng and a block device
-c
(char)testing if the file is exisitng and a char device
-r
testing if the file is exisitng and read only
-w
testing if the file is exisitng and writable
-x
testing if the file is exisitng and execuatable
-d
testing if the file is a directory
-f
testing if the file is a normal file
-e
testing if the file is existing

Some samples are here:


Value testing:

n1 -eq n2
true if n1 equals n2
n1 -ne n2
true if n1 not equals n2
n1 -gt n2
true if n1 is greater than n2
n1 -lt n2
true if n1 is less than n2
n1 -ge n2
true if n1 is greater than or equal to n2

Some samples are here:

String compare:

s1 = s2
if the two strings are identical
s1 != s2
if the two strings are not identical
s1
if the string is existing

Some samples are here:

Also BASH have

  • [evaluation1] -a [evaluation 2] : AND connection
  • [evaluation1] -o [evaluation 2] : OR connection
  • !  [evaluation 2] : NOT operator.




No comments:

Post a Comment