0% found this document useful (0 votes)
17 views

OS - PracticalAssignment 1 - Operation On Process - Shell Programming

Ooooooooooo

Uploaded by

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

OS - PracticalAssignment 1 - Operation On Process - Shell Programming

Ooooooooooo

Uploaded by

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

/*

Extended Shell.

1. Write a program that behaves like a shell ( Command Interpreter).


It has its own prompt say “MyShell $”. Any normal shell
command is executed from your shell by starting a child process to
execute the system program corresponding to the command.
( Hint : System Calls Used : fork ( ) , execlp ( ) , waitpid ( ) )*/

#include<stdio.h>
#include<unistd.h>
main ()
{
char buff[80],t1[30],t2[30],t3[30],t4[30];
int pid,n;

while (1)
{
printf("MyShell$");
fflush(stdin);
fgets(buff,80,stdin);
n=sscanf(buff,"%s %s %s %s",t1,t2,t3,t4);

switch(n)
{
case 1:
pid=fork();
if(pid==0)
{
execlp(t1,t1,NULL);
}
else
{
wait();
}
break;
case 2:
pid=fork();
if(pid==0)
{
execlp(t1,t1,t2,NULL);
}
else
{
wait();
}
break;
case 3:
pid=fork();
if(pid==0)
{
execlp(t1,t1,t2,t3,NULL);
}
else
{
wait();
}
break;
case 4:
pid=fork();
if(pid==0)
{
execlp(t1,t1,t2,t3,t4,NULL);
}
else
{
wait();
}
}//switch
}//while
}

