Vous êtes sur la page 1sur 34

CSE LAB1

(MASTER Manual)

Prepared by: N.KRISHNA


HOD:

Signature of

Department of Computer Science and


Engineering
2013-14
II Year IInd sem
CSE LAB List of Conducting Experiments:
FOSS
s.n
o
1

Name of the Experiment

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

a)Log into the system


b)Use the cat command to create a file containing the following
data.
Call it mytable use tabs to separate the fields
1425
Ravi
15.65
4320
Ramu
26.27
6830
Sita
36.15
1450
Raju
21.86
c)Use the cat command to display the file mytable.
d)Use the vi command to correct any errors in the file ,mytable
e)Use the sort command to sort the file mytable according to the
first field. Call the sorted file mytable(same name)
f)Print the file mytable
g)Use the cut and paste commands to swap fields 2 and 3 of
mytable. Call it mytable
h)Print the new file mytable
g)Logout of the system

NAME OF THE EXPERIMENT

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

a)write a shell script that computes yhe gross salary of a

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.

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 id >=1500 then HRA=Rs500 and
DA=98% of the basic
The basic salary is entered interactively through the key
board.
b)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.

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 a shell script that a login name as command line
argument and reports when that person logs in.
c)Write a shell script that receieves two file name as arguments.It
should check whether the file contents are same or not. If they
are same then second file should be deleted.

s.no

Name of the
Experiment

Pg no

a)Write a shell script that


displays a list of all
the files in the
current
directory to which the user
has read,write and
executive
permissions.

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.

c)Write a shell script to


perform the following string
operations:
i)To extract a sub-String
from a given string.
ii)To find the length of a
given string.

10

Write a C program that tkes 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

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

Write C program that simulate the following unix commands:


2. Write a shell script to accept the name of the file from standard input
a)mv
b)cp
(use system calls)
and perform the following tests on it
a. File executable
b. File readable
c. File writable

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

who login recently in to the unix system.


4. Write a shell script to find no. of files in a directory.
5. Write a shell script to check whether a given number is perfect or not.
6. Write a menu driven shell script to copy, edit, rename and delete a file.
7. Write a shell script for concatenation of two strings.
8. Write a shell script which will display Fibonacci series up to a given

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)

log into the system

sol) login
b)

use vi editor to create a file called myfile.txt which contains some text

sol) vi myfile
c)

correct typing errors during creation

sol) practise vi editor commands Esc,i


d) save the file
sol) :wq
e) logout the system
sol) logout

WEEK-1
SESSION-2
Session-2
a) Log into the system
sol:login
b)

open the file created in session 1

sol: vi myfile.txt
c)

Add some text

sol: i->insert the text before the current cursor location


l->insert the text begining of the cursor
A->insert the text at the end of the line
O->create a newline for entry above cursor location
o->create a newline for text entry below the cursor location
d)

Change some text

sol: k->move the cursor up one line


j->move the cursor down one line
H->move the cursor left one character
L->move the cursor right one character
e)

Delete some text

sol: x->delete the character under the cursor location


dw->delete the word
dd->delete the line to the cursor is on
D->delete of the cursor position to the end of the current line

f)

Save the Changes

Sol: Practice the commands in Vi editor


g)

Logout of the system

sol: logout

WEEK-2
a)

login to the system

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

4320 Ramu 26.27


6830 Sita

36.17

1450 Raju

21.86

sol: [13W61A0502 @ localhost ~]$cat>mytable


1425 Ravi

15.65

4320 Ramu 26.27

c)

6830 Sita

36.15

1450 Raju

21.86

use the cat command to display the file,mytable

sol: [13W61A0502 @ localhost ~]$cat>mytable


1425 Ravi

15.65

4320 Ramu 26.27


6830 Sita

36.15

1450 Raju

21.86

d)

use the vi command to correct any errors the file,mytable

sol: verify the file with vi editor commands


e)
use the sort command to sort the file mytable according to the first field.call the
sorted file mytable
sol: [13W61A0502 @ localhost ~]$sort mytable>mytable.txt
[13W61A0502 @ localhost ~]$cat mytable.txt
1425 Ravi

15.65

1450 Raju

21.86

4320 Ramu 26.27


6830 Sita
f)

36.15

print the file mytable

sol: [13W61A0502 @ localhost ~]$sort mytable>mytable.txt


[13W61A0502 @ localhost ~]$cat mytable.txt
1425 Ravi

15.65

1450 Raju

21.86

4320 Ramu 26.27


6830 Sita
g)

36.15

use the cut and paste command to swap the fields 2 and 3 of mytable

sol: [13W61A0502 @ localhost ~]$cut -f1 mytable>mytable1


[13W61A0502 @ localhost ~]$cat mytable1
1425
4320
6830
1450

[13W61A0502 @ localhost ~]$cut -f2 mytable>mytable2


[13W61A0502 @ localhost ~]$cat mytable2
Ravi
Ramu
Sita
Raju
[13W61A0502 @ localhost ~]$cut -f3 mytable>mytable3
[13W61A0502 @ localhost ~]$cat mytable3
15.65
26.27
36.15
21.86

[13W61A0502 @ localhost ~]$paste mytable3 mytable2>mytable4


