100% found this document useful (1 vote)
867 views

Experiment:1: Write A Shell Script To Generate A Multiplication Table

The document contains summaries of several experiments related to shell scripting and C programming. The experiments cover topics like: - Writing shell scripts to generate multiplication tables, copy files to directories, count lines/words in files, and display directory listings - Implementing basic calculator operations using shell script arguments and conditions - Writing C programs to count blanks in files using standard I/O and system calls, get file type/link information, and execute concurrent commands using pipes

Uploaded by

gdayanand4u
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
867 views

Experiment:1: Write A Shell Script To Generate A Multiplication Table

The document contains summaries of several experiments related to shell scripting and C programming. The experiments cover topics like: - Writing shell scripts to generate multiplication tables, copy files to directories, count lines/words in files, and display directory listings - Implementing basic calculator operations using shell script arguments and conditions - Writing C programs to count blanks in files using standard I/O and system calls, get file type/link information, and execute concurrent commands using pipes

Uploaded by

gdayanand4u
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 48

Experiment:1

AIM: Write a shell script to generate a multiplication table.

PROGRAM :

echo enter the number read a echo enter the num up to which the multiplication table has to be printed read n i=1; while [ $i -le $n ] do c=`expr $a \* $i` echo "$a * $i = $c" i=`expr $i + 1` done

ADVANCED UNIX PROGRAMMING LAB

OUTPUT :
Enter the number 5

enter the num up to which the multiplication table has to be printed 10

5* 1= 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50

ADVANCED UNIX PROGRAMMING LAB

Experiment:2 AIM : Write a shell script that copies multiple files to a directory.

PROGRAM :

echo enter the target directory read dir if [ ! -d $dir ] then echo directory does not exits else echo enter the number of files to copy read n i=1 echo enter the file names while [ $i -le $n ] do read file[$i] if [ ! -f ${file[$i]} ] then echo file does not exists i=`expr $i - 1` ADVANCED UNIX PROGRAMMING LAB 3

else cp ${file[$i]} $dir fi i=`expr $i + 1` done

OUTPUT :

enter the target directory shellpgrs enter the number of files to copy 2 enter the file names lab7a.c lab7b.c the files in the shellpgrs directory are lab7b.c lab7a.c lab5.sh lab4.sh lab3.sh lab2.sh lab1.sh

ADVANCED UNIX PROGRAMMING LAB

Experiment:3

AIM :

Write a shell script which counts number of lines and words present in a given file.

PROGRAM :

echo enter filename read file1 echo the no of lines in $file1 are nol=`wc -l $file1 | cut -c -7` echo $nol echo the no of words in $file1 are now=`wc -w $file1 | cut -c -7` echo $now

ADVANCED UNIX PROGRAMMING LAB

OUTPUT :

enter filename lab1.sh the no of lines in lab1.sh are 13 the no of words in lab1.sh are 44

ADVANCED UNIX PROGRAMMING LAB

Experiment:4

AIM: Write a shell script which displays the list of files in the given directory.

PROGRAM : echo "enter the name of the directory" read dir if [ -d $dir ] then echo "files in the directory $dir are" ls -r $dir else echo the given $dir not available fi

OUTPUT : enter the name of the directory shellpgrs files in the directory shellpgrs are lab7b.c lab7a.c lab5.sh lab4.sh lab3.sh lab2.sh lab1.sh

ADVANCED UNIX PROGRAMMING LAB

Experiment:5 AIM : Write a shell script(small calculator) that adds, subtracts, multiplies and divides the given two integers. There are two division options: one returns the quotient and the other returns reminder. The script requires 3 arguments: The operation to be used and two integer numbers. The options are add(-a), subtract(-s), multiply(-m), quotient(c) and reminder(-r). PROGRAM : echo "enter two integers:" read a read b while true do echo -e "1. Addition \n2. Subtraction \n3. Multiplication\n4. Quotient\n5. Reminder\n6. Exit" echo "enter option" read op case $op in 1) add=`expr $a + $b`; echo "addition of two integers :"$add;; 2) sub=`expr $a - $b`; ADVANCED UNIX PROGRAMMING LAB 8

