Jntu Mca Linux Lab Programs

You might also like

You are on page 1of 23

JNTU MCA LINUX LAB PROGRAMS

1. Displaying all lines between specified line numbers

Aim: Write a shell script that accepts a file name, starting and ending line
numbers as arguments and displays all the lines between the given line
numbers.

Algorithm:
1. Start
2. Get file name as first argument
3. Get starting ending line numbers as second & third arguments
4. Display the lines from starting line number to ending line number in specified
file using ‘sed’ command
5. Stop.

Input:

$cat >file.dat

This is first shell program


For displaying
The lines between
The given numbers

Shell script:

sed -n ''$2,$3'p' $1

Execution:

$sh f1.sh emp.lst 2 4

Output:

For displaying
The lines between
The given numbers

Conclusion: the shell script is successfully executed for displaying specified


lines in a file.
2. Deleting a word from list of files

Aim: Write a shell script that deletes all lines containing a specified word in
one or more files supplied as arguments to it.

Algorithm:
1. Start
2. Get the file names as command line arguments
3. Write “Enter the word to search & delete:”
4. Read word
5. for each argument repeat step6 to step8
6. if it is file then go to next step else display “error in file “
7. search the lines not contain specified ‘word’
8. Write the above result into same file
9. Stop.

Input:
$cat >a.txt
hi
hello
how are you
$cat >b.txt
hello
where are you
bye

