LINUX PROGRAMMING LAB MANUAL Computer Science and Engineering
LINUX PROGRAMMING LAB MANUAL Computer Science and Engineering
Mission
To impart Quality Technical Education and to undertake Research and Development with a focus
on application and innovation which offers an appropriate solution to the emerging societal needs
by making the students globally competitive, morally valuable and socially responsible citizens.
Vision
To emerge as a center of excellence with global reputation with adaption of rapid advancements in
the field of computer specialization.
Mission
1. To provide a strong theoretical and practical background in area of computer science with an
emphasize on software development.
3. To educate students to become effective problem solvers, apply knowledge with social
sensitivity for the betterment of the society and humanity as a whole.
Programme educational objectives are broad statements that describe the career and professional
accomplishments that the programme is preparing graduates to achieve within 3 to 5 years after
graduation.
Ø PEO1: To apply the knowledge of mathematics, basic science and engineering solving the
real world computing problems to succeed higher education and professional careers.
Ø PEO2: To develop the skills required to comprehend, analyze, design and create innovative
computing products and solutions for real life problems.
Ø PEO3: To inculcate professional and ethical attitude, communication and teamwork skills,
multi-disciplinary approach and an ability to relate computer engineering issues with social
awareness.
LABORATORY OUTCOMES:
CO [1] To demonstrate the basic knowledge of Linux commands and file handling utilities by
using Linux shell environment.
CO [2] To evaluate the concept of shell scripting programs by using an AWK and SED
commands.
CO[3]To create the directory, how to change and remove the directory.
CO [4] To analyze the process of how the parent and child relationships
CO [5] To define IPC mechanism.
CO [6] To understand the concept of client-server communication by using sockets.
Do’s
1. Come with completed observation and record
3. Know the location of the fire extinguisher and the first aid box and how to use them in case of an
emergency.
4. Read and understand how to carry out an activity thoroughly before coming to the laboratory.
5. Report any broken plugs or exposed electrical wires to your lecturer/laboratory technician
immediately.
6. Write in time, out time and system details in the login register.
Don’ts
1. Do not eat or drink in the laboratory.
2. Do not operate mobile phones in the lab. Keep mobile phones either in silent or switched off mode.
4. Do not disturb your neighbouring students. They may be busy in completing tasks.
7. Do not misbehave.
Objectives:
Intel based desktop PC with minimum of 166 MHZ or faster processor with at least
64 MB RAM and 100MB free disk space.
Fedora OS
Page -5
Computer Science and Engineering Linux Programming
Page -6
Computer Science and Engineering Linux Programming
AIM: 1.Write a Shell script that accepts a filename, starting and ending line numbers as
arguments and displays all the lines between the given line numbers?
THEORY:
Sed: sed means stream editor it can be used for editing the main program by using sed
Page -7
Computer Science and Engineering Linux Programming
2.Write a Shell script that deletes all lines containing a specified word in one or more files
supplied as arguments to it?
VIVA QUESTIONS
Page -8
Computer Science and Engineering Linux Programming
AIM: 3.Write a Shell script that displays list of all the files in the current directory to which
the user has read, write and execute permissions.?
THEORY:
Cd: The cd command can be used for to change the directory that means we can change the
existing directory.
Wc: The wc command can be used for to count the number of words on given data by using wc
command options we can count the number of lines on a file (wc-l).
Page -9
Computer Science and Engineering Linux Programming
fi
Page -10
Computer Science and Engineering Linux Programming
4. 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?
for x in $*
do
if [ -f $x ]
then
echo " $x is a file "
echo " no of lines in the file are "
wc -l $x
elif [ -d $x ]
then
echo " $x is a directory "
else
echo " enter valid filename or directory name "
fi
done
VIVA QUESTIONS:
1. what is an directory?
2. which command is used to create directory?
3.what is the purpose of wc cmd?
4.which command is used to change the directory
Page -11
Computer Science and Engineering Linux Programming
AIM: 5. 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.?
THEORY:
Tr: The Tr command can be used for to translate the characters that means the given string is
replaced with the replacement string.
Syntax: Tr [original string] [replacement string]
Grep: The Grep command can be used for to search the regular pattern on given file.
Syntax: Grep [ pattern ] file name
if [ $# -eq 0 ]
then
echo "no arguments"
else
tr " " "
" < $1 > temp
shift
for i in $*
do
tr " " "
" < $i > temp1
y=`wc -l < temp`
j=1
while [ $j -le $y ]
do
x=`head -n $j temp | tail -1`
c=`grep -c "$x" temp1`
Page -12
Computer Science and Engineering Linux Programming
echo $x $c
j=`expr $j 1`
done
done
fi
Page -13
Computer Science and Engineering Linux Programming
# !/bin/bash
echo "enter directory name"
read dir
if[ -d $dir]
then
echo "list of files in the directory"
ls –l $dir|egrep „^d‟
else
echo "enter proper directory name"
fi
Page -14
Computer Science and Engineering Linux Programming
# !/bin/bash
echo "enter a number"
read num
fact=1
while [ $num -ge 1 ]
do
fact=`expr $fact\* $num`
num=‟expr $num – 1‟
done
echo "factorial of $n is $fact"
VIVA QUESTIONS
1.why we are using grep command?
2.what is an shell script?
3.why we are using egrep command?
4.what is the purpose of fgrep?
5.write a syntax of grep?
6.what is an shell variable?
Page -15
Computer Science and Engineering Linux Programming
AIM: 8. Write a awk script to find the number of characters, words and lines in a file?
THEORY:
AWK: The awk is similar to sed it can be used for to edit the file.
NF: The NF can be used to count the number of fields on a records.
NR: The NR can be used to count the number of records on a file.
Mv:The mv command is used to move the data from one file to another file.
Syntax: mv file1 file2
Page -16
Computer Science and Engineering Linux Programming
9. Write a C Program that makes a copy of a file using standard I/O and system calls?
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
void typefile (char *filename)
{
int fd, nread;
char buf[1024];
fd = open (filename, O_RDONLY);
if (fd == -1) {
perror (filename);
return;
}
while ((nread = read (fd, buf, sizeof (buf))) > 0)
write (1, buf, nread);
close (fd);
}
int
main (int argc, char **argv)
{
int argno;
for (argno = 1; argno < argc; argno )
typefile (argv[argno]);
exit (0);
}
Page -17
Computer Science and Engineering Linux Programming
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[3] )
{
int fd,i;
char buf[2];
fd=open(argv[1],O_RDONLY,0777);
if(fd==-argc)
{
printf("file open error");
}
else
{
while((i=read(fd,buf,1))>0)
{
printf("%c",buf[0]);
}
close(fd);
}
}
Page -18
Computer Science and Engineering Linux Programming
Page -19
Computer Science and Engineering Linux Programming
Page -20
Computer Science and Engineering Linux Programming
Page -21