Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

/*8.Write an awk script to count the number of lines in a file that do not contain vowels.

*/ //Program for i in $* do awk -f l_awk.awk $i done l_awk.awk BEGIN{ lines=0; } !/[aeiou]/ { lines++; } END { print(lines,abc.c); } Test data:I/P: sh linux8.sh hello.c O/P: 1 hello.c /*9.Write an awk script to find the number of characters, words and lines in a file.*/ //Program for i in $* do awk -f wc_awk.awk $i done wc_awk.awk BEGIN{ print("Number of words, lines and characters in a file:"); lines = 0; numwords = 0; numchar = 0; } { lines++; if(length($0) > 1) numwords += NF; numchar += length($0);

} END { print("Number of lines:",lines); print("Number of words:",numwords); print("Number of characters:",numchar+NR,FILENAME); } Test data:I/P: sh linux9.sh hello.c O/P: Number of words, lines and characters in a file: Number of lines: 7 Number of words: 23 Number of characters: 206 hello.c /*11. Implement mv command with Linux system calls link,unlink.*/ //Program #include<stdio.h> #include<unistd.h> #include<string.h> int main(int argc,char *argv[]) { if(argc!=3){ printf("Usage mv old_file_name new_file_name\n"); return(1); } if(link(argv[1],argv[2])==0) return unlink(argv[1]); return -1; } Test data:I/P: ls -l linux11.c O/P: -rw-rw-r-- 1 exam exam 284 2011-11-14 07:28 linux11.c I/P: cc o cp3 linux11.c ./cp3 linux11.c lul.c ls -l linux11.c O/P: ls: cannot access linux11.c: No such file or directory I/P: ls -l lul.c

O/P: -rw-rw-r-- 1 exam exam 284 2011-11-14 07:28 lul.c /*14.Write a C program to list for every file in a directory, its inode number and filename.*/ //Program #include<stdio.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<sys/stat.h> #include<dirent.h> typedef struct dirent Dirent; int main(int argc,char *argv[]){ Dirent* dp; DIR* dir_fdesc; while(--argc>0){ if((dir_fdesc=opendir(*++argv))==NULL){ printf("Open Dir Error:%s\n",*argv); return 1; } while(dp=readdir(dir_fdesc)) printf("%d\t%s\n",dp->d_ino,dp->d_name); closedir(dir_fdesc); } } Test data:I/P: cc -o abc linux14.c ./abc .* O/P: 1115122 priority.c 1116461 pt11.c 1116118 cp3 1115494 mftvs.c 1116345 outlinux11.c 1115493 mftfs.c 1114942 factorial.c 1115871 lfu.c

1115425 distance.c 1116124 .l_awk.awk.swp 1115796 lin_disp.sh 1115066 fcfs.c 1116463 lul.c 1116468 linux14.c 1115221 crc.c 1114297 .. 1115284 shortest.c 1116072 linux8.sh 1115422 .fifo.c.swp 1115853 mvt.c 1116473 linux21.c 1115392 .lru.c.swo 1116260 hello.c 1115047 spn.c 1115254 .linux11.c.swp 1115283 .lru.c.swp 1115424 fifo.c 1114929 . 1116086 outputsimple.txt 1116462 temp 1116143 bankeravoidance.c 1116135 simplepaging.c 1116465 abc 1115081 rr.c 1115426 .mvt.c.swp 1115421 .shortest.c.swp 1116454 a.out 1115869 lru.c 1114794 Videos 1114865 .tomboy 1114298 .kde 1114793 Pictures 1114927 .bash_history 1114923 1233 1114817 .redhat 1114861 .wapi 1114939 .mozilla

/*16.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.*/

//Program #include<stdio.h> int main() { int pid; if((pid=fork())==0){ printf("Child\n"); }else printf("Parent\n"); return 0; } Test data:I/P: cc linux16.c ./a.out O/P: Child Parent /*21.Write a C program in which parent writes a message to a pipe and the child reads the message.*/ //Program #include<stdio.h> #include<fcntl.h> #include<string.h> int main() { int p1[2],pid,k,fd; char a[80],d[80]; pipe(p1); if((pid=fork())!=0){ close(p1[0]); printf("Enter string for parent:"); scanf("%s",d); write(p1[1],d,strlen(d)); }else{ close(p1[1]);

sleep(5); while((k=read(p1[0],a,80))>0)write(1,a,k); printf(" Messsage Read from Parent=%s\n",a); return(0); } return 0; } Test data:I/P: cc linux21.c ./a.out Enter string for parent:hello O/P: hello Message Read from Parent=hello

You might also like