Shell script:
$vi f2.sh
echo "Enter word to search and delete:"
read word
while [ $# -gt 0 ]
do
if [ -f $1 ]
then
grep -v $word $1|cat >$1
else
echo “error in file $1”
fi
shift 1
done
Execution:
$sh f2.sh a.txt b.txt
Enter the word to search and delete:hello
output:
$cat a.txt
hi
how are you
$cat b.txt
where are you
bye

Conclusion: the awk script is successfully executed for is deleting specified


word in a list of files.

3. Displaying read,write and executable files

Aim: Write a shell script that displays a list of all the files in the current
directory to which the user has read, write and execute permissions.

Algorithm:
1. Start
2. For each file in current directory repeat step.3 to steps.5
3. Check it is readable
4. Check it is writeable
5. Check it is executable
6. If it satisfies the steps 3,4,5 then display file name else skip the file.
7. Stop.

Input:
$ls –l
-rwxrwxrwx 1 root root 0 Nov 17 17:38 a.txt
-rwxrwx--- 1 root root 0 Nov 17 17:39 b.txt
-r-------- 1 root root 0 Nov 17 17:40 c.txt
drwx------ 1 root root 0 Nov 18 17:46 sub

Shell script:
echo "the read,write,execution files are:"
for file in *
do
if [ -r $file ]
then
if [ -w $file ]
then
if [ -x $file ]
then
echo "$file"
fi
fi
fi
done

Execution:
$sh f3.sh

output:

the read,write,execution files are:


a.txt
b.txt
sub

Conclusion: the shell script is successfully executed for is displaying


read,write,executionfiles in a directory.

4. finding file or directory

Aim: Write a shell script that receives any number of file names as
arguments checks if every argument supplied is a file or a directory and
reports accordingly. Whenever the argument is a file, the number of lines
on it is also reported.

Algorithm:
1. Start
2. Read file names as command line arguments
3. For each argument do step.5 to step.7
4. If given argument is a directory name then display “it is directory.”
5. if given argument is a ordinary file name then display “it is file”
6. if given argument is ordinary file then count number of lines in file
7. Display the count in the screen.
8. Stop.
Input:
$touch l.txt
$cat >m.txt
This
Linux
Shell program.
$mkdir sub

shell script:

$vi f4.sh

while [ $# -gt 0 ]
do
if [ -f $1 ]
then
echo "$1 is file,contains `cat $1|wc –l` lines."
elif [ -d $list ]
then
echo "$1 is directory."
fi
shift 1
done

Execution:
$sh f4.sh l.txt m.txt sub

output:
l.txt is a file contains 0 lines.
m.txt is a file contains 3 lines.
Sub is a directory.

Conclusion: the shell script is successfully executed for displaying file or


directory in list of arguments

5.number of occurrences of each word in


list of files
Aim: Write a shell script that accepts a list of file names as its arguments,
counts and reports the occurrence of each word that is present in the first
argument file on other argument files.

Algorithm:
1. Start
2. Get list of file names from command arguments
3. Get a word from first file
4. If the word is end of file then goto step.8
else go to next step.
5. count the occurrences of ‘word’ in each file.
6. Display the count for each file
7. Go to step.3
8. Stop.

Input:
$cat >list1
mango
banana
pineapple
$cat >list2
mango
banana
$cat >list3
mango
mango
pineapple
Shell script:
$vi f5.sh
file1=$1
shift 1
exec<$file1
while read li
do
echo "$li:"
grep -c "$li" $@
done
Execution:
$sh f5.sh list1 list2 list3
output:
mango:
list2:1
list3:2
banana:
list2:1
list3:0
pineapple:
list2:0
list3:1

Conclusion: the shell script is successfully executed for displaying count of


each word in specified files.

6.Listing directories

Aim: Write a shell script to list all of the directory files in a directory.

Algorithm:
1. Start
2. Write “the directories files in current directory are:”
3. For each file in current directory do the step.4
4. Check it is directory or not ,if it is directory then print file name
5. Stop.

Input:
$touch a.txt b.txt
$mkdir sub1 sub2
$ls
a.txt b.txt sub1 sub2

Shell script:
Vi f6.sh

echo "the directorys are:"


for file in *
do
if [ -d $file ]
then
echo "$file"
fi
done

Execution:
$sh f6.sh

output:

the directorys are:


sub1
sub2

Conclusion: the shell script is successfully executed for displaying


directories in directory

7. Factorial of a number

Aim: Write a shell script to find factorial of a given integer.

Algorithm:
1. Start
2. Write “enter the number:”
3. Read n
4. Fact ← 1
5. i ← 1
6. if i is less then or equals to n go to next step else go to step.9
7. f ← f * i
8. Go to step.6 by incrementing i by 1.
9. write “the factorial of n is :“
10. Display‘fact’ value
11. Stop.

Shell script:

$vi f7.sh

echo "enter the number:"


read num
fact=1
while [ $num -gt 1 ]
do
f=`expr $fact \* $num`;
num=$((num - 1));
done
echo "the factorial of $num is:$fact"

Execution:

$sh f7.sh

output:

enter the number:


5
The factorial of 5 is:120

Conclusion: the shell script is successfully executed for finding factorial of


a given number.

8. Number of lines without vowels

Aim: Write an awk script to count the number of lines in a file that do not
contain vowels.

Algorithm:
1. Start
2. Get the source file name
3. Write “ the number of lines not contain vowels are :”
4. k ←0
5. For each row in the source file repeat the step.5 to step.7 until end of the file
6. Check the row contain ‘a’ or ‘e’ or ‘i’ or ’o’ or ’u’
7. If the row not contain above characters then increment the c value by 1.
8. Print the ‘k’ value
9. Stop.

Input:
$cat >list.txt
Cat bat rat
Fly
sky
round the world

Awk script:
$vi f8.awk

BEGIN{
printf "the no of lines not contain vowels are:"
}!/[aA]|[eE]|[Ii]|[Oo]|[Uu]/{
k++
}
END{
printf "%d",k
}

Execution:
$awk –f f8.awk list.txt

Output:
The no of lines not contain vowels are:2

Conclusion: the awk script is successfully executed for displaying lines not
containing vowels.

9. counting characters,words,lines

Aim: Write an awk script to find the number of characters, words and lines
in a file.

Algorithm:
1. Start
2. Get the source file name
3. Set field separator as “ “ (space)
4. Declare c←0 ,w←0,l←0
5. For each row in source file repeat the step.5 to step.7 until end of file.
6. c ← c + length of row
7. w ← w + number of fields in row
8. increment the l by 1
9. print “characters:”, c value
10 .print “word:”, w value
11.Print “lines:”, l value
12.Stop.

Input:
cat>list2.txt
Hi how
Are
You
What are you
Doing bye

Awk script:

$vi f9.awk

BEGIN{
FS= " "
}{
c=c+length()
w=w+NF
l++
}
END{
Printf"characters:%d\nwords:%d\nlines%d:",c,w,l}

Execution:

$awk -f f9.awk list2.txt

Output:
Characters:29
Words:9
Line:5

Conclusion: the awk script is successfully executed for counting


lines,words,and characters in a file.

10.Copying file using system calls


And standard I/O

Aim: Write a c program that makes a copy of a file using standard I/O and
system calls.
Algorithm:
1. Start
2. Declare src[10],dest[10],ch[2] as strings
3. display “enter the src file name:”
4. Read src
5. display “enter the destination file name:”
6. Read dest
7. Open ‘src’ file in read mode
8. Open ‘dest’ file in write” or create ‘dest’ file.
9. display “src contains:”
10.Get the character from src file and
store it in ch
11.If it is end of file go to step.14 else
go to next step
12.Write ch into dest file
13.Display the ch in the screen.
14.Display ‘file is copied”
15.Stop.

using standard I/O:


C source code:
$vi f10.c

#include<stdio.h>
#include<stdlib.h>
int main()
{
char src[10],dest[10],ch;
FILE *fp1,*fp2;
printf("enter the source file name:");
scanf("%s",src);
if((fp1=fopen(src,"r"))==0)
{
printf("error in opening src file");
exit(0);
}
printf("enter the destination file name:");
scanf("%s",dest);
if((fp2=fopen(dest,"w"))==0)
{
printf("error in destination file");
exit(1);
}
printf("%s contains:\n",src);
while((ch=getc(fp1))!=EOF)
{
fputc(ch,fp2);
printf("%c",ch);
}
printf("file is copied.");
fclose(fp1);
fclose(fp2);
return 1;
}

Input:

$cat >abc.txt

Java is a simple, object oriented


And multithreaded.

Execution:
$gcc f10.c –o f10
$./f10

Output:
enter the source file name:abc.txt
enter the destination file name:abccpy.txt
abc.txt contains:

Java is a simple, object oriented


And multithreaded.

file is copied.

Verification:
$cat abccpy.txt

Java is a simple, object oriented


And multithreaded.
$
using system call:
C source code:
$vi f10b.c

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
char src[10],dest[10],ch[2];
int fd1,fd2,c;
printf("enter the source file name:");
scanf("%s",src);
if((fd1=open(src,O_RDONLY))==-1)
{
printf("error in opening src file");
exit(0);
}
printf("enter the destination file:");
scanf("%s",dest);
if((fd2=open(dest,O_CREAT|O_WRONLY,0700))==-1)
{
printf("error in destination file");
exit(0);
}
printf("%s contains:\n",src);
while(read(fd1,ch,1)==1)
{
write(1,ch,1);
write(fd2,ch,1);
}
printf("file is copied");
close(fd1);
close(fd2);
return 1;
}
Input:

$cat >def.txt
cpp is a object oriented
language.

Execution:
$gcc f10b.c –o f10b
$./f10b

Output:
enter the source file name:def.txt
enter the destination file name:defcpy.txt
def.txt contains:

cpp is a object oriented


language.

file is copied.

Verification:
$cat defcpy.txt

cpp is a object oriented


language.

Conclusion: a c program is successfully executed for copying file


using system calls and standard I/O.

16.CREATING CHILD PROCESS


Aim: To write a c program create a child process and allow the parent to
display “parent” And child to display “child”.

Algorithm:
1. start
2. Declare pid as pid_t data type.
3.call fork() and pid<---return value of fork()
4. if pid <o then display “child process creation error”
5.if pid==0 then display “child”
6. if pid>0 then display “parent”
7. stop.

Source code:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid;
pid=fork();
if(pid<0)
{
perror("error in fork:");
}
if(pid==0)
{
printf("\nchild process..");
}
if(pid>0)
{
printf("\nparent process..");
}
}
Execution:
$gcc child.c -o child
$./child
Child
parent
Conclusion:
A c program is successfully executed to display child process as “child” parent
process as “parent”.

