0% found this document useful (0 votes)
7 views

Unix Lab

BTech-CSE Unix Lab file

Uploaded by

Swarnim Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Unix Lab

BTech-CSE Unix Lab file

Uploaded by

Swarnim Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

GOVERNMENT ENGINEERING COLLEGE,

BILASPUR

SESSION 2018-19

UNIX
&
SHELL
LABORATORY FILE

SUBMITTED TO: SUBMITTED BY:


PRO. NISHANT YADAV SIR
ROLL NO
BRANCH- C.S.E
SEMESTER- 5TH
Name: - Class: - BE (CSE)
Subject: - UNIX & SHELL LAB FILE Year: - 3rd
Roll No.: - Semester: - V

INDEX

Sr. No. Experiment Description Date of Submission Remarks /


Experiment Signature

01 a) 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.
b) Write a shell script that deletes all lines
containing a specified word in one or more files
supplied as arguments to it.

02 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 execute permissions.
b) 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.

03 a) Write a shell script to list all of the directory


files in a directory.
b) Write a shell script to find factorial of a given
integer.

04 a) Write an awk script to count the number of


lines in a file that do not contain vowels.
b) Write an awk script to find the number of
characters, words and lines in a file.

05 a) 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.
b) Write a c program that makes a copy of a file
using standard I/O and system calls.

06 a) Implement in C the following Unix commands


using System calls cat, ls, mv.
b) Write a C program to emulate the Unix ls –l
command.
07 Write a program that takes one or more
file/directory names as command line input and
reports the following information on the file.
a) File type.
b) Number of links.
c) Time of last access.
d) Read, Write and Execute permissions.

08 a) Write a C program to list for every file in a


directory, its inode number and file name.
b) Write a C program that demonstrates
redirection of standard output to a file. Ex: ls> f1.

09 a) Write a C program to create a child process


and allow the parent to display “parent” and the
child to display “child” on the screen.
b) Write a C program that illustrates how to
execute two commands concurrently with a
command pipe. Ex:- ls –l | sort

10 a) Write a C program to create a Zombie process.


b) Write a C program that illustrates how an
orphan is created.
PROGRAM NO. – 01
Object: – (a) 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.

Source Code: –
echo"enter the filename"
readfname
echo"enter the starting line number"
read s
echo"enter the ending line number"
read n
sed -n $s,$n\p $fname | cat > newline
cat newline

Output: –
[root@localhost ~]# vi 1s.sh
[root@localhost ~]# ./1s.sh

bash: ./1s.sh: Permission denied


[root@localhost ~]# chmod 777 1s.sh

[root@localhost ~]# ./1s.sh


enter the filename
sales.dat
enter the starting line number
2
enter the ending line number
4
1 computers 9161
1 textbooks 21312 2 clothing 3252
(b) Write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments
to it.

Source Code: –## forthis program we have to create one or more files (optional),
## I am creating two files names are del ,dell.

[root@localhost ~]# vi del


unixisos
dosis also os
hereusingunix
unixis powerful os
~
[root@localhost ~]# vi dell
windowsntis also os
there are some difference between unix and windowsnt
butunixis great among all os
## after creation two files now we have to write sed script file name isdel.sedusing vi editor.
[root@localhost ~]# videl.sed
{
/os/d
}

Output:

[root@localhost ~]# sed -f del.sed del dell


hereusingunix
there are some difference between unix and windowsnt
PROGRAM NO. – 02
Object: – (a) 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.

Source Code: –echo "enter the directory name"


readdir
if [ -d $dir ]
then
cd $dir
ls> f
exec< f
while read line
do
if [ -f $line ]
then
if [ -r $line -a -w $line -a -x $line ]
then
echo"$line has all permissions"
else
echo"files not having all permissions"
fi
fi
done
fi

Output: –student@ubuntu:~$sh prg3.sh


enter the directory name
dir1
ff has all permissions
files not having permissions
(b) 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.

Source Code: – 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

Output: –guest-glcbIs@ubuntu:~$sh lprg4.sh dir1 d1


