0% found this document useful (0 votes)
100 views23 pages

JNTU MCA Linux Lab Exercises

The document provides details on 10 Linux lab programs. Each program is described with its aim, algorithm, input, code sample, execution, and output. The programs cover tasks like displaying lines between numbers in a file, deleting words from multiple files, listing readable/writable files, checking if arguments are files/directories, counting word occurrences, and more.

Uploaded by

backiyalakshmi
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)
100 views23 pages

JNTU MCA Linux Lab Exercises

The document provides details on 10 Linux lab programs. Each program is described with its aim, algorithm, input, code sample, execution, and output. The programs cover tasks like displaying lines between numbers in a file, deleting words from multiple files, listing readable/writable files, checking if arguments are files/directories, counting word occurrences, and more.

Uploaded by

backiyalakshmi
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

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 >[Link]

This is first shell program


For displaying
The lines between
The given numbers

Shell script:

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

Execution:

$sh [Link] [Link] 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 >[Link]
hi
hello
how are you
$cat >[Link]
hello
where are you
bye

Shell script:
$vi [Link]
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 [Link] [Link] [Link]
Enter the word to search and delete:hello
output:
$cat [Link]
hi
how are you
$cat [Link]
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 [Link]
-rwxrwx--- 1 root root 0 Nov 17 17:39 [Link]
-r-------- 1 root root 0 Nov 17 17:40 [Link]
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 [Link]

output:

the read,write,execution files are:


[Link]
[Link]
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 [Link]
$cat >[Link]
This
Linux
Shell program.
$mkdir sub

shell script:

$vi [Link]

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 [Link] [Link] [Link] sub

output:
[Link] is a file contains 0 lines.
[Link] 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

[Link] 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 [Link]
file1=$1
shift 1
exec<$file1
while read li
do
echo "$li:"
grep -c "$li" $@
done
Execution:
$sh [Link] 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.

[Link] 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 [Link] [Link]
$mkdir sub1 sub2
$ls
[Link] [Link] sub1 sub2

Shell script:
Vi [Link]

echo "the directorys are:"


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

Execution:
$sh [Link]

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 [Link]

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 [Link]

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 >[Link]
Cat bat rat
Fly
sky
round the world

Awk script:
$vi [Link]

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

Execution:
$awk –f [Link] [Link]

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
[Link] “lines:”, l value
[Link].

Input:
cat>[Link]
Hi how
Are
You
What are you
Doing bye

Awk script:

$vi [Link]

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

Execution:

$awk -f [Link] [Link]

Output:
Characters:29
Words:9
Line:5

Conclusion: the awk script is successfully executed for counting


lines,words,and characters in a file.

[Link] 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:”
[Link] the character from src file and
store it in ch
[Link] it is end of file go to step.14 else
go to next step
[Link] ch into dest file
[Link] the ch in the screen.
[Link] ‘file is copied”
[Link].

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 >[Link]

Java is a simple, object oriented


And multithreaded.

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

Output:
enter the source file name:[Link]
enter the destination file name:[Link]
[Link] contains:

Java is a simple, object oriented


And multithreaded.

file is copied.

Verification:
$cat [Link]

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 >[Link]
cpp is a object oriented
language.

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

Output:
enter the source file name:[Link]
enter the destination file name:[Link]
[Link] contains:

cpp is a object oriented


language.

file is copied.

Verification:
$cat [Link]

cpp is a object oriented


language.

Conclusion: a c program is successfully executed for copying file


using system calls and standard I/O.

[Link] 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.
[Link] fork() and pid<---return value of fork()
4. if pid <o then display “child process creation error”
[Link] 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”.

[Link] Process

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


Algorithm:
[Link]
[Link] pid as pid_t data type.
[Link] fork function and pid<-- return value of fork()
[Link] pid >0 dispaly “parent process..” display “pid”
[Link] 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.

[Link] 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:
[Link]
[Link] the file name as first argument.
[Link] the character form the given file.
[Link] the character in the screen.
[Link] the next character from file
[Link] it is not end of file the goto step 4 else goto next step
[Link].

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 [Link]
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:
[Link]
[Link] directory pointer dp;
[Link] an instance of structure dirent as dirp
[Link] p as string variable.
[Link] number of arguments==1 then p,<- ’.’else p<-second argument
[Link] directory p
[Link] the file name
[Link] the “file name”
[Link] next file name
[Link] is not null then goto step 7 else goto next step.
[Link]
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
.
[Link]
[Link]
sub
..
Conclusion: the ls command is successfully implemented in c.

[Link] Inode,File Name


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

Algorithm:
[Link]
[Link] directory pointer dp;
[Link] an instance of structure dirent as dirp
[Link] struct stat instance as buf;
[Link] “file_name,inode “
[Link] the directory
[Link] the file name
[Link] file information into buf
[Link] the “file name” and it’s inode number
Using buf varable
[Link] next file name
[Link] is not null then goto step 7 else goto next step.
[Link]

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 [Link]
8781935 [Link]
8781932 .

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


number.

You might also like