17.Zombie Process

Aim: To write a c program to create a zombie process.


Algorithm:
1.start
2.delcare pid as pid_t data type.
3.call fork function and pid<-- return value of fork()
4.if pid >0 dispaly “parent process..” display “pid”
5.sleep parent process
6. stop.

C source code:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
if(fork()>0)
{
printf("parent with %d",getpid());
sleep(10);
}
}
Execution:
$gcc zombie.c –o zombie
$./ zombie &
$ps Z
LABEL PID TTY STAT TIME COMMAND
user_u:system_r:unconfined_t 3769 pts/1 Ss 0:00 bash
user_u:system_r:unconfined_t 3799 pts/1 S 0:00 ./zom
user_u:system_r:unconfined_t 3800 pts/1 Z 0:00 [zom] <defunct>
user_u:system_r:unconfined_t 3801 pts/1 R+ 0:00 ps Z

$ parent with 3799

Conclusion:
A c programme is successfully executed to create zombie process.

18.Orphan Process
Aim: To write a c program to illustrates a orphan process.
Algorithm:
1. Start
2. Delcare pid as pid_t data type.
3. call fork() and pid<- -return value of fork()
4. if pid==0 then display “child process..”
5. display parent process id and child process id
6. call sleep() to delay the child process.
7. if pid==1 then display “parent process”
8. terminate parent the process.
9. terminate the child after the sleep function.
10. Stop.