dir1is a directory
d1is a file
no of lines in the file are 2
PROGRAM NO. – 03
Object: – (a) Write a shell script to list all of the directory files in a directory.

Source Code: echo "enter the directory name"


readdir
if [ -d $dir ]
then
cd $dir
ls> f
exec< f
while read line
do
if [ -f $line ]
then
if [ -r $line -a -w $line -a -x $line ]
then
echo"$line has all permissions"
else
echo"files not having all permissions"
fi
fi
done
fi

Output: –
student@ubuntu:~$sh prg3.sh
enter the directory name
dir1
ff has all permissions
files not having permissions
(b) Write a Shell script to find factorial of a given integer.

Source Code: – # !/bin/bash


echo"enter a number"
readnum
fact=1
while [ $num -ge 1 ]
do
fact=`expr $fact\* $num`
num=’expr $num – 1’
done
echo"factorial of $n is $fact"

Output: – guest-glcbIs@ubuntu:~$sh lprg7.sh


enter a number
4
Factorial of 4 is 24
PROGRAM NO. – 04
Object: – (a) Write an awk script to count the number of lines in a file that do not contain vowels.
Source Code: –
[singh@00-13-02-56-15-7c programs]$ vi test1

engineering
data
and
lab
workshop
programming
rdx
bpb
hp

[singh@00-13-02-56-15-7c programs]$ vi raj11.sh

#!/bin/bash
echo "Enter file name"
read file
awk '$0!~/[aeiou]/{ count++ }
END{print "The number of lines that does not contain vowels are: ",count}' $file

Output: –
[singh@00-13-02-56-15-7c programs]$ sh raj11.sh
Enter file name
test1
The number of lines that does not contain vowels are: 3

Object: – (b) Write an awk script to find the number of characters, words and lines in a file.

Source Code: –BEGIN{print "record.\t characters \t words"}


#BODY section
{ len=length($0) total_len =len
print(NR,":\t",len,":\t",NF,$0)
words =NF }
END{ print("\n total") print("characters :\t" total len)
print("lines :\t" NR) }

Output: –Student@ubuntu:~$ awk –f cnt.awk ff1


Record words
1: 5: 1hello
Total
Characters:5
Lines:1

PROGRAM NO. – 05
Object: – (a) 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.
Source Code: –
clear

echo " Enter the number of files::"

read n

echo "Enter the "n" files ::"

readfn

set $fn

for i in `cat $1`

do

echo -e " word = $i"

echo -e "------------"

grep -c "$i" $*

echo -e "------------"

done

Output: –
Enter the number of files::
2
Enter the n files ::
ex1.txt ex2.txt

word = this
------------
ex1.txt:1
ex2.txt:0
------------
word = is
------------
ex1.txt:1
ex2.txt:0
------------
word = linux
------------
ex1.txt:1
ex2.txt:1
------------
-bash-3.2$

---------------------------------------------------------*/

(b) Write a c program that makes a copy of a file using standard I/O and system calls.
Source Code: – #include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

voidtypefile (char *filename)


{
intfd, nread;
charbuf[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 (intargc, char **argv)
{ intargno;
for (argno = 1; argno<argc; argno )
typefile (argv[argno]);
exit (0);
}

Output: –
student@ubuntu:~$gcc –o prg10.out prg10.c
student@ubuntu:~$cat >ff
hello
hai
student@ubuntu:~$./prg10.outff
hello

PROGRAM NO. – 06
Object: – (a) Implement in C the following Unix commands using System calls o cat ,ls, mv.
Source Code: –A) cat

#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main(intargc,char *argv[3] )
{
intfd,i;
charbuf[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);
}
}

Output: –
student@ubuntu:~$gcc –o prgcat.outprgcat.c
student@ubuntu:~$cat >ff
hello
hai
student@ubuntu:~$./prgcat.outff
hello
hai

B) ls

