Professional Documents
Culture Documents
Write a shell script for sorting, searching, insertion, deletion and displaying of elements in a list. Program #!/bin/sh clear i=1 echo Enter the size of array : read n echo Enter $n elements : while [ $i -le $n ] do read a[$i] i=`expr $i + 1` done while true do i=1 j=1 clear echo 1. Sort echo 2. Search echo 3. Insert echo 4. Delete echo 5. Display echo 6. Exit echo Enter your choice : read ch case $ch in 1) while [ $i -le $n ] do j=1 while [ $j -le $n ] do if [ ${a[$i]} -lt ${a[$j]} ] then t=${a[$i]} a[$i]=${a[$j]} a[$j]=$t fi j=`expr $j + 1` done i=`expr $i + 1` done ;; 2) echo Enter the element to search : flag=0 read e while [ $i -le $n ] do if [ $e -eq ${a[$i]} ] then flag=1
Page No : 1
Page No : 2
OUTPUT Enter the size of array : 5 Enter 5 elements : 65 34 27 12 53 1.Sort 2.Search 3.Insert 4.Delete 5.Display 6.Exit Enter your choice : 1.Sort 2.Search 3.Insert 4.Delete 5.Display 6.Exit Enter your choice : 12 27 34 53 65
Page No : 3
Page No : 4
Page No : 5
Now execute the shell script by typing sh shell script filename samp1 samp2 at shell command prompt. After executing see the contents of the files samp1 and samp2. You will observe that the lines containing the word unix will be removed. $>cat samp1 hello this is C $> cat samp2 C
java
.net
Write a shell script which displays a list of all files in the current working directory to which we have read, write and execute permissions PROGRAM #!/bin/sh clear echo "The files which are having read, write and execute permissions are" for i in * do if [ -r $i -a -w $i -a -x $i ] then echo $i fi done OUTPUT First give read, write and execute permissions for some files using chmod and then execute the shell script to get the output The files which are having read, write and execute permissions are a b
Page No : 6
Page No : 7
Page No : 8
Page No : 9
Page No : 10
Page No : 11
Page No : 12
4)
*) esac
Write a shell script that accepts two integers as its arguments and computes the value of first number raised to the power of the second number PROGRAM #!/bin/sh clear if [ $# -ne 2 ] then clear echo "This script requires two integers as arguments" echo "Usage : script_name <n1> <n2>" exit 1 fi i=1 result=1 while [ $i -le $2 ] do result=`expr $result \* $1` i=`expr $i + 1` done echo "$1 raised to the power of $2 is $result"
Page No : 13
Page No : 14
Page No : 15
OUTPUT Enter N value : 10 Child Process is calculating the sum of even numbers between 1 to 10 Even numbers sum is 30 Parent Process is calculating the sum of odd numbers between 1 to 10 Odd numbers sum is 25
Page No : 16
Page No : 17
Page No : 18
Page No : 19
Page No : 20
Then come out from the subdirectory and execute the program $> ./countlinks <subdirectory name> eg. $> ./countlinks samp
After executing it displays the files which r having more than one link in the following style -----------------------------------------------------------------------------------------
Page No : 21
Page No : 22
Page No : 23
Page No : 24
OUTPUT SEGMENTATION PROBLEM SIGNAL NO IS : 8 Write a program which demonstrates the shared memory functions PROGRAM SHM_PROCESS1 #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #define SHMSZ 27 main() { char c; int shmid; key_t key; char *shm, *s; key = 5678;
Page No : 25
Page No : 26
Page No : 27
Page No : 28
Write a program demonstrating semaphore operation on a shared file for reading but not writing PROGRAM #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<fcntl.h> #include<unistd.h> #include<sys/sem.h> #include<sys/ipc.h> #include<unistd.h> int main(int argc, char *argv[]) { struct flock fl; int fd, ch, semid, sc; struct sembuf sem_op; fl.l_whence=SEEK_SET; fl.l_start=0; fl.l_len=0; fl.l_pid=getpid(); sem_op.sem_num=0; sem_op.sem_flg=0; if(argc!=2) { printf("\n Usage : %s <filename>",argv[0]); exit(0); } if((semid=semget(998,1,IPC_CREAT | 0666))==-1) { printf("\n Error in semophore"); exit(1);
Page No : 29
Page No : 30
Page No : 31
Write a C program that takes one or more file or directory names as command line input and reports the following information on the file. File type Number of links Read, write and execute permissions Time of last access (Note: use stat/fstat system calls) PROGRAM #include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<string.h> #include<stdlib.h> int main(int argc, char *argv[]) { int i=1; char ftype[100]; struct stat StatusBuffer; if(argc<2) { printf("\n Arguments not specified properly \n"); printf("\n Usage : filename <file_list>\n\n"); exit(1); } while(i<argc) { if((lstat(argv[i],&StatusBuffer))==-1) { printf("\n ----- %s Invalid Filename specified ----- \n",argv[i]); i++; continue; } printf("\n %s - File Status Information",argv[i]); printf("\n ------------------------------------------------\n"); if(S_ISREG(StatusBuffer.st_mode)) strcpy(ftype,"Regular File"); else if(S_ISDIR(StatusBuffer.st_mode)) strcpy(ftype,"Directory File"); else if(S_ISLNK(StatusBuffer.st_mode)) strcpy(ftype,"Link File"); else strcpy(ftype,"Special File or Unknown File"); printf("\n File Type : %s",ftype); printf("\n I-node no of the file : %ld",StatusBuffer.st_ino); printf("\n Permission mode : %o",StatusBuffer.st_mode & 0777); printf("\n No of Links to the file : %ld",StatusBuffer.st_nlink); printf("\n size of the file : %ld",StatusBuffer.st_size); printf("\n Last access time : %ld",StatusBuffer.st_atime); printf("\n"); i++; }} out put:
Page No : 32
Write C program that simulate the mv command PROGRAM #include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<stdlib.h> int main(int argc, char *argv[]) { int i=1; struct stat StatusBuffer; if(argc!=3) { printf("\n Arguments not specified properly \n"); printf("\n Usage : filename <source> <target>\n\n"); exit(1); } if((lstat(argv[1],&StatusBuffer))==-1) { printf("\n Invalid source Filename specified \n",argv[1]); exit(2); } if(link(argv[1],argv[2])==-1) { printf("\n Error While Moving"); exit(3); } else if(unlink(argv[1])==-1) { unlink(argv[2]); printf("\n Error While Moving"); exit(4); } else printf("\n File Moved or Renamed Sucessfully"); }
Page No : 33
Write C program that simulate the cp command PROGRAM #include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<stdlib.h> #include<fcntl.h> #define SIZE 1024 #define MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) int main(int argc, char *argv[]) { int srcfd,dstfd; int nbytes; char buff[SIZE]; if(argc!=3) { printf("\n Arguments not specified properly \n"); printf("\n Usage : filename <source> <target>\n\n"); exit(1); } if((srcfd=open(argv[1],O_RDONLY))==-1) { printf("\n Invalid Filename or File cannot open"); exit(2); } if((dstfd=creat(argv[2],MODE))==-1) { printf("\n Error while creating destination file"); close(srcfd); exit(3); } while((nbytes=read(srcfd,buff,SIZE))>0) { write(dstfd,buff,nbytes); } close(dstfd); close(srcfd); printf("\n File Copied Sucessfully \n\n"); } OUTPUT
Page No : 34
Write a c program that simulates ls command (Use system calls / directory API) PROGRAM #include<stdio.h> #include<stdio.h> #include<dirent.h> #include<unistd.h> #include<sys/types.h> int main() { size_t sz; char pathname[100]; struct dirent *DirectoryEntry; DIR *DirectoryStream; int i=0; getcwd(pathname,sz); printf("%s\n",pathname); if((DirectoryStream=opendir(pathname))==NULL) { printf("\n Error while opening directory"); exit(1); } while((DirectoryEntry=readdir(DirectoryStream))) { printf("\n %s",DirectoryEntry->d_name); } closedir(DirectoryStream); }
OUTPUT
Page No : 35