EC-231 Operating Systems Lab Report #05: Department of Computer Engineering HITEC University Taxila
EC-231 Operating Systems Lab Report #05: Department of Computer Engineering HITEC University Taxila
Submitted by
Name: Farjad Khan
Reg. No. 19-ce-029
Total 5 5 5 15
Obtained
___________________________
Lab. Instructor
Kaynat Rana
Experiment # 05
Implementation of Conditional Statements and Loops in Linux
● If-else statements
● For loop
● While loop
in LINUX Ubuntu.
Theory: if
simple if is used for decision making in shell script.if the given condition is true then it
will execute the set of code that you have allocated to that block.
Syntax
if [ condition ]
then
Execute the statements
fi
Example
#Check Number is 1
if [ $no -eq 1]
then
echo "Number 1 "
fi
if..else
if..else is used for decision making in shell script where the given condition is true then it
will execute the set of code that you have allocated to that block otherwise you can
execute the rest of code for the false condition.
Syntax
if [ condition ]
then
Execute Statement if Condition is True
elif
Execute Statement if Condition is False
fi
Example
if..elif..else
it is possible to create compound conditional statements by using one or more else
if(elif) clause.if the 1st condition is false,then subsequent elif statements are
checked.when an elif condition is found to be true,the statements following that
associated parts are executed.
Syntax
if [ condition ]
then
Execute Statement if Condition 1
elif [ condition ]
Execute Statement if Condition 2
elif [ condition ]
Execute Statement if Condition 3
elif
Else Condition
fi
Example
Nested if
if statement and else statement can be nested in bash shell programming.the keyword
"fi" indicates the end of the inner if statement and all if statement should end with "fi".
Syntax
if [ condition ]
then
if [ condition ]
then
Execute Statement
elif
Execute Statement
fi
elif
Execute Statement
fi
Example
#Nested if Example
echo "Enter Your Country:"
read cn
if [$cn -eq 'India']
then
echo "Enter Your State:"
read st
if [$st -gt 'Gujarat']
then
echo "Welcome to Gujarat"
elif
echo "You are Not Gujarati"
fi
elif
echo "Other Country"
fi
Case Statement
The case statement is good alternative to multilevel if then else fi statement.it enables
you to match several values against one variable.its easier to read and write multiple
conditions.
Syntax
case $[ variable_name ] in
value1)
Statement 1
;;
value2)
Statement 2
;;
value3)
Statement 3
;;
value4)
Statement 4
;;
valueN)
Statement N
;;
*)
Default Statement
;;
esac
Example
while [ condition ]
do
command1
command2
..
....
commandN
done
Example
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
TASKS
1)Simulate suitable command/commands in gedit to input 2 numbers
from user and display the greater number.
Output:
2. Simulate suitable command/commands in gedit to input 2
numbers from user and make a simple calculator which should
be capable of performing basic calculations i.e. addition,
subtraction, multiplication, division by using if-else conditions.
Display all possible outputs.
Output:
3. Simulate suitable command/commands in gedit to input 2 numbers
from user and make a simple calculator which should be capable of
performing basic calculations i.e. addition, subtraction, multiplication,
division by using switch-case statements. Display all possible outputs.
Output:
4. Simulate suitable command/commands in gedit to print
table of a user defined number.
Output:
5. Simulate suitable command/commands in gedit to check
whether the user defined number is even or odd.
Output:
Conclusion: In this lab we learned the conditional statements and loops
in UNIX operating system i.e. if-else statement, switch statement, for loop
and whereas loop.