|
|
The conditional structures like if, elseif, 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