OS Lab Manual 06 07
OS Lab Manual 06 07
OS Lab Manual 06 07
SHELL PROGRAMS
EXPERIMENT 1
REQUIREMENTS :
THEORY :
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument1]
The shell executes the program CommandName and passes argument on the command line to
it. Then the shell waits for the command to exit.
This program is for swapping two numbers using only two variables
ALGORITHM :
PROGRAM:
OUTPUT :
EXPERIMENT 2
REQUIREMENTS :
THEORY :
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument1]
The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.
This program is for swapping two numbers using three variables
ALGORITHM:
PROGRAM
OUTPUT :
EXPERIMENT 3
AIM : To read a number from the user and to calculate square and cube of the number.
REQUIREMENTS :
THEORY :
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument1]
The shell executes the program CommandName and passes argument on the command line to
it. Then the shell waits fro the command to exit.
This program is for reading a number from the user and to calculate square and cube of the
number.
ALGORITHM :
PROGRAM:
OUTPUT:
EXPERIMENT 4
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line to
it. Then the shell waits fro the command to exit.
ALGORITHM :
PROGRAM:
clear
echo enter the number
read n
r=1
r=`expr $n % 2`
if [ $r -eq 0 ]
then
echo even
else
echo odd
fi
OUTPUT:
[krishna-edu-balu]$ sh eorodd
enter the number
5
odd
[krishna-edu balu]$
EXPERIMENT 5
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit. Files are the passive
containers of data on disk. Many operating systems name hardware devices in the same way
as files and allow access to devices using the same interface as to files: open, read, write,
close.
ALGORITHM:
PROGRAM
clear
echo enter a filename
read $fname
if test –f $fname
then
echo file is a ordinary file
else
echo directory
fi
OUTPUT:
[krishna-edu balu]$ sh fd
enter a filename
string
file is a ordinary file
[krishna-edu balu]$
EXPERIMENT 6
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.
This program is to check the type of input character using case statement.
ALGORITHM:
PROGRAM:
clear
echo enter a character from the keyboard
read ch
case $ch in
OUTPUT:
EXPERIMENT 7
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.
ALGORITHM:
PROGRAM
clear
echo 1.display a file
echo 2.copy a file
echo 3.create a directory
echo enter ur choice
read ch
case $ch in
1) echo enter filename
read fname
cat $fname
;;
2) echo enter two files
read file1 file2
cp $file1 $file2
;;
3) echo enter the directory
read dname
mkdir $dname
;;
*) echo “ invalid option “
esac
OUTPUT:
EXPERIMENT 8
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it(via the argv array of strings). Then the shell waits for the command to
exit.
ALGORITHM:
PROGRAM
clear
echo "enter a string1"
read first
echo "enter a string2"
read second
if [ $first = $second ]
then
echo equal
else
echo unequal
fi
OUTPUT:
EXPERIMENT 9
AIM : To compare two strings by reading strings from the command line .
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit
This program is for comparing two strings by reading strings from the commandline
ALGORITHM:
PROGRAM:
if [ $1 = $2 ]
then
echo "Strings are equal ...."
else
echo "Strings are not equal....."
fi
EXPERIMENT 10
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.
ALGORITHM:
PROGRAM
OUTPUT
EXPERIMENT 11
AIM : To calculate the value of X n
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.
ALGORITHM:
STEP 1. Read the values of x and n
STEP 2. Initialize sum and I to 1
STEP 3. Check for the condition [ $I -le $n ]
STEP 4.Repeat the following steps until the above condition becomes false
Sum=`expr $sum \* $x`
i=`expr $i + 1
STEP 5. Echo the value of sum as the value of x power n
PROGRAM:
OUTPUT:
EXPERIMENT 12
AIM : To read 10 numbers from the user and to find the sum and
average of the numbers .
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line to
it.
This is the program for reading 10 numbers from the user and finding the sum and average of
the numbers
ALGORITHM:
PROGRAM:
n=1
sum=0
while [ $n -le 10 ]
do
echo -n "enter number $n"
read num
sum=`expr $sum + $num`
num=0;
n=`expr $n + 1`
done
avg=`expr $sum / 10`
echo "ther sum of 10 numbers is $sum"
echo "the average of 10 numbers is $avg"
OUTPUT :
EXPERIMENT 13
AIM : To accept a number from the user and display the list of even numbers
below that number
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line to
it.
This is the program for accepting numbers from the user and display the list of even numbers
below that number
ALGORITHM:
PROGRAM:
echo -n "enter the limit:"
x=2
read num
while [ $x -lt $num ]
do
echo -n "$x \t"
x=`expr $x + 2`
done
OUTPUT :
EXPERIMENT 14
AIM : To find the count and sum of even and odd numbers separately
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line to
it.
This is the program for finding the count and sum of even and odd numbers separately
ALGORITHM
PROGRAM:
i=`expr $i + 1`
OUTPUT :
EXPERIMENT 15
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line to
it.
ALGORITHM:
PROGRAM:
echo -n "Enter an interge:"
read num
digit=0
while [ $num -gt 0 ]
do
a=`expr $num % 10`
digit=`expr $digit + 1`
num=`expr $num / 10`
done
echo "the number of digits in the integer is $digit"
OUTPUT:
[balu@krishna-edu shell]$ sh count.sh
Enter an interge:12345
the number of digits in the integer is 5
[balu@krishna-edu shell]$
EXPERIMENT 16
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line to
it.
ALGORITHM:
PROGRAM:
OUTPUT :
EXPERIMENT 17
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line to
it.
This is the program for printing the multiplication table of the given number
ALOGRITHM :
PROGRAM:
OUTPUT :
EXPERIMENT 18
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line to
it.
ALGORITHM:
PROGRAM :
OUTPUT :
EXPERIMENT 19
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.
ALGORITHM:
PROGRAM
clear
echo enter the number
read n
res=1
i=1
while [ $i -le $n ]
do
res=`expr $res \* $i`
i=`expr $i + 1`
done
echo factorial=$res
OUTPUT:
EXPERIMENT 20
REQUIREMENTS :
THEORY :
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the command line to
it. Then the shell waits for the command to exit.
ALGORITHM:
PROGRAM:
clear
echo enter the no. of numbers in the series
read n
a=0
b=1
d=2
echo $a
echo $b
while [ $d -lt $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
d=`expr $d + 1`
done
OUTPUT:
EXPERIMENT 21
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.
ALGORITHM:
PROGRAM :
clear
echo enter a number
read n
rev=0
while [ $n -gt 0 ]
do
dig=`expr $n % 10`
rev=`expr $rev \* 10 + $dig`
n=`expr $n / 10`
done
echo $rev
OUTPUT:
EXPERIMENT 22
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit. This program is to find the
reverse of a number.
ALGORITHM:
PROGRAM
OUTPUT:
EXPERIMENT 23
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.
ALGORITHM:
PROGRAM:
clear
echo enter a number
read n
i=2
flag=1
m=1
j=`expr $n / 2`
while [ $i -le $j ]
do
m=`expr $n % $i`
if [ $m -eq 0 ]
then
flag=0
break
fi
i=`expr $i +1`
done
if [ $flag -eq 1 ]
then
echo $n is prime
else
echo $n is not a prime
fi
OUTPUT:
EXPERIMENT 24
AIM: To read numbers from the user from command line and to display
its sum using until statement.
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit
This program is for reading a number from the command line and to display its sum using
until statement.
ALGORITHM:
PROGRAM :
if [ $# -eq 0 ]
then
echo " Invalid numbers"
fi
sum=0
i=1
n=$#
until [ $# -eq 0 ]
do
sum=`expr $sum + $i`
shift
done
if [ $n -ne 0 ]
then
echo " The sum of $n numbers is $sum"
fi
OUTPUT :
EXPERIMENT 25
AIM:To read n numbers from the user from the command line and to display its
sum using while statement
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit
This program is for reading numbers from the user from the command line
and to display its sum using while statement.
ALGORITHM:
STEP 1. If the count of the number of command line arguments is 0 then Echo as Invalid
Numbers
STEP 2. Initialize the values of sum to 0 and n to $#
STEP 3. While $# is not equal to zero repeat the following steps
sum=`expr $sum + $i`
Increment the value of $#
STEP 4. If n is not equal to 0 then echo the value of sum
PROGRAM:
if [ $# -eq 0 ]
then
echo "Invalid numbers"
fi
sum=0
i=1
n=$#
while [ $# -ne 0 ]
do
sum=`expr $sum + $1`
shift
done
if [ $n -ne 0 ]
then
echo "The sum of $n numbers is $sum"
fi
OUTPUT :
EXPERIMENT 26
AIM :-To read a numbers from the user from the command line and to display its
sum using for statement
REQUIREMENTS:
THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit
This program is for reading a number from the user from the command line and to display its
sum using for statement
ALGORITHM :
PROGRAM:
if [ $# -eq 0 ]
then
echo " Invalid numbers"
fi
sum=0
n=$#
for i in $*
do
sum=`expr $sum + $i`
shift
done
if [ $n -ne 0 ]
then
echo "The sum of $n numbers is $sum"
fi
OUTPUT :
EXPERIMENT 27
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.
ALGORITHM:
STEP 1.Read the value of the string
STEP 2. Find the length of the string
STEP 3. Initialize the value of a to 1
STEP 4.Repeat the following statements until the condition a<len becomes false
Cut the first character of the string and echo rev
Increment the value of a
Store the new string in temp
STEP 6.Echo the copied string
PROGRAM:
len=`echo $str | wc -c `
a=1
while [ $a -lt $len ]
do
rev=`echo $str | cut -c$a `
echo $rev
a=`expr $a + 1`
temp=`echo $temp$rev`
done
echo " The copied string is $temp"
EXPERIMENT 28
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.
ALGORITHM:
STEP 1. Read the value of string
STEP 2. Find the length of the string
STEP 3.Initialize the value of a
STEP 4. Repeat the following steps until the condition length > 0 becomes false
rev=`echo $str | cut -c $len`
len=`expr $len - 1`
temp=`echo $temp$rev`
STEP 5.Echo the reversed string
PROGRAM :
echo -n "Enter the string "
read str
len=`echo $str | wc -c`
while [ $len -gt 0 ]
do
rev=`echo $str | cut -c $len`
len=`expr $len - 1`
temp=`echo $temp$rev`
done
EXPERIMENT 29
AIM : To convert lowercase letters to uppercase .
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.
ALGORITHM:
PROGRAM:
OUTPUT:
EXPERIMENT 30
AIM : To count the number of vowels in the string
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.
ALGORITHM:
PROGRAM:
OUTPUT:
EXPERIMENT 31
REQUIREMENTS :
THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]
The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.
ALGORITHM:
PROGRAM:
OUTPUT: