You are on page 1of 5

EC-231 Operating Systems

Experiment# 04
Implementation of Conditional Statements and Loops in Linux

Objective
The objectives of this lab are to familiarize students with shell programming and practice few
problems related to
 If-else statements
 For loop
 While loop

in LINUX Ubuntu.

Software Tools
 Ubuntu 16.04
 GCC Complier

Description:
This lab has been designed to get even better insight of the shell programming. By the end of this
lab you‘ll be able to solve more complex problems in Linux using shell.

The details of few programming constructs to be used in the tasks are as follows:-

Normal
Arithmetical/
Mathematical Mathematical
Operator in Shell Meaning Statements But in Shell

For test
statemen For [ expr ]
t with if statement
comman with if
Script d command

-eq is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ]

-ne is not equal 5 != 6 if test 5 -ne 6 if [ 5 -ne 6 ]

Department of Computer Engineering


HITEC University Taxila
1
EC-231 Operating Systems
to

-lt is less than 5< 6 if test 5 -lt 6 if [ 5 -lt 6 ]

-le is less than 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ]

or equal to

-gt is greater 5> 6 if test 5 -gt 6 if [ 5 -gt 6 ]

than

-ge is greater 5 >= 6 if test 5 -ge 6 if [ 5 -ge 6 ]

than or equal
to

if statements :-
if condition then
commands
fi
Example
Following script determine whether given argument number is positive.
if test $1 -gt 0 then
echo "$1 number is positive" fi
Run it as follows:
$ chmod 755
ispostive $./ ispostive
5
5 number is positive

$./ispostive -45
Nothing is printed
if else statements:-
ifcondition ;then
commands
else
commands
fi

Example
Following script determine whether given argument number is positive.
$ cat>ispostive
#

Department of Computer Engineering


HITEC University Taxila
2
EC-231 Operating Systems
#
# Script to see whether argument is positive
#
if test $1 -gt 0 then
echo "$1 number is positive"
elsif test $1 –lt 0
then
echo “$1 number is negative”
else
echo “number is zero”
fi

Run it as follows:
$ chmod 755
ispostive $./ ispostive
5
5 number is positive

$./ispostive -45
Number is negative

Case…esac statement in shell:


These are the statements in shell which replicates the switch case statements in c/c++ language.
Syntax is as follows:-

#!/bin/sh

echo "Enter a number between 1 and 10. "


read NUM

case $NUM in
1) echo "one";;
2) echo "two";;
3) echo "three";;
4) echo "four";;
5) echo "five";;
6) echo "six";;
7) echo "seven";;
8) echo "eight";;
9) echo "nine";;
10) echo "ten";;
*) echo "INVALID NUMBER!";;
esac

The 1, 2, 3 are the values that could possibly be contained in $NUM. The * character at the end
indicates how you’ll handle any other case just like “Default case” in switch case scenario of
C/C++ language.

Department of Computer Engineering


HITEC University Taxila
3
EC-231 Operating Systems

While loop:-

while [ condition ]
do
command1
command2
..
....
commandN
done

Example

#!/bin/sh

a=0

while [ $a -lt 10 ]

do

echo $a

a=`expr $a + 1`

done

Lab Tasks

1. Simulate suitable command/commands in gedit to input 2 numbers from user and display
the greater number.
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.
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.
Department of Computer Engineering
HITEC University Taxila
4
EC-231 Operating Systems

4. Simulate suitable command/commands in gedit to print table of a user defined number.


5. Simulate suitable command/commands in gedit to check whether the user defined
number is even or odd.

Conclusion:

__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
_________________________________

_________________

Lecturer

Iram Abdullah

Department of Computer Engineering


HITEC University Taxila
5

You might also like