[13W61A0502 @ localhost ~]$cat mytable4
15.65

Ravi

26.27

Ramu

36.15

Sita

21.86

Raju

[13W61A0502 @ localhost ~]$paste mytable1 mytable4>mytable5

[13W61A0502 @ localhost ~]$cat mytable5

h)

1425

15.65

Ravi

4320

26.27

Ramu

6830

36.15

Sita

1450

21.86

Raju

print the new file mytable

sol: [13W61A0502 @ localhost ~]$cat>myfile.txt

i)

Sowjanya

502

Sirisha 513

80

Viswani 506

85

Niharika

504

85

80

logout the system

sol: logout

WEEK-3

SESSION-1

a)Login to the system


b)Use the appropriate command to determine your login shell
Sol: $echo $SHELL
Sh
c)Use the /etc/passwd file to verify the result of step b.
Sol: $cat /etc/passwd
d)Use the who command and redirect the result to a file called myfile1. Use the
more command to see the contents of myfile1.
Sol : $who > myfile1 | more
User1 pts/0 Apr 23 10:43
User2 pts/1 May 6 18:19
e)Use the date and who commands 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 a file
called myfile2. Use the more command to check the contents of myfile2.
Sol: $ date ; who > myfile2
Fri Aug 9 16:47:32 IST 2008
Cat myfile2 :
User3 pts/2 Apr 25 10:43
User4 pts/3 May 8 18:19

SESSION-2

Input File : file1.dat :


Unix is Multiuser OS
Unix was developed by Brian Kernighan and KenThomson
a)Write a sed command that deletes the first character in each line in a file.
Sol: sed 's/^./ / file1.dat
nix is Multiuser OS
nix was developed by Brian Kernighan and KenThomson
b)Write a sed command that deletes the last character in each line in a file.
Sol: sed '$s/.$//' file1.dat
Unix is Multiuser O
Unix was developed by Brian Kernighan and KenThomso
c)Write a sed command that swaps the first and second words in each line in a file.
Sol:

sed -e 's/\([^ ]\+\) *\([^ ]\+\)/\2 \1/'

sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/'

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

directory, a file, or something else.


Program:
echo " enter file"
read str
if test -f $str
then echo "file exists n it is an ordinary file"
elif test -d $str
then echo "directory file"
else
echo "not exists"
fi
if test -c $str
then echo "character device files"
f
Output:
[13W61A0506@localhost ~]$ sh week5a.sh viswani
viswani is a directory
[13W61A0506@localhost~]$ sh week5a.sh mytable
mytable is a file
[13W61A0506@localhost~]$ mkdir viswani
[13W61A0506@localhost~]$ sh week5a.sh viswani
viswani is a directory

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.

Program:echo enter the login of the user read name


logindetails=`who|grep w $name | grep tty if [ $? ne 0 ]
then
echo $name has not logged in yet exit
fi
loginhours=`echo $logindetails | cut c 26,27`
loginminuts=`echo $logindetails | cut c 29-30`
hoursnow=?date | cut c 12,13`
minnow =`date | cut c 15,16`
hour=`expr $loginhours - $hoursnow`
min=`expr $loginminuts - $minnow`
echo $name is working since $hour Hrs - $min Minuts
OUTPUT:
[13W61A0506@localhost~]$ sh week5c.sh
[13W61A0506@localhost~]$ enter the login of the user
13W61A0506
[13W61A0506@localhost~]$ name has logged in
13W61A0506 : x:737:738::/home/13W61A0506:/bin/bash

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

echo chech the arguments once lastline=?wc l < $1?


if [ $2 lt $lastline a $3 -le $lastline ] then
nline=?expr $3 -$2 + 1?
echo ?tail +$2 $1 | head -$nline? else
echo invalid range specification fi
fi
OUTPUT:
[13E61A0506@localhost~]$ cat>sm.dat
hi
i am fine
welcome
welcome to sivani
[13W61A0506@localhos~] sh week6a.sh
enter the file name
sm.dat
enter the starting line number
1
enter the ending line number
hi
i am fine
welcome

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

echo Chech the arguments once exit


fi
echo Enter a word read word
for file in $* do
grep iv $word $file | tee 1> /dev/null done
echo lines containing given word are delete"
[13W61A0506@localhost~]$ cat sm.dat
unix is os
dos is also os
here using unix
unix is powerful os
[13W61A0506@localhost~]$ cat sm1.dat
windows nt is also os
there are some difference between unix and windows ht
windows nt

[13W61A0506@localhost 2cse~]$ cat del.sd


{
/os/d
}

OUTPUT:
[13W61A0506@localhos 2cse]$ sed- fdcl.sed sm.dat sm1.dat
here using unix

there are some difference between unix and the windows nt


[13W61A0506@localhost ~]$ sed -f del.sed sm.dat sm1.dat
here using unix
unix is powerful os

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

AIM:- Write C programs that simulate the following unix commands:


a)mv
Program:#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];
char *p="/f2";
fd1=open("f2",O_RDWR);
fd2=open("f6",O_RDWR);
read(fd1,buf,sizeof(buf));
write(fd2,buf,sizeof(buf));

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);
}
}

Vous aimerez peut-être aussi