Shell Script
Shell Script
Shell Script
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 Write a program to display the 'Good Morning', 'Good After Noon', 'Good Evening' and 'Good Night' depending on the users log on time PROGRAM
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
Page No : 6
Page No : 7
Write a shell script for renaming each file in the directory such that it will have current shell id as and extension. The shell script should ensure that the directories do not get renamed. PROGRAM #!/bin/sh clear for i in * do if [ -f $i ] then echo ;mv $i $i.$$ fi done OUTPUT First create some files with the name a b c d and directories x y z in a sub folder(eg sample) of current working directory. Then execute the shell script by passing the sub folder(sample) as an argument to the shell script. After execution you will find all the files present in the sub folder will get extension of the current shell id. $ sample>ls a.19647 b.19647 c.19647 d.19647 x y z
Page No : 8
Page No : 9
Page No : 10
Page No : 11
Page No : 12
*) 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
Page No : 16
Page No : 17
Page No : 18
Page No : 19
Page No : 20
} printf(" %s ",file[i]); if(ino[i]!=ino[i+1]) printf("\t\t\t\t\t %ld ",size[i]); } } printf("\n -------------------------------------------------------------------------------------- \n"); exit(0); } OUTPUT First create some files with the names a b c d sample in any subdirectory of current working directory. Then enter into subdirectory and use ln command at shell prompt to provide links for the files $> ln a x $> ln a y $> ln c z $> sample prog $ ln b bb ....
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 ----------------------------------------------------------------------------------------Inode Files which r having more than one link size in bytes ---------------------------------------------------------------------------------------1867903 y a x 6 1867904 bb b bbb bbbb 12 1867905 z c 14 1867906 prog sample 0 --------------------------------------------------------------------------------------Write a program to demonstrating the use of exec family functions PROGRAM PRGEXEC.C #include<stdio.h> #include<unistd.h> char *env_init[]={"user=kiran", "PATH=/tmp", NULL}; char *argarray[]={"dispprg","myarg1","myarg2","myarg3","myarg4"}; main() { int pid; if( (pid=fork()) < 0 ) printf("\n Error while creating process"); else if(pid==0) { printf("\n -----------------------------------------------------------------"); printf("\n EXECL "); printf("\n------------------------------------------------------------------"); if( execl("/home/kiran/dispprg","dispprg","myarg1","myarg2",(char *) 0) < 0 ) printf("\n Error in Execl"); } if(waitpid(pid,NULL,0) < 0)
Page No : 21
Page No : 22
Page No : 23
Page No : 24
Page No : 25
Page No : 26
Page No : 27
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;
Page No : 28
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: 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"); }
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
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
/* tcpserver.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> int main() { int sock, connected, bytes_recieved , true = 1; char send_data [1024] , recv_data[1024]; struct sockaddr_in server_addr,client_addr; int sin_size; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket"); exit(1); } if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) { perror("Setsockopt"); exit(1); } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(5000); server_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server_addr.sin_zero),8); if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1) { perror("Unable to bind"); exit(1); } if (listen(sock, 5) == -1) { perror("Listen"); exit(1); } printf("\nTCPServer Waiting for client on port 5000"); fflush(stdout); while(1) { sin_size = sizeof(struct sockaddr_in); connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size); printf("\n I got a connection from (%s , %d)", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); while (1) { printf("\n SEND (q or Q to quit) : "); gets(send_data); if (strcmp(send_data , "q") == 0 || strcmp(send_data , "Q") == 0)
{ send(connected, send_data,strlen(send_data), 0); close(connected); break; } else send(connected, send_data,strlen(send_data), 0); bytes_recieved = recv(connected,recv_data,1024,0); recv_data[bytes_recieved] = '\0'; if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0) { close(connected); break; } else printf("\n RECIEVED DATA = %s " , recv_data); fflush(stdout); } } close(sock); return 0; } /* tcpclient.c */ #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> int main() { int sock, bytes_recieved; char send_data[1024],recv_data[1024]; struct hostent *host; struct sockaddr_in server_addr; host = gethostbyname("127.0.0.1"); if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket"); exit(1); } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(5000); server_addr.sin_addr = *((struct in_addr *)host->h_addr); bzero(&(server_addr.sin_zero),8); if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("Connect"); exit(1);
} while(1) { bytes_recieved=recv(sock,recv_data,1024,0); recv_data[bytes_recieved] = '\0'; if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0) { close(sock); break; } else printf("\nRecieved data = %s " , recv_data); printf("\nSEND (q or Q to quit) : "); gets(send_data); if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0) send(sock,send_data,strlen(send_data), 0); else { send(sock,send_data,strlen(send_data), 0); close(sock); break; } } return 0; }