0% found this document useful (0 votes)
12 views15 pages

OS Lab - Record

The document outlines a laboratory exercise on shell programming and the implementation of UNIX commands in C. It includes algorithms and code for various tasks such as summing numbers, swapping values, finding the greatest of three numbers, calculating factorials, generating Fibonacci series, identifying prime numbers, creating a menu-driven calculator, and verifying Armstrong numbers. Additionally, it details the implementation of UNIX commands like copy, move, grep, and listing directory contents.

Uploaded by

tanishmurugesan2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

OS Lab - Record

The document outlines a laboratory exercise on shell programming and the implementation of UNIX commands in C. It includes algorithms and code for various tasks such as summing numbers, swapping values, finding the greatest of three numbers, calculating factorials, generating Fibonacci series, identifying prime numbers, creating a menu-driven calculator, and verifying Armstrong numbers. Additionally, it details the implementation of UNIX commands like copy, move, grep, and listing directory contents.

Uploaded by

tanishmurugesan2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

CS22411-OPERATING SYSTEMS LABORATORY

DATE:
EX.NO: 2b
STUDY OF SHELL PROGRAMMING

AIM:

To study shell programming and implement several programs.

(i) Sum of two numbers:

Algorithm:

1. Start
2. Read two number from user
3. Add the given two numbers.
4. Print the result
5. Stop
Code:

echo "Enter the first number: "


read a
echo "Enter the second number: "
read b
c=$(($a+$b))
echo "Sum of $a and $b is $c"
Sample input and output:

[cs23c158@CS22411 OSlab]$ vi sum.sh


[cs23c158@CS22411 OSlab]$ sh sum.sh
Enter the first number:
20
Enter the second number:
30
Sum of 20 and 30 is 50

ROLL NO:2127230501158 Page |


(ii) Swapping of two numbers:
Algorithm:
1. Start
2. Read two numbers from the user
3. Swap numbers using temporary variable
4. Print the result
5. Stop
Code:
echo "Enter first number: "
read fr
echo "Enter second number: "
read sec
echo "Before swap"
echo "first=$fr"
echo "second=$sec"
temp=$fr
fr=$sec
sec=$temp
echo "After swap"
echo "first=$fr"
echo "second=$sec"
Sample input and output:
[cs23c158@CS22411 OSlab]$ vi swap.sh
[cs23c158@CS22411 OSlab]$ sh swap.sh
Enter first number:
10
Enter second number:
20
Before swap
first=10
second=20

ROLL NO:2127230501158 Page |


After swap
first=20
second=10

(iii) Greatest of three numbers:


Algorithm:
1. Start
2. Read three numbers from the user
3. Compare the first two numbers and then the third number
4. Find the greatest number
5. Print the result
6. Stop
Code:
echo "Enter the first number: "
read a
echo "Enter the second number: "
read b
echo "Enter the third number: "
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "$a is the greatest"
elif [ $b -gt $a ] && [ $b -gt $c ]
then
echo "$b is the greatest"
else
echo "$c is the greatest"
fi

ROLL NO:2127230501158 Page |


Sample input and output:
[cs23c158@CS22411 OSlab]$ vi greatest.sh
[cs23c158@CS22411 OSlab]$ sh greatest.sh
Enter the first number:
10
Enter the second number:
30
Enter the third number:
20
30 is the greatest

(iv) Factorial of a number:


Algorithm:
1. Start
2. Read a number from the user.
3. Calculate the factorial using the loop concept by multiplying and reducing the number.
4. Print the result
5. Stop
Code:
echo "Enter the number whose factorial has to be computed: "
read num
fact=1
while [ $num -gt 1 ]
do
fact=$(($fact*$num))
num=$(($num-1))
done
echo "The factorial of $num is: $fact"

ROLL NO:2127230501158 Page |


Sample input and output:
[cs23c158@CS22411 OSlab]$ vi factorial.sh
[cs23c158@CS22411 OSlab]$ sh factorial.sh
Enter the number whose factorial has to be computed:
5
The factorial of 5 is: 120

(v) Fibonacci Series:


Algorithm:
1. Start
2. Read a number from the user.
3. Print 0 and 1 as starting numbers.
4. Add numbers and print result till the number is reduced.
5. Stop
Code:
echo "Enter the number: "
read n
a=0
b=1
echo "The fibonacci series is: "
echo "$a"
echo "$b"
while [ $n -ne 2 ]
do
c=$(($a+$b))
echo "$c"
a=$b
b=$c
n=$((n-1))
Done

ROLL NO:2127230501158 Page |


Sample input and output:
[cs23c158@CS22411 OSlab]$ vi fibonacci.sh
[cs23c158@CS22411 OSlab]$ sh fibonacci.sh
Enter the number:
7
The fibonacci series is:
0
1
1
2
3
5
8

(vi) Generation of Prime numbers:


Algorithm:
1. Start
2. Read a number from the user.
3. Check whether the given number is divisible by only 1 and itself.
4. If true, print “It is a prime number”.
5. Else, print “It is not a prime number”.
6. Stop.
Code:
echo "Enter the range: "
read n
echo "The prime numbers are: "
m=2
while [ $m -le $n ]
do
i=2
flag=0

