Foss Lab Manual
Foss Lab Manual
(MASTER Manual)
Signature of
Session-1:
a)Log into the system
b)Use vi editor to create a file called myfile.txt which contains
some text.
c)Correct typing errors during creation
d)Save the file
e)Logout of the system
Session-2:
a)Log into the system
b)Open the file created in session 1
c)Add some text
d)Delete some text
e)Save the changes
g)Logout of the system
Pg
no
s.no
PG
NO
1)
a)Login to the system
b)Use the appropriate command to determine your login shell
c)Use the /etc/passwd file to verify the result of step b
d)Use the who command and redirected yhe result to afile myfile1
use more ommand to see the contents of myfile1
e)Use the data and who command in sequence(in one line) such
that the output of date will display on the screen and the output of
who will be redirected to afile called myfile2.Use the more
command to check the contents of myfile2
2)
a)Write a sed command that deletes the first character in each line
in a file
b)Write a sed command that deletes the character before the last
character in each line in a file
c)Write a sed command that swaps the first and second words in
each line in a file
a)Pipe your /etc/passwd file to awk, and print out the home
directory of each user
b)Develop an interactive grep script that asksfor a word and a file
name and then tells how many linescontain the word
c)Repeat
d)Part using awk
s.no
Name of the
Experiment
Pg no
65
a)a)Write
Write aashell
a file name starting
and and
enling
shellscript
script that
that accepts
takes acommand-line
argument
reports
onwhether
it is directory,a
file,orallsomething
line
number
as arguments
and displays
the lines else
between the
given
b)Write
lineanumbers.
shell script that accepts one or more file name as
arguments
and script
converts
of them
uppercase,provided
they
b)Write
a shell
thatalldeletes
alltolines
containing a specified
word
existsininone
theorcurrent
more files
directory
supplied as arguments to it.
s.no
Name of the
Experiment
Pg no
b)Develop an interactive
script that ask for a word
and a file name and then
tells howmany times that
word ouccred in the file.
10
s.no
13
Name of the
Experiment
Pg no
Do
the following
shell
programs
also
iii)Read,write
and
execute
permissions
iv)Time of last access
(note:use
calls) a particular user has logged in or
1. Write
a shellstat/fstat
script to system
check whether
not. If he has logged in, also check whether he has eligibility to receive
a message or not.
11
12
Both readable
& writable
Write a C d.
program
that simulates
ls command
(use system calls/director
3. Write a shell script which will display the username and terminal name
number of argument.
9. Write a shell script to accept student number, name, marks in 5
subjects. Find total, average and grade. Display the result of student
and store in a file called stu.dat.
Rules: avg>=80 then grade A
Avg<80&&Avg>=70 then grade B
Avg<70&&Avg>=60 then grade C
Avg<60&&Avg>=50 then grade D
WEEK -1
SESSION-1
a)
sol) login
b)
use vi editor to create a file called myfile.txt which contains some text
sol) vi myfile
c)
WEEK-1
SESSION-2
Session-2
a) Log into the system
sol:login
b)
sol: vi myfile.txt
c)
f)
sol: logout
WEEK-2
a)
sol: login
b) use the cat command to create a file containing following data.call it my table use
tabs to seperate the fields
1425 Ravi
15.65
36.17
1450 Raju
21.86
15.65
c)
6830 Sita
36.15
1450 Raju
21.86
15.65
36.15
1450 Raju
21.86
d)
15.65
1450 Raju
21.86
36.15
15.65
1450 Raju
21.86
36.15
use the cut and paste command to swap the fields 2 and 3 of mytable
Ravi
26.27
Ramu
36.15
Sita
21.86
Raju
h)
1425
15.65
Ravi
4320
26.27
Ramu
6830
36.15
Sita
1450
21.86
Raju
i)
Sowjanya
502
Sirisha 513
80
Viswani 506
85
Niharika
504
85
80
sol: logout
WEEK-3
SESSION-1
SESSION-2
WEEK-4
a)Pipe your /etc/passwd file to awk, and print out the home directory of each user.
Sol: cat /etc/passwd | awk { print $7}
b)Develop an interactive grep script that asks for a word and a file name and then tells
how many lines
contain that word.
Sol:
echo Enter a word
read word
echo Enter the filename
read file
nol=grep -c $word $file
echo $nol times $word present in the $file
c)Part using awk
Sol:
echo Enter a word
read word
echo Enter the filename
read file
nol=awk /$word/ { print NR } Infile
echo $nol times $word present in the $file
Week5(a)
AIM:Write a shell script that takes a command line argument and reports on whether it is
Week-5(b)
Aim: Write a shell script that accepts one or more file name as arguments and converts
all of them to uppercase, provided they exist in the current directory.
Program:
# get filename
echo -n "Enter File Name : " read fileName
#make sure file exits for reading
if [ ! -f $fileName ]
then
echo "Filename $fileName does not exists" exit 1
fi
#convert uppercase to lowercase using tr command
tr '[A-Z]' '[a-z]' < $fileName
OUTPUT:
[13W61A0506@localhost~] sh week5a.sh
enter file name
file1 file2
[13W61A0506@localhost~] filename
FILE1 FILE2
Week-5(c)
Aim:-Write a shell script that determines the period for which a specified user is working on the
system.
WEEK-6(A)
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.
Program:If [ $# -ne 3 ] then
WEEK-6(B)
Aim:-Write a shell script that deletes all lines containing a specified word in one or more
files supplied as arguments to it.
Program:if [ $# -lt 1] then
OUTPUT:
[13W61A0506@localhos 2cse]$ sed- fdcl.sed sm.dat sm1.dat
here using unix
WEEK-7(A)
Aim:-Write a shell script that computes the gross salary of a employee according to the
following rules:
i)If basic salary is < 1500 then HRA =10% of the basic and DA =90% of the basic.
ii)If basic salary is >=1500 then HRA =Rs500 and DA=98% of the basic
The basic salary is entered interactively through the key board.
Program:echo enter basic salary
read sal
a=0.1
b=0.8
echo $a
echo "hra is"
hra=`echo 0.1 \* $sal|bc`
echo da is
da=`echo 0.8\*$sal|bc`
gsal=expr $hra + $da + $sal
echo $gsal
Output:-
WEEK-7(B)
Aim:-Write a shell script that accepts two integers as its arguments and computers the value
of first number raised to the power of the second number.
Program:If [ $# -ne 2 ]
then
echo chech the number of arguments
count=1
result=1
if [ $2 ge 0 ]
then
while [ $count le $2 ]
do
result=`expr $result \* $1`
count=`expr $count + 1`
done
fi
fi
output:-
WEEK-8(A)
Aim:a)Write an interactive file-handling shell program. Let it offer the user the choice of
copying, removing, renaming, or linking files. Once the user has made a choice, have the
program ask the user for the necessary information, such as the file name, new name and
so on.
b)Write shell script that takes a login name as command line argument and reports
when that person logs in
Sol:
#Shell script that takes loginname as command line arg and reports when that person logs
in.
if [ $# -lt 1 ]
then
echo improper usage
echo correct usage is: $0 username
exit
fi
logname=$1
while true
do
who|grep "$logname">/dev/null
if [ $? = 0 ]
then
echo $logname has logged in
echo "$logname">>sh01log.txt
date >>sh01log.txt
echo "Hi" > mesg.txt
echo "$logname" >> mesg.txt
echo "Have a Good Day" >> mesg.txt
mail "$logname" < mesg.txt
exit
else
sleep 60 fi done
WEEK8(C)
Aim:-Write a shell script which receives two file names as arguments. It should check
whether the two file contents are same or not. If they are same then second file should be
deleted.
Program:echo enter first file name
read file1
echo enter second file name
read file2
cmp file1 file2 > file3
if [ -z $file1 ] rm file2
fi
echo duplicate file deleted successfully
output
WEEK-9
WEEK-10
Aim:Write a C program that takes one or more file or directory names as command line input
and reports the following information on the file:
i)File type
ii)Number of links
iii)Read, write and execute permissions
iv)Time of last access
PROGRAM:#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
void main()
{ int fd;
struct stat buf;
fd=open("f5.txt",O_RDONLY|O_CREAT,600);
if(fd!=-1)
{ if(fstat(fd,&buf)==0)
{ printf("mode of fileis %u",buf.st_mode);
printf("\n size of the file is %u",buf.st_size);
printf("\n device name %u",buf.st_dev);
printf("\n inode of file is %u",buf.st_ino);
printf("\n no. of links are %u",buf.st_nlink);
printf("\n owner oof a file is %u",buf.st_uid);
printf("\n no.of blocks is %u",buf.st_blocks);
printf("\n group owner is %u",buf.st_gid);
printf("\n blocks size of the file is %u",buf.st_blksize);
printf("\n time of last modifiedis %u",buf.st_ctime);
}
else
printf("error in fstat() syscall");
}
else
printf("error in open() sys call");
}
WEEK-11
remove(p);
}
b)cp
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
void main()
{
int fd1,fd2;
char buf[60];
fd1=open("f2",O_RDWR);
fd2=open("f6",O_RDWR);
read(fd1,buf,sizeof(buf));
write(fd2,buf,sizeof(buf));
close(fd1);
close(fd2);
}
WEEK-12
Aim:-Write a C program that simulates ls Command
Program:#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<dirent.h>
void main()
{
DIR *dp;
struct dirent *dirp;
dp=opendir(".");
while(dirp=(readdir(dp)!=NULL))
{
if(dirp->d_ino==0)
continue;
else
printf("%s \n",dirp->d_name);
}
}