Source code:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("child process...");
printf("process id=%d,parentid=%d",getpid(),getppid());
sleep(15);
printf("termination of child");
}
if(pid>0)
{
printf("parent process:%d",getpid());
printf("termination of parent.");
}
}
Execution:

$gcc orphan.c –o orphan


$./orphan
Parent process:3395
Termination of parent
Child process..
Child id:3396 parent id:3395
Termination of child...

Conclusion: the creation of orphan process is successfully executed.

11(A).IMPLEMENTING ‘CAT’ COMMAND

Aim: To implement a c program to ‘cat’ unix command using system call.

Algorithm:
1.start
2.read the file name as first argument.
3.read the character form the given file.
4.display the character in the screen.
5.read the next character from file
6.if it is not end of file the goto step 4 else goto next step
7.stop.

Source code:
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char **argv)
{
char ch[2];
int fd;
fd=open(argv[1],O_RDONLY);

while((read(fd,ch,1))==1)
{
write(1,ch,1);
}
close(fd);
return 1;
}

Execution:
$gcc display.c -o display
$ ./display a.txt
Eno Ename job salary dob
101 john sales 10000 12/10/89
103 pavan sales 2000 15/11/89
Conclusion: a c program is successfully executed for ‘cat’ unix command
using system call.

11(B).IMPLEMETING ‘LS’ COMMAND


Aim: To implement a c program to ‘ls’ unix command using system call.
Algorithm:
1.start
2.declare directory pointer dp;
3.declare an instance of structure dirent as dirp
4.declare p as string variable.
5.if number of arguments==1 then p,<- ’.’else p<-second argument
6.open directory p
7.read the file name
8.display the “file name”
9.read next file name
10.if is not null then goto step 7 else goto next step.
11.stop
Source code:
#include<stdio.h>
#include<string.h>
#include<dirent.h>
#include<stdlib.h>
#include<sys/types.h>
int main(int argc ,char **argv)
{
DIR *dp;
struct dirent *dirp;
char p[8];
if(argc==1)
strcpy(p,".");
else
{
strcpy(p,argv[1]);
}
if((dp=opendir(p))==NULL)
{
printf("can't open directoey");
exit(0);
}
while((dirp=readdir(dp))!=NULL)
{
printf("%s\n",dirp->d_name);
}
closedir(dp);
return 1;
}
Execution:
$gcc dirlist.c –o dirlist
$./dirlist
.
a.txt
b.txt
sub
..
Conclusion: the ls command is successfully implemented in c.

14.Listing Inode,File Name


Aim:TO write a c programe to list for every file in a directory,its inode number
and file name.

Algorithm:
1.start
2.declare directory pointer dp;
3.declare an instance of structure dirent as dirp
4.declare struct stat instance as buf;
5.write “file_name,inode “
6.open the directory
7.read the file name
8.extract file information into buf
9.display the “file name” and it’s inode number
Using buf varable
10.read next file name
11.if is not null then goto step 7 else goto next step.
12.stop

C source code:
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<dirent.h>
int main(int argc,char **argv)
{
DIR *dp;
struct stat buf;
ino_t ino;
struct dirent *dirp;
if(argc!=2)
{
printf("usage:dirname");
exit(1);
}
if((dp=opendir(argv[1]))==NULL)
perror("open:");
printf("\nfile_name inode");
printf("\n----------------");
while((dirp=readdir(dp))!=NULL)
{
stat(dirp->d_name,&buf);
ino=buf.st_ino;
printf("\n %ld %s",ino,dirp->d_name);
}
return 1;
}
Execution:
$gcc ilist.c –o ilist
$./ilist sub1
file_name inode
----------------
8781826 ..
8781821 sub3
8781827 sub2
8781823 d.tct
8781935 a.txt
8781932 .

Conclusion:a c program is successfully executed for list file name,its inode


number.

You might also like