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

Linux Lab Progs Executed

This document contains summaries of 14 Linux shell scripting and C programming assignments: 1) A script to display lines between specified start and end lines of a file. 2) A script to delete lines containing a specified word from one or more files. 3) A script to display files user has read, write and execute permissions for.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Linux Lab Progs Executed

This document contains summaries of 14 Linux shell scripting and C programming assignments: 1) A script to display lines between specified start and end lines of a file. 2) A script to delete lines containing a specified word from one or more files. 3) A script to display files user has read, write and execute permissions for.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

File: /media/ishanth/Ishanth/Linux Lab Progs Executed

Page 1 of 4

1) write a shell script that accepts filename,starting and ending line as arguments and displays all
the lines between them.
if [ $# -lt 3 ]
then
echo "enter vlaid no. of arguments i.e, Filename,starting line,endind line no."
exit 1
fi
if [ ! -e $1 ]
then
echo "file doeasnt exists"
exit 1
fi
a=`expr $2 + 1 `
b=`expr $3 - 1 `
#head -$b $1 | tail -n +$a
sed -n "$a,$b p" $1
---------------------------------------------------------------------2)write a shell script that deletes all the lines containing a specified word in one or more files
supplied as arguments.
if [ $# -eq 0 ]
then
echo "enter sufficient arguments i.e, <word> <filenames>"
exit 1
fi
n=$1
shift
for i in $*
do
grep -v $n $i>temp
mv temp $i
echo $n
echo $i
done
sh second.sh sumi filename1 filename filename3
---------------------------------------------------------------------3) write sh script that displays a list of all the files in the current directory to which the user
has read,write and execute permissions.
for i in *
do
if [ -x $i -a -w $i -a -r $i ]
then
echo $i
fi
done
---------------------------------------------------------------------shellscript is used to receives 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 i in *
do
if [ -d $i ]
then
echo $i is a directory
fi
if [ -f $i ]
then
echo " $i --fi

no. of lineS= `wc -l`"

done
--------------------------------------------------------------------write a shell script that accepts a list of names as its arguments,counts and reports the occurance of
each word that is present in the first argument file on other argument files
n=$1

File: /media/ishanth/Ishanth/Linux Lab Progs Executed


shift
tr'.' '\n'<$n>temp
sort $n|uniq>temp1
mv temp1 temp
for i in $*
do
echo "====> $i <====="
for j in `cat $temp`
do
printf "%-12s%1d times\n"$j `grep -wco $j $i`
done
done
--------------------------------------------------------------------Shell program display all the directory name in a directory which is passed by commandline
if [ $# -ne 1 ]
then
echo "enter suffiecent arguments i.e, dir name"
exit
fi
cd $1
for i in *
do
if [ -d $i ]
then
echo $i
fi
done
----------------------------------------------------------------------shell script for finding factorial
echo "enter any integer"
read n
m=$n
if [ $n -lt 0 ]
then
echo "enter only +ve numbers"
exit 1
fi
fact=1
while [ $n -ne 0 ]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done
echo "factorial for entered number $m = $fact"
----------------------------------------------------------------------copying the contents of one file to another using standard input output
functions
#include<stdlib.h>
#include<stdio.h>
int main(int argc,char * argv[])
{
FILE *fp1,*fp2;
int ch;
if(argc!=3)
{
printf("enter sufficient nuo.of arguments");
exit(1);
}
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{
printf("file hasnt opened properly");
exit(1);
}
fp2=fopen(argv[2],"w");

Page 2 of 4

File: /media/ishanth/Ishanth/Linux Lab Progs Executed


if(fp2==NULL)
{
printf("file hasnt opened properly");
exit(1);
}
while((ch=fgetc(fp1))!=EOF)
fputc(ch,fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
--------------------------------------------------------------------c program that copies one file to another using system calls
#include<stdio.h>
int main(int argc,char * argv[])
{
int fd1,fd2;
char b[512];
int n;
if(argc!=3)
{
printf("enter sufficient nuo.of arguments");
exit(1);
}
fd1=open(argv[1],O_RDONLY);
if(fd1==-1)
{
printf("file hasnt opened properly");
exit(1);
}
fd2=open(argv[2],O_WRONLY|O_CREAT,0666);
if(fd2==-1)
{
printf("file hasnt opened properly");
exit(1);
}
while((n=read(fd1,b,sizeof(b)))>0)
write(fd2,b,n);
close(fd1);
close(fd2);
return 0;
}
-------------------------------------------------------------------Write a C program that redirects standard output to a file.Ex: ls > f1.
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int fd;
fd = open("filename.txt",O_WRONLY|O_TRUNC|O_CREAT,0666);
if(fd==-1)
{
printf("file doesnt opened");
exit (1);
}
dup2(fd,1);
close(fd);
execlp("ls","ls",NULL);
printf("execl error ouccured");
return 0;
}
-------------------------------------------------------------------12. Write a C program to list all files in a directory.
#include<stdlib.h>

Page 3 of 4

File: /media/ishanth/Ishanth/Linux Lab Progs Executed


#include<dirent.h>
#include<stdio.h>
int main(int argc,char * argv[])
{
struct dirent *p;
DIR *d;
if(argc!=2)
{
printf("enter sufficient nuo.of arguments");
exit(1);
}
d=opendir(argv[1]);
chdir(argv[1]);
if(d==NULL)
{
printf("dir is not opened properly");
exit(1);
}
while((p=readdir(d))!=NULL)
{
if(strcmp(p->d_name,".")==0 || strcmp(p->d_name,"..")==0|| ( p->d_name[0] == '.'))
continue;
printf("%s \n",p->d_name);
}
return 0;
}
---------------------------------------------------------------------c program which list the files in the directory and its inode number
#include<stdlib.h>
#include<dirent.h>
#include<stdio.h>
int main(int argc,char * argv[])
{
struct dirent *p;
DIR *d;
if(argc!=2)
{
printf("enter sufficient nuo.of arguments");
exit(1);
}
d=opendir(argv[1]);
chdir(argv[1]);
if(d==NULL)
{
printf("dir is not opened properly");
exit(1);
}
while((p=readdir(d))!=NULL)
{
if(strcmp(p->d_name,".")==0 || strcmp(p->d_name,"..")==0|| ( p->d_name[0] == '.'))
continue;
printf("%s , %ld \n",p->d_name,p->d_ino);
}
return 0;
}

Page 4 of 4

You might also like