echo "subtraction of two integers:"$sub;; 3) mul=`expr $a \* $b`; echo "multiplication of two integers:"$mul;; 4) r=`expr $a % $b`; echo "remainder is:"$r;; 5) q=`expr $a / $b`; echo "quotient is:"$q;; 6) exit;; *) echo invalid option;; esac done OUTPUT : enter two integers: 4 3 1. Addition 2. Subtraction 3. Multiplication 4. Quotient 5. Reminder 6. Exit enter option 1 ADVANCED UNIX PROGRAMMING LAB 9

addition of two integers :7 1. Addition 2. Subtraction 3. Multiplication 4. Quotient 5. Reminder 6. Exit enter option 2 subtraction of two integers:1 1. Addition 2. Subtraction 3. Multiplication 4. Quotient 5. Reminder 6. Exit enter option 3 multiplication of two integers:12 1. Addition 2. Subtraction 3. Multiplication 4. Quotient ADVANCED UNIX PROGRAMMING LAB 10

5. Reminder 6. Exit enter option 4 remainder is:1 1. Addition 2. Subtraction 3. Multiplication 4. Quotient 5. Reminder 6. Exit enter option 5 quotient is:1 1. Addition 2. Subtraction 3. Multiplication 4. Quotient 5. Reminder 6. Exit enter option 6

ADVANCED UNIX PROGRAMMING LAB

11

Experiment:6 AIM : Write a shell script to reverse the rows and columns of a matrix.

PROGRAM :

echo " enter size of matrix " echo "row size " read m echo " coloumn size " read n i=1; j=1 echo " Enter Matrix elements " while [ $i -le $m ] ; do while [ $j -le $n ] ; do read a[$i$j] j=`expr $j + 1` done i=`expr $i + 1` j=1 done echo " The reverse Matrix Is: " ADVANCED UNIX PROGRAMMING LAB 12

i=1 j=1 while [ $i -le $n ] ; do while [ $j -le $m ] do echo -e " ${a[$j$i]} \c" j=`expr $j + 1` done i=`expr $i + 1` j=1 echo " " done

ADVANCED UNIX PROGRAMMING LAB

13

OUTPUT :

enter size oif matrix row size 2 coloumn size 2 Enter Matrix elements 1 2 3 4 The reverse Matrix Is: 1 3 2 4

ADVANCED UNIX PROGRAMMING LAB

14

Experiment:7 ( a )

AIM :

Write a C program that counts the number of blanks in a text file. using standard I/O using system calls. a. Using standard I/O

PROGRAM : #include<stdio.h> main(int argc,char *argv[]) { int i=1,c,sp; FILE *fp; fp=stdin; do { if(argc>1 && (fp=fopen(argv[i],"r"))==NULL) { printf("can not open %s\n",argv[i]); continue; } sp=0; ADVANCED UNIX PROGRAMMING LAB 15

while((c=getc(fp))!=EOF) { if(c==' ' || c=='\t' || c=='\n') sp=sp+1; } printf("The no of spaces in the file are : %d\n",sp); fclose(fp); }while(++i<argc); }

OUTPUT : The no of spaces in the file are : 48

ADVANCED UNIX PROGRAMMING LAB

16

Experiment:7 ( b ) AIM :

Write a C program that counts the number of blanks in a text file. using standard I/O using system calls. b. Using system calls

PROGRAM : #include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<stdlib.h> int main () { int fd; char buff[2]; int c=0; char pathname[30]; printf("enter file name:"); scanf("%s",pathname); if((fd=open(pathname,O_RDONLY))==-1) { printf("file not exists/unable to open"); ADVANCED UNIX PROGRAMMING LAB 17

