Saturday, 25 June 2011

Conditional Structures in Shell Scripting - I


The conditional structures like ifelseif, while and for in shell are exactly the same in semantics as the those found in the high level programming languages like Java, C, C++. The difference lies in the syntax.

First lets see the most basic conditional statement - if

Syntax:
             if condition
      then 
      statement1 # if the condition is true or exit status is zero 
fi
Exit status denotes the value of the command returned upon its execution. Exit status is zero in case the command executes successfully else it returns a non zero status.
The condition can be evaluated using test command or [ expr ]
The following table illustrates the conditional operators in Linux shell


For string comparisons


Lets make a shell script to clear things up.



Now using test command



Same program with else statement included



Logical operator example











No comments:

Post a Comment

Popular Posts