ROLL NO:2127230501158 Page |


while [ $i -le $(($m/2)) ]
do
if [ $(($m%$i)) -eq 0 ]
then
flag=1
break
fi
i=$(($i+1))
done
if [ $flag -eq 0 ]
then
echo "$m"
fi
m=$(($m+1))
done
Sample input and output:
[cs23c158@CS22411 OSlab]$ vi Prime.sh
[cs23c158@CS22411 OSlab]$ sh Prime.sh
Enter the range:
15
The prime numbers are:
2
3
5
7
11
13

ROLL NO:2127230501158 Page |


(vii) Menu-Driven Calculator:
Algorithm:
1. Start
2. Read a numbers from the user.
3. Select the operation to be done using the numbers.
4. Do the specified operation
5. Print the result.
6. Stop.
Code:
sum=0
i="y"
echo "Enter the first number: "
read a
echo "Enter the second number: "
read b
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "Enter your choice: "
read ch
case $ch in
1)sum=$(($a+$b))
echo "Sum: " $sum
;;
2)sub=$(($a-$b))
echo "Subtraction: " $sub
;;
3)product=$(($a*$b))
echo "Product: " $product
;;

ROLL NO:2127230501158 Page |


4)division=$(($a/$b))
echo "Quotient: " $division
;;
esac
Sample input and output:
[cs23c158@CS22411 OSlab]$ vi mdcalulator.sh
[cs23c158@CS22411 OSlab]$ sh mdcalulator.sh
Enter the first number:
8
Enter the second number:
9
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice:
2
Subtraction: -1

(viii) Verification of Armstrong Number:


Algorithm:
1. Start
2. Read a number from the user.
3. Separate the digits and calculate the powers.
4. Sum the powers.
5. Print the result.
6. Stop.

ROLL NO:2127230501158 Page |


Code:
echo "Enter a number: "
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=$(( $x % 10))
n=$(( $r *$r *$r))
sum=$(( $sum+$n))
x=$(( $x/10))
done
if [ $sum -eq $c ]
then
echo "$sum is an armstrong number"
else
echo "$c is not armstrong number"
fi
Sample input and output:
[cs23c158@CS22411 OSlab]$ vi Armstrong.sh
[cs23c158@CS22411 OSlab]$ sh Armstrong.sh
Enter a number:
153
153 is an armstrong number

RESULT:
The study of shell programming was conducted and the programs were
successfully executed .

ROLL NO:2127230501158 Page |


CS22411-OPERATING SYSTEMS LABORATORY
DATE:
EX.NO: 3

IMPLEMENTATION OF UNIX COMMANDS

AIM:

To implement unix commands such as copy, move, grep and ls in the C langauge.

(i) Copy:
Code:
#include <stdio.h>
#include<stdlib.h>
void main ()
{
FILE *fp1,*fp2;
fp1=fopen(“file1”,”r”);
fp2=fopen(“file5”,”w”);
char s[100];
fgets(s,100,fp1);
while(!feof (fp1))
{
fputs(s,fp2);
fgets(s,100,fp1);
}
fclose(fp1);
fclose(fp2);
}
Sample output:
file 1 file 5
cat1 cat1
cat2 cat2
dog1 dog1
ROLL NO:2127230501158 Page |
(ii) Move
Code:
#include <stdio.h>
#include<stdlib.h>
void main ()
{
FILE *fp1,*fp2;
fp1=fopen(“file1”,”r”);
fp2=fopen(“file5”,”w”);
char s[100];
fgets(s,100,fp1);
while(!feof (fp1))
{
fputs(s,fp2);
fgets(s,100,fp1);
}
remove(“file1”);
fclose(fp1);
fclose(fp2);
}
Sample output:
file 1 file 5
<empty file> cat1
cat2
dog1

ROLL NO:2127230501158 Page |


(iii) Grep:
Code:
#include <stdio.h>
#include<stdlib.h>
void main ()
{
FILE *fp1;
fp1=fopen(“file1”,”r”);
char s[100];
char pattern[100];
int c=1;
printf(“Enter the pattern”);
scanf(“%s”,&pattern);
fgets(s,100,fp1);
while(!feof (fp1))
{
{
if(strstr(s,pattern)
{
printf(“%s”,s);
printf(“ is at line %d”,c);
}
fgets(s,100,fp1);
c++;
}
fclose(fp1);
}
Sample output:
Enter the pattern:dog
dog1 is at line 6

ROLL NO:2127230501158 Page |


(iv) Program

#include<stdio.h>
#include<dirent.h>
void main()
{
DIR *D;
struct dirent *r;
d=opendir(“Sample”);
r=readdir(d);
while (r!=NULL)
{
printf(“%s”,r→d_name);
r=readdir(d);
}
}

OUTPUT
f3 ..f2.f1

RESULT:
Thus we have implemented unix commands such as copy, move, grep and ls in the C
langauge.

ROLL NO:2127230501158 Page |


ROLL NO:2127230501158 Page |

You might also like