exit(1); } while(read(fd,buff,1)) { if(buff[0]==' ') c++; } printf("Blanks count in given file are: %d",c); return 0; }

OUTPUT :

enter file name:lab1.sh Blanks count in given file are: 35

ADVANCED UNIX PROGRAMMING LAB

18

Experiment:8 ( a ) AIM :

Implement in C the following Unix commands using system calls. a) cat PROGRAM : include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<stdlib.h> int main () { int fd; char buff[2]; char pathname[30]; printf("Enter file name:"); scanf("%s",pathname); if((fd=open(pathname,O_RDONLY))==-1) { printf("File not Exits or Unable to open a file"); exit(1); } while(read(fd,buff,1)) ADVANCED UNIX PROGRAMMING LAB 19

{ if((write(1,buff,1))==-1) printf("write Error"); } }

OUTPUT :

Enter file name:lab1.sh echo enter the number read a echo enter the num upto which the multiplication table has to be printed read n i=1; while [ $i -le $n ] do c=`expr $a \* $i` echo "$a * $i = $c" i=`expr $i + 1` done

ADVANCED UNIX PROGRAMMING LAB

20

Experiment:8 ( b ) AIM : Implement in C the following Unix commands using system calls. b) mv

PROGRAM :

#include<unistd.h> #include<stdio.h> #include<stdlib.h> int main(int argc,char *argv[]) { if(argc!=3) { printf("usage:a.out <old name> <new name>"); exit(1); } if(access(argv[1],F_OK)<0) { if("File not found"); exit(2); } if(rename(argv[1],argv[2])<0) ADVANCED UNIX PROGRAMMING LAB 21

{ printf("Rename Error"); exit(3); } printf("%s renamed as %s",argv[1],argv[2]); return 0; }

OUTPUT :

$./a.out lab8c.c lab8b.c lab8c.c renamed as lab8b.c

ADVANCED UNIX PROGRAMMING LAB

22

Experiment:9 ( a ) AIM :

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.

PROGRAM :

#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> int main(int argc,char *argv[]) { struct stat b; int i; for(i=1;i<argc;i++) { if(lstat(argv[i],&b)==-1) { printf("%s",argv[i]); fflush(stdout); ADVANCED UNIX PROGRAMMING LAB 23

perror("lstat error"); continue; } else if(S_ISREG(b.st_mode)) printf("regular file\n"); else if(S_ISDIR(b.st_mode)) printf("directory file \n"); else if(S_ISCHR(b.st_mode)) printf("character special file\n"); else if(S_ISBLK(b.st_mode)) printf("block special file \n"); else if(S_ISFIFO(b.st_mode)) printf("pipefile \n"); else if(S_ISLNK(b.st_mode)) printf("symbloic link file"); else if(S_ISSOCK(b.st_mode)) printf("socket file \n"); else printf("unknown file"); } }

ADVANCED UNIX PROGRAMMING LAB

24

OUTPUT : $./a.out lab1.sh regular file

ADVANCED UNIX PROGRAMMING LAB

25

Experiment:9 ( b ) AIM : Write a program that takes one or more file/directory names as command line input and reports the following information on the file: b) Number of links.

PROGRAM : #include<sys/stat.h> #include<stdio.h> #include<unistd.h> #include<stdlib.h> int main(int argc,char *argv[]) { struct stat statbuf; int i; if(argc<2) { printf("usage:./a.out< filename(s)"); exit(1); } for(i=1;argc!=i;i++) { if(access(argv[i],F_OK)<0) ADVANCED UNIX PROGRAMMING LAB 26

{ printf(" \n %s,File/dir not exists",argv[i]); continue; } else { if(lstat(argv[i],&statbuf)<0) { printf("unable to read Status of file :%s",argv[i]); continue; } printf("No of links :%d \t",statbuf.st_nlink); } } return(0); } OUTPUT : No of links :1 Last access time is 1207834720 Read permissions for the user Write permissions for the user Execute permissions for the user

ADVANCED UNIX PROGRAMMING LAB

27

Experiment: 10 AIM: Write a C program that illustrates how to execute two commands concurrently with a command pipe. PROGRAM :

#include<unistd.h> int main() { int fd1[2],fd2[2],pid,i; char buf[512]={hello}; pipe(fd1); pipe(fd2); if(fork()==0) { close(fd1[0]); close (fd2[1]); write(fd1[1],hello,5); read(fd2[0],buf,5); } else { close(fd1[1]); close(fd2[0]); read(fd2[0],buf,5); for(i=0;i<=5;i++) buf[i]=toupper(buf[i]); printf(%s\n,buf); } } ADVANCED UNIX PROGRAMMING LAB 28

Output: [cse@unix API]$ cc ipcunname3.c [cse@unix API]$ ./a.out HELLO

Experiment:11 AIM : Write a C program that illustrates the creation of child process using fork system call. PROGRAM :

#include<stdio.h> #include<unistd.h> #include<sys/types.h> int main() { int pid_t,pid; printf("the original process with pid %d and ppid %d\n",getpid(),getppid()); pid=fork(); if(pid!=0) ADVANCED UNIX PROGRAMMING LAB 29

{ printf("\nthe parent process with pid %d and ppid %d \n",getpid(),getppid()); printf("the child pid is %d\n",pid); } else printf("\nthe child process with pid %d and ppid %d \n",getpid(),getppid()); printf("\npid %d terminates \n",getpid()); }

OUTPUT :

the original process with pid 2899 and ppid 2735

the child process with pid 2900 and ppid 2899

ADVANCED UNIX PROGRAMMING LAB

30

Experiment:12. AIM: Write a C Program that illustrates the creation of child process using Vfork( ) system call. Source Code: #include<stdio.h> #include<unistd.h> #include<sys|types.h> int glob=6; char buf[]=a write to stdout\n; int main(void) { int var; pid-t.pid; var=88; if(write(stdout-fileno, buf,sizeof(buf)=1!=size of (buf)-1)) printf(write error); printf(before vfork()); if(((pid=vfork())<0 printf(fork error); else if(pid==0) { glob++; var++; exit(0); } printf(pid=%d,glob=%d var=%d,getpid(), glob, var); exit(0); } Output: Before vfork pid=21254,glob=7,var=89 ADVANCED UNIX PROGRAMMING LAB 31

Experiment:13 AIM:

Write a C program that illustrates file locking semaphores.

Source code: #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #define shmsz 27 main() { char c; int shmid; key_t key; char *shm, *s; key = 5678; if ((shmid = shmget(key, shmsz, ipc_creat | 0666)) < 0) { perror("shmget"); exit(1); } if ((shm = shmat(shmid, null, 0)) == (char *) -1) { perror("shmat"); exit(1); } s = shm; for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = null; while (*shm != '*') ADVANCED UNIX PROGRAMMING LAB 32

sleep(1); exit(0);

shm_client.c #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #define SHMSZ 27 main() { int shmid; key_t key; char *shm, *s; key = 5678; if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { perror("shmget"); exit(1); } if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } for (s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); *shm = '*'; exit(0); }

ADVANCED UNIX PROGRAMMING LAB

33

Output: [cse@unix API] $ cc sharemem.c [cse@unix API] $ ./a.out The uid and gid is 504 504 Size is 4096 Lpid is 0 Cpid is 13307 Access time is 0 Deattach time is 0 Change time is 1144321934 Experiment:14 AIM:

Write a C program that implements a producer-consumer system with two process using semaphores.

Source Code: Let's suppose that we have N units free space for new products and M units occupied space (some product occupies the space). If N > 0 , then a coming producer can proceed. If M > 0 , then a coming consumer can proceed. Only one producer or consumer is allowed go to the critical area. #define N 100 typedef int semaphore; semaphore mutex = 1; /* Control access to the critical section / semaphore empty = N; /* Counts empty buffer slots */ semaphore full = 0; /* Counts full buffer slots */ producer() { int item; ADVANCED UNIX PROGRAMMING LAB 34