#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>
#define FALSE 0
#define TRUE 1
externintalphasort();
char pathname[MAXPATHLEN];
main() {
intcount,i;
structdirent **files;
intfile_select();
if (getwd(pathname) == NULL )
{
printf("Error getting pathn");
exit(0);
}
printf("Current Working Directory = %sn",pathname);
count = scandir(pathname, &files, file_select, alphasort);
if (count <= 0)
{
printf("No files in this directoryn");
exit(0);
}
printf("Number of files = %dn",count);
for (i=1;i<count 1; i)
printf("%s \n",files[i-1]->d_name);
}
intfile_select(struct direct *entry)
{
if ((strcmp(entry->d_name, ".") == 0) ||(strcmp(entry->d_name, "..") == 0))
return (FALSE);
else
return (TRUE);
}

Output: –
Student@ubuntu:~$ gcclist.c
Student@ubuntu:~$ ./a.out
Current working directory=/home/student/
Number of files=57

C) mv

#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main(intargc,char *argv[] )
{ int i,fd1,fd2;
char *file1,*file2,buf[2];
file1=argv[1];
file2=argv[2];
printf("file1=%s file2=%s",file1,file2);
fd1=open(file1,O_RDONLY,0777);
fd2=creat(file2,0777); while(i=read(fd1,buf,1)>0)
write(fd2,buf,1);
remove(file1);
close(fd1);
close(fd2); }

Output: –
student@ubuntu:~$gcc –o mvp.outmvp.c
student@ubuntu:~$cat >ff
hello
hai
student@ubuntu:~$./mvp.outff ff1
student@ubuntu:~$cat ff
cat:ff:No such file or directory
student@ubuntu:~$cat ff1
hello
hai

(b) Write a C program to emulate the Unixls –l command.

Source Code: – #include <stdio.h>


#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
intpid; //process id
pid = fork(); //create another process
if ( pid< 0 )
{ //fail
printf(“\nFork failed\n”);
exit (-1);
} elseif( pid == 0 )
{ //child
execlp ( “/bin/ls”, “ls”, “-l”, NULL ); //execute ls
} else
{ //parent
wait (NULL); //wait for child
printf(“\nchild complete\n”);
exit (0);
}
}

Output: –
guest-glcbIs@ubuntu:~$gcc –o lsc.outlsc.c
guest-glcbIs@ubuntu:~$./lsc.out
total 100
-rwxrwx—x 1 guest-glcbls guest-glcbls 140 2012-07-06 14:55 f1
drwxrwxr-x 4 guest-glcbls guest-glcbls 140 2012-07-06 14:40 dir1 child complete

PROGRAM NO. – 07

Object: – Write a program that takes one or more file/directory names as command line input and reports the
following information on the file
a) File type.
b) Number of links.
c) Time of last access.
d) Read, Write and Execute permissions.
Source Code: -
clear
for i in $*
do
if [ -d $i ]
then
echo “Given directory name is found as $i”
fi
if [ -f $i ]
then
echo “Given name is a file as $i “
fi
echo “Type of file/directory $i”
file $i
echo “Last access time is:”
ls -l$i | cut-c 31-46
echo "no.of links"
ln $i
if [ -x $i –a -w $i-a –r $i ]
then
echo “$i contains all permission”
else
echo “$i does not contain all permissions”
fi
done

Output: –
student@ubuntu:~$sh prg12.sh ff1
given name is file ff1
Type of file/directory ff1
last access time
2012-07-07 10:1
No.of links
ff1 does not contain all permissions

PROGRAM NO. – 08
Object: – (a) Write a C program to list for every file in a directory, its inode number and file name.

Source Code: –#include<stdlib.h>


#include<stdio.h>
#include<string.h>
main(intargc, char *argv[])
{
char d[50];
if(argc==2)
{
bzero(d,sizeof(d));
strcat(d,"ls ");
strcat(d,"-i ");
strcat(d,argv[1]);
system(d);
}
else
printf("\nInvalid No. of inputs");
}

Output: –
student@ubuntu:~$ mkdirdd
student@ubuntu:~$ cd dd
student@ubuntu:~/dd$ cat >f1
hello
^z
student@ubuntu:~/dd$ cd
student@ubuntu:~$gcc –o flist.outflist.c
student@ubuntu:~$./flist.outdd
hello
46490 f1