/*

OUTPUT:

[root@localhost ~]# ./a.out


MyShell$ls
anaconda-ks.cfg Desktop fifo.c~ kjava mycount.c Pictures
Templates Videos
a.out Documents install.log login.java~ myshella1.c Public
tybcsbooklet2
chitra Download install.log.syslog Music ooosss sk.txt untitled
folder
MyShell$vim a.txt
MyShell$ls
anaconda-ks.cfg chitra Download install.log.syslog Music ooosss
sk.txt untitled folder
a.out Desktop fifo.c~ kjava mycount.c Pictures Templates
Videos
a.txt Documents install.log login.java~ myshella1.c Public
tybcsbooklet2
MyShell$cat a.txt
Java
Php
Networking
Operating System
TCS
MyShell$cat sk.txt
operating system
java
php
networking
compilar construction
business applications
MyShell$
[root@localhost ~]#
*/
/*Assign No:1.Extended Shell.
Set A:Program 3.
3. Write a program that behaves like a shell ( Command Interpreter).
It has its own prompt say “MyShell $”. It should interpret the
following command :
a. Count CW < filename > : To print number of Characters &
Words in the file.
b. Count WL < filename > : To print number of Words &
Lines in the file.
c. Count CL < filename > : To print number of Characters &
Lines in the file.
d. Count CWL < filename > : To print number of Characters,
Words & Lines in the file.
( Hint : System Calls Used : fork ( ) , execlp ( ) , waitpid ( ), open (
), close( ), read ( ), write ( ) )*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<dirent.h>
void doublecount(char *ch,char *fn)
{
FILE *fp;
char ch1;
int lcnt=0,wcnt=0,ccnt=0;
fp=fopen(fn,"r");
if(fp==NULL)
{
printf("\ncan't Open");
exit(0);
}
while(!feof(fp))
{
ch1=fgetc(fp);
if(ch1=='\n')
lcnt++,wcnt++;
else if(ch1=='\t'||ch1==' ')
wcnt++;
else ccnt++;
}
if(strcmp(ch,"cw")==0)
printf("\nTotal Character and words are: %d %d ",ccnt,wcnt);
if(strcmp(ch,"wl")==0)
printf("\nTotal words and Line: %d %d ",wcnt,lcnt);
if(strcmp(ch,"cl")==0)
printf("\nTotal Character and Lines are: %d %d ",ccnt,lcnt);
if(strcmp(ch,"cwl")==0)
printf("\nTotal Character words and Lines are: %d %d %d ",ccnt,wcnt,lcnt);

}
main()
{
char s[50],t1[50],t2[50],t3[50],t4[50];
int n;
while(1)
{
fflush(stdin);
printf("\nMYSHELL$:");
fgets(s,80,stdin);
n=sscanf(s,"%s%s%s%s",t1,t2,t3,t4);
switch(n)
{
case 1:
if(strcmp(t1,"exit")==0)
exit(0);
else if(!fork())
{
execlp(t1,t1,NULL);
perror(t1);
}
break;
case 2:
if(!fork())
{
execlp(t1,t2,NULL);
perror(t1);
}
break;
case 3:
if(strcmp(t1,"count")==0)
doublecount(t2,t3);
else if(!fork())
{
execlp(t1,t1,t2,t3,NULL);
perror(t1);
}
break;

}
}
}
/*OUTPUT:
[root@localhost myshellall]# ./a.out

MYSHELL$:count cw type.txt

Total Character and words are: 58 10


MYSHELL$:count wl type.txt

Total words and Line: 10 9


MYSHELL$:count cl type.txt

Total Character and Lines are: 58 9


MYSHELL$:count cwl type.txt

Total Character words and Lines are: 58 10 9


MYSHELL$:exit
*/
/*
Assign No:1.Extended Shell.
Set B:Program 1.
1. Write a program that behaves like a shell ( Command Interpreter).
It has its own prompt say “MyShell $ “. Any normal shell
command is executed from your shell by starting a child process to
execute the system program corresponding to the command. It
should additionally interpret the following command :
a. Typeline +n < filename > : To print first ‘n’ lines of file file.
b. Typeline –n < filename > : To print last ‘n’ lines of the file.
c. Typeline a < filename > : To print all lines of the file.
( Hint : System Calls Used : fork ( ) , execlp ( ) , waitpid ( ), open (
), close( ), read ( ), write ( ), lseek ( ) )*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<dirent.h>
void typeline(char *ch,char *fn)
{
FILE *fp;
char s[50][50],*ptr;
int i=0,cnt=0,n,f=0;
fp=fopen(fn,"r");
if(fp==NULL)
{
printf("\n File can not be open");
exit(0);
}
while(!feof(fp))
{
fgets(s[i],80,fp);

i++;cnt++;
}
strcpy(s[i-1],"NULL");
cnt--;
if(strcmp(ch,"a")==0)
{
for(i=0;i<cnt;i++)
printf("%d: %s",i+1,s[i]);
}
else
{
n=atoi(ch);
if(n>0)
{
if(n>cnt)
{
printf("Enter Valid Line: ");
f=1;
}
if(f==0)
{
for(i=0;i<n;i++)
printf("%d: %s",i+1,s[i]);
}

}
if(n<0)
{
n=n*(-1);
if(n>cnt)
{
printf("Enter Valid Line: ");
f=1;

}
if(f==0)
{
for(i=cnt-1;i>=cnt-n;i--)
printf("%d: %s",i+1,s[i]);

}
}

main()
{
char s[50],t1[50],t2[50],t3[50],t4[50];
int n;
while(1)
{
fflush(stdin);
printf("\nMYSHELL$:");
fgets(s,80,stdin);
n=sscanf(s,"%s%s%s%s",t1,t2,t3,t4);
switch(n)
{
case 1:
if(strcmp(t1,"exit")==0)
exit(0);
else if(!fork())
{
execlp(t1,t1,NULL);
perror(t1);
}
break;
case 2:
if(!fork())
{
execlp(t1,t2,NULL);
perror(t1);
}
break;
case 3:
if(strcmp(t1,"typeline")==0)
typeline(t2,t3);
else if(!fork())
{
execlp(t1,t1,t2,t3,NULL);
perror(t1);
}
break;

}
}
}

/*OUTPUT:
[root@localhost os]# ./a.out

MYSHELL$:typeline +8 type.txt
1: java
2: php
3: operating system
4: business Application
5: Networking
6: Data structure
7: C language
8: Cpp

MYSHELL$:typeline -5 type.txt
12: Tcs
11: Cc
10: Statistics
9: Maths
8: Cpp

MYSHELL$:typeline a type.txt
1: java
2: php
3: operating system
4: business Application
5: Networking
6: Data structure
7: C language
8: Cpp
9: Maths
10: Statistics
11: Cc
12: Tcs

MYSHELL$:exit
*/
/*
Assign No:1.Extended Shell.
Set B:Program 2.
2. Write a program that behaves like a shell ( Command Interpreter).
It has its own prompt say “MyShell $ “. Any normal shell
command is executed from your shell by starting a child process to
execute the system program corresponding to the command. It
should additionally interpret the following command :
a. Search F < pattern > < filename > : To search first
occurance of pattern in the file.
b. Search C < pattern > < filename > : To count number of
occurances of pattern in the file.
c. Search A < pattern > < filename > : To Search number of
occurances of pattern in the file.
( Hint : System Calls Used : fork ( ) , execlp ( ) , waitpid ( ), open (
), close( ), read ( ), write ( ) )*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<dirent.h>
void search(char *ch,char *pat,char *fn)
{
FILE *fp;
char s[80],*ptr;
int i=0,cnt=0;
fp=fopen(fn,"r");
if(fp==NULL)
{
printf("\n File can not be open");
exit(0);
}
if(strcmp(ch,"f")==0)
{
i=0;
while(!feof(fp))
{
fgets(s,80,fp);
i++;
if(strstr(s,pat))
{
printf("%d:%s",i,s);
break;
}
}
}
if(strcmp(ch,"a")==0)
{
i=0;
while(!feof(fp))
{
fgets(s,80,fp);
i++;
if(strstr(s,pat))
{
printf("%d:%s",i,s);
}
}
}

if(strcmp(ch,"c")==0)
{
while(!feof(fp))
{
ptr=NULL;
fgets(s,80,fp);
ptr=strstr(s,pat);
while(ptr!=NULL)
{
cnt++;
ptr++;
ptr=strstr(ptr,pat);
}
}
printf("\nTotal no. of occurance: %d",cnt);
}
}
main()
{
char s[50],t1[50],t2[50],t3[50],t4[50];
int n;
while(1)
{
fflush(stdin);
printf("\nMYSHELL$:");
fgets(s,80,stdin);
n=sscanf(s,"%s%s%s%s",t1,t2,t3,t4);
switch(n)
{
case 1:
if(strcmp(t1,"exit")==0)
exit(0);
else if(!fork())
{
execlp(t1,t1,NULL);
perror(t1);
}
break;
case 2:
if(!fork())
{
execlp(t1,t2,NULL);
perror(t1);
}
break;
case 3:
if(!fork())
{
execlp(t1,t1,t2,t3,NULL);
perror(t1);
}
break;
case 4:
if(strcmp(t1,"search")==0)
search(t2,t3,t4);
else if(!fork())
{
execlp(t1,t1,t3,t4,NULL);
perror(t1);
}
break;

}
}
}
/*OUTPUT:

[root@localhost ]# ./a.out

MYSHELL$:search f va type.txt
1:java

MYSHELL$:search c at type.txt

Total no. of occurance: 5


MYSHELL$:search a st type.txt
3:operating system
6:Data structure
10:Statistics

MYSHELL$:exit
*/

You might also like