while( TRUE) { produce_item(&item); P(&empty) /* Decrement empty count */ P(&mutex) /* Enter critical region */ enter_item(item); /* Put new item in buffer */ V(&mutex); /* Leave critical region */ V(&full); /* increment count of full slots */ } }

consumer() { int item; while( TRUE) { P(&full) /* Decrement full count */ P(&mutex) /* Enter critical region */ remlove_item(item); /* Take item from buffer */ V(&mutex); /* Leave critical region */ V(&empty); /* increment count of empty slots */ consume_item(item); } } Output: [cse@unix API]$ cc sem.c [cse@unix API]$ ./a.out the uid is 504 semaphore count is 1 modification time is 0 changed time is 1144305537

ADVANCED UNIX PROGRAMMING LAB

35

Experiment:15 AIM:

communication using shared memory system calls. Source Code:

Write a C program that illustrates inter process

#include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> Int main() { Int fd,k; Fd=shmget(IPC_PRIVATE,getpagesize(),IPC_CREAT|IPC_EXCL|0766); if(fd<0) { printf(not created); } struct shmid_ds p; k=shmctl(fd,ipc_stat,&p); printf(the uid and gid is %d\n%d\n,p.shm_perm.uid,p.shm_perm.gid); printf((size is %d\n,p.shm_segsz); printf(lpid is %d\n,p.shm_lpid); printf(cpid is %d\n,p.shm_cpid); printf(access time is %d\n,p.shm_atime); printf(deattach time is %d\n,p.shm_dtime); printf(change time is %d\n,p.shm_ctime); }

ADVANCED UNIX PROGRAMMING LAB

36

Output: [cse@unix API]$ cc sharedmen.c [cse@unix API]$ ./a.out the uid and gid is 504 504 size is 4096 lpid is 0 cpid is 13307 access time is 0 deattach time is 0 change time is 1144321934

Experiment:16 AIM: Write a C program that illustrates the following: (i) Creating a message queue. (ii) Writing to a message queue. (iii) Reading from a message queue. Source Code: #include #include #include #include #include #include <stdio.h> <stdlib.h> <errno.h> <sys/types.h> <sys/ipc.h> <sys/msg.h>

struct my_msgbuf { long mtype; char mtext[200]; };

ADVANCED UNIX PROGRAMMING LAB

37

int main(void) { struct my_msgbuf buf; int msqid; key_t key; if ((key = ftok("kirk.c", 'B')) == -1) { perror("ftok"); exit(1); } if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) { perror("msgget"); exit(1); } printf("Enter lines of text, ^D to quit:\n"); buf.mtype = 1; /* we don't really care in this case */ while(gets(buf.mtext), !feof(stdin)) {

if (msgsnd(msqid, (struct msgbuf *)&buf, sizeof(buf), 0) == -1) perror("msgsnd"); } if (msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(1); } return 0;

ADVANCED UNIX PROGRAMMING LAB

38

#include #include #include #include #include #include

<stdio.h> <stdlib.h> <errno.h> <sys/types.h> <sys/ipc.h> <sys/msg.h>

struct my_msgbuf { long mtype; char mtext[200]; }; int main(void) { struct my_msgbuf buf; int msqid; key_t key; if ((key = ftok("kirk.c", 'B')) == -1) { /* same key as kirk.c */ perror("ftok"); exit(1); }

if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */ perror("msgget"); exit(1); } printf("spock: ready to receive messages, captain.\n"); for(;;) /* Spock never quits! */ 39

ADVANCED UNIX PROGRAMMING LAB

if (msgrcv(msqid, (struct msgbuf *)&buf, sizeof(buf), 0, 0) == -1) perror("msgrcv"); exit(1); } printf("spock: \"%s\"\n", buf.mtext); } return 0;

} Output:

