file · web viewis a file as its arguments, counts and reports the occurrence of each...

22
WEEK 1 1.Write a shell script that accepts a file name, starting and ending numbers as arguments and displays all the lines between the given line numbers. echo "Enter the file name" read fname echo "enter starting line number" read sl echo "enter ending line number" read el d=`expr $el - $sl` if [ -f $fname ] then echo " the lines between $sl and $el of given file are " head -$el $fname | tail -$d else echo "file doesnt exist" fi

Upload: dohanh

Post on 13-Mar-2018

215 views

Category:

Documents


2 download

TRANSCRIPT

WEEK 1

1.Write a shell script that accepts a file name, starting and ending numbers as arguments and displays allthe lines between the given line numbers.

echo "Enter the file name"read fnameecho "enter starting line number"read slecho "enter ending line number"read eld=`expr $el - $sl` if [ -f $fname ]thenecho " the lines between $sl and $el of given file are "head -$el $fname | tail -$delseecho "file doesnt exist"fi

2. Write a shell script that deletes all lines containing the specified word in one or more files supplied asarguments to it.

if [ $# -ne 0 ]thenecho enter the wordread wordfor fname in $*doif [ -f $fname ]thenecho the given input filename is:$fnamegrep -v "$word" $fnameelseecho its not a filefidoneelseecho "enter atleast one argument as input"fi

3. Write a shell script that displays a list of all files in the current directory to which the user has read, writeand execute permissions.

echo "List of Files which have Read, Write and Execute Permissions in CurrentDirectory"for file in *doif [ -r $file -a -w $file -a -x $file ]thenecho $filefidone

4.Write a shell script that receives any number of file names as arguments checks if every argumentsupplied is a file as its arguments, counts and reports the occurrence of each word that is present in the firstargument file on other argument files.

for x in $*doif [ -f $x ]thenecho "$x is a file"echo "no of lines in the file are"wc -l $xelif [ -d $x ]thenecho "$x is a directory"elseecho "enter valid filename or directory name"fidone

WEEK 2 5. Write a shell script that accepts a list of file names as its arguments, counts and reports the occurrence ofeach word that is present in the first argument file on other argument files.

echo " Enter the number of files::"read necho "Enter the "n" files ::"read fnset $fnfor i in `cat $1`doecho " word = $i"echo "------------"grep -c "$i" $*echo " "done

6. Write a shell script to list all of the directory files in a directory.

# !/bin/bashecho "enter directory name"read dirif [ -d $dir ]thenecho "list of files in the directory"ls $direlseecho "enter proper directory name"fi

Output:

7. Write a shell script to find factorial of a given number.

echo “ Factorial”echo "Enter a number: " read ffact=1factorial=1while [ $fact -le $f ] do factorial=`expr $factorial \* $fact` fact=`expr $fact + 1` done echo "Factorial of $f = $factorial"

8) Write am awk script to find the number of lines in a file that do not contain vowels.

Program:BEGIN{printf "the no of lines not contain vowels are:"}!/[aA]|[eE]|[Ii]|[Oo]|[Uu]/{k++}END{printf "%d",k}

Output:

9) Write a awk script to find the number of characters, words and lines in a file.

Program:

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

Output:

10) Write a C Program that makes a copy of a file using standard I/O and system calls.

Program:#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <stdlib.h>void typefile (char *filename){int fd, nread;char buf[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 (int argc, char **argv){int argno;for (argno = 1; argno < argc; argno )typefile (argv[argno]);exit (0);}

Output:student@ubuntu:~$gcc –o prg10.out prg10.cstudent@ubuntu:~$cat > ffhellohaistudent@ubuntu:~$./prg10.out ffhellohai

11) Implement in C the following Unix commands using system calls A). cat B). mv

Programs:

A) cat

#include<sys/types.h>#include<sys/stat.h>#include<stdio.h>#include<fcntl.h>#include<unistd.h>int main( int argc,char *argv[3] ){int fd,i;char buf[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:

B) mv

#include<sys/types.h>#include<sys/stat.h>#include<stdio.h>#include<fcntl.h>#include<unistd.h>int main( int argc,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:

13) Write a C program to emulate the Unix ls-l command.

Program:#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>#include <stdlib.h>int main(){int pid; //process idpid = fork(); //create another processif ( pid < 0 ){ //failprintf("\nFork failed\n");exit (-1);}else if ( pid == 0 ){ //childexeclp ( "/bin/ls", "ls", "-l", NULL ); //execute ls}

else{ //parentwait (NULL); //wait for childprintf("\nchild complete\n");exit (0);}}

Output:

14.Write a C program to list for every file in a directory, its inode number and file name.Program:#include<stdlib.h>#include<stdio.h>#include<string.h>int main(int argc, char *argv[]){char d[50];if(argc==2){bzero(d,sizeof(d));strcat(d,"ls ");strcat(d,"-i ");strcat(d,argv[1]);system(d);}elseprintf("\nInvalid No. of inputs");}output:

student@ubuntu:~$ mkdir dd

student@ubuntu:~$ cd ddstudent@ubuntu:~/dd$ cat >f1hello^zstudent@ubuntu:~/dd$ cdstudent@ubuntu:~$gcc –o flist.out flist.cstudent@ubuntu:~$./flist.out ddhello46490 f1

15) Write a C Program that demonstrates redirection of standard output to a file .EX:ls>f1.

Program:

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

Output:

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

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

}

Output:guest-glcbIs@ubuntu:~$gcc zombie.c

guest-glcbIs@ubuntu:~$./a.outThen command prompt will wait for some time(60 sec) and then again command prompt will appear later.

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

Program:#include <stdio.h>#include<unistd.h>int main(){int pid ;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.cguest-glcbIs@ubuntu:~$./prg18.outI am the original process with PID2242 and PPID1677.I am the parent with PID2242 and PPID1677My child’s PID is 2243PID2243 terminates.$ I am the child with PID2243 and PPID1.PID2243 termanates.

19. Write a program that illustrates how to execute two commands concurrently with a command pipe. Ex.ls –l | sort

Program:#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h> int main(){int pfds[2];char buf[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

20. Write a C programs that illustrate communication between two unrelated processes using named pipe.

Program:#include<stdio.h>#include<stdlib.h>#include<errno.h>#include<unistd.h>int main(){int pfds[2];char buf[30];if(pipe(pfds)==-1){perror("pipe");exit(1);

}printf("writing to file descriptor #%d\n", pfds[1]);write(pfds[1],"test",5);printf("reading from file descriptor #%d\n ", pfds[0]);read(pfds[0],buf,5);printf("read\"%s\"\n" ,buf);}

Output:

[student@gcet ~]$ vi pipes1.c

[student@gcet ~]$ cc pipes1.c

[student@gcet ~]$ ./a.out

writing to file descriptor #4

reading from file descriptor #3

read"test"