Upload
ishanth-agarwal
View
7
Download
1
Embed Size (px)
DESCRIPTION
linux programs
Citation preview
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 1fiif [ ! -e $1 ]then echo "file doeasnt exists" exit 1fia=`expr $2 + 1 `b=`expr $3 - 1 `#head -$b $1 | tail -n +$ased -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, " exit 1fi n=$1shiftfor i in $*do grep -v $n $i>temp mv temp $i echo $n
echo $idone
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 fidone----------------------------------------------------------------------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 --- no. of lineS= `wc -l`" fidone---------------------------------------------------------------------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 Page 2 of 4
shifttr'.' '\n'tempsort $n|uniq>temp1mv temp1 temp
for i in $*do echo "====> $i
File: /media/ishanth/Ishanth/Linux Lab Progs Executed Page 3 of 4
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
#includeint 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#include#include#includeint 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
File: /media/ishanth/Ishanth/Linux Lab Progs Executed Page 4 of 4
#include#includeint 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#include#includeint 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;}