Unix Lab
Unix Lab
BILASPUR
SESSION 2018-19
UNIX
&
SHELL
LABORATORY FILE
INDEX
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
Source Code: –## forthis program we have to create one or more files (optional),
## I am creating two files names are del ,dell.
Output:
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.
engineering
data
and
lab
workshop
programming
rdx
bpb
hp
#!/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.
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
read n
readfn
set $fn
do
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>
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
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.
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.
#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.
(b) Write a C program that illustrates how to execute two commands concurrently with a command pipe. Ex:-ls –l |
sort.
#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.
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.