(b) Write a C program that demonstrates redirection of standard output to a file. Ex: ls> f1.

Source Code: – #include<stdlib.h>


#include<stdio.h>

#include<string.h>
main(intargc, char *argv[])
{
char d[50];
if(argc==2)
{
bzero(d,sizeof(d));
strcat(d,"ls ");
strcat(d,"> ");
strcat(d,argv[1]);
system(d);
}
else
printf("\nInvalid No. of inputs");
}
Output: –
student@ubuntu:~$ gcc –o std.outstd.c
student@ubuntu:~$ls
downloads documents listing.clisting.outstd.cstd.out
student@ubuntu:~$ cat > f1
^z
student@ubuntu:~$./std.out f1
student@ubuntu:~$cat f1
downloads
documents
listing.c
listing.out
std.c
std.out

PROGRAM NO. – 09
Object: – (a) Write a C program to create a child process and allow the parent to display “parent” and the child to
display “child” on the screen.

Source Code: – #include <stdio.h>


#include <sys/wait.h>/* contains prototype for wait */
int main(void)
{
intpid;
int status;
printf("Hello World!\n");
pid = fork( );
if(pid == -1) /* check for error in fork */
{
perror("bad fork");
exit(1);
}
if (pid == 0)
printf("I am the child process.\n");
else
{
wait(&status); /* parent waits for child to finish */
printf("I am the parent process.\n");
}
}
Output: –
student@ubutnu:$gcc –o child.outchild.c
student@ubutnu: ./child.out
Hello World!
I am the child process.
I am the parent process

(b) Write a C program that illustrates how to execute two commands concurrently with a command pipe. Ex:-ls –l |
sort.

Source Code: –#include <stdio.h>


#include <unistd.h>

#include <sys/types.h>
#include <stdlib.h>
int main()
{ intpfds[2];
charbuf[30];
if(pipe(pfds)==-1)
{ perror("pipe failed");
exit(1);
} if(!fork())
{ close(1);
dup(pfds[1];
system (“ls –l”);
}else
{ printf("parent reading from pipe \n"); while(read(pfds[0],buf,80))
printf("%s \n" ,buf);
}
}
Output: –
[student@gcet ~]$ vi pipes2.c
[student@gcet ~]$ cc pipes2.c
[student@gcet ~]$ ./a.out
Parent reading from pipe
Total 24
-rwxrwxr-x l student student 5563Aug 3 10:39 a.out
-rw-rw-r—l
Student student 340 jul 27 10:45 pipe2.c
-rw-rw-r—l student student
Pipes2.c
-rw-rw-r—l student student 401 34127 10:27 pipe2.c
student

PROGRAM NO. – 10
Object: – (a) Write a C program to create a Zombie process.

Source Code: –
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
intpid_tchild_pid;
child_pid = fork ();
if (child_pid> 0) {
sleep (60);
}
else {
exit (0);
}
return 0;
}

Output: –
guest-glcbIs@ubuntu:~$gcczombie.c
guest-glcbIs@ubuntu:~$./a.out
Then command prompt will wait for some time(60 sec) and then again command prompt will appear later.

(b) Write a C program that illustrates how an orphan is created.

Source Code: –#include <stdio.h>


main()
{
intpid ;
printf("I'am the original process with PID %d and PPID %d.\n",getpid(),getppid());
pid=fork();
if(pid!=0 )
{
printf("I'am the parent with PID %d and PPID %d.\n",getpid(),getppid());
printf("My child's PID is %d\n",pid) ;
}
else
{
sleep(4);
printf("I'm the child with PID %d and PPID %d.\n",getpid(), getppid()) ;
}
printf ("PID %d terminates.\n", getpid()) ;
}

Output: –
guest-glcbIs@ubuntu:~$gcc –o prg18.out prg18.c
guest-glcbIs@ubuntu:~$./prg18.out
I am the original process with PID2242 and PPID1677.
I am the parent with PID2242 and PPID1677
My child’s PID is 2243
PID2243 terminates.
$ I am the child with PID2243 and PPID1.
PID2243 termanates.

You might also like