[cse @unix API]$ CC mqcontrol.c [cse @unix API]$ ./a.out Uid and gid is 504 504 num of bytes in message queue is 0 num of message queues in memory is 0 max num of bytes per a message is 16384 pid of last process that sends a message 0 pid of last process that receives a message 0 time of last sent message 0 time of last receive message 0 change time for last modified message 1144321778

ADVANCED UNIX PROGRAMMING LAB

40

ADDITIONAL EXPERIMENTS

ADVANCED UNIX PROGRAMMING LAB

41

Experiment:1 Aim:

Write a shell script to check the user name on the command line and check that user is logged on to that system or not.

Source Code: while: do echo enter login id of user read x who | grep $x if [$? eq 0] then echothe user $x is in login state else echo user is logged off fi done Input: Enter login id of user ramesh Output: user ramesh in login state

ADVANCED UNIX PROGRAMMING LAB

42

Experiment:2 Aim: one

Write a program to print the sum of even numbers in process and sum of odd numbers in another process.

Source code: #include<stdio.h> int main() { int i,pid,suml=0,sum2=0,n; printf(enter n); scanf(%d,&n); pid=fork(); if(pid==0) { printf(child process:\n even numbers are \n); for(i=0;i<n;i++) { if((i%2)==0) { printf(%d\n,i); sum1=sum1+i; } } printf(sum of even numbers is %d\n,sum1); } else { printf(the parent process:\n the list of odd numbers:\n); ADVANCED UNIX PROGRAMMING LAB 43

for(i=0;i<n;i++) { if((i%1)!=0) { printf(%d\n,i); sum2=sum2+i; } } printf(sum of odd numbers is %d\n,sum2); } } printf(sum of odd(i%1)!=0) { printf(%d\n,i); sum2=sum2+i; } } printf(sum of odd numbers is %d\n,sum2); } } printf(sum of odd(i%1)!=0) { printf(%d\n,i); sum2=sum2+i; } } printf(sum of odd numbers is %d\n,sum2); } } }

ADVANCED UNIX PROGRAMMING LAB

44

Output: [cse@unix Process]$ cc evenodd.c [cse@unix process]$ ./a.out Enter n 10 Child process: Even numbers are 0 2 4 6 8 Sum of even numbers is 20 The parent process: The list of odd numbers: 1 3 5 7 9 Sum of odd numbers is 25.

ADVANCED UNIX PROGRAMMING LAB

45

Experiment:3 AIM:

Write a shell script to accept the 6 subjects marks and 2 subjects lab marks and print whether he is failed (or) passed. It passed then find distribution, first class (or) 2nd class (or) 3rd class.
Source Code: echo enter student name read x echo enter roll no: read y echo enter student 6 subjects marks i=0 sum=0 while[$i-ne 6] do read n if[$n-le 0-0 $n-gt 100] then echoinvalid marks exit else if[$n-le 40] then echo$x is failed exit else sum=expr $sum &$n fi fi i=expr $ i + 1 ADVANCED UNIX PROGRAMMING LAB 46

done sum x=0 j=0 echoenter lab marks while[$j-ne 2] do read l if[$l-le 0 -o $l gt 75] then echo invalid exit else if[$l le 30] then echo $x is failed exit else sumx=expr $sumx+ $l fi fi j=expr $j+i done

sumy=expr $sum f $sumx echo total=$sumy percent=lexpr &sumy \*100 percent=lexpr $percent /750 echo[$percent -gt70] then echo $x got distinction else if[$percent le 70 a $percent gt 60] then ADVANCED UNIX PROGRAMMING LAB 47

echofirst class else if[$percentage -le 60 a $percent gt 50] then echosecond class else echothird class fi fi fi

Output: Total=574 Percent=76.53 Sanjay got distinction

ADVANCED UNIX PROGRAMMING LAB

48

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy