Conditional statment
5. CONDITIONAL STATEMENTS :
The if construct is followed by a command. If
an expression is to be tested, it is enclosed in square brackets. The then
keyword is placed after the closing parenthesis. An if must end with a fi.
Syntax :
1.if
This is used to
check a condition and if it satisfies the condition if then does the next
action , if not it goes to the else part.
2.if…else
Syntax :
If cp $ source $
target
Then
Echo File copied
successfully
Else
Echo Failed to
copy the file.
3.nested if
here sequence of
condition are checked and the corresponding
performed
accordingly.
Syntax :
if condition
then
command
if condition
then
command
else
command
fi
fi
4.case ….. esac
This construct helps in execution of the
shell script based on Choice.
EXAMPLE
|
The if construct is: if command then block of statements fi -------------------------------------- if [ expression ] then block of statements fi -------------------------------------- The if/else/else if construct is: if command then block of statements elif command then block of statements elif command then block of statements else block of statements fi ------------------------------ if [ expression ] then block of statements elif [ expression ] then block of statements elif [ expression ] then block of statements else block of statements fi -------------------------------------- |
The case command construct is: case variable_name in pattern1) statements ;; pattern2) statements ;; pattern3) ;; *) default value ;; esac case "$color" in blue) echo $color is blue ;; green) echo $color is green ;; red|orange) echo $color is red or orange ;; *) echo "Not a color" # default esac The if/else construct is: if [ expression ] then block of statements else block of statements fi -------------------------------------- |
Comments
Post a Comment