vivveekkaanannddaa tiinnsstiittuuttee...
TRANSCRIPT
VV II VV EE KK AA NN AA NN DD AA II NN SS TT II TT UU TT EE OO FF TT EE CC HH NN OO LL OO GG YY && SS CC II EE NN CC EE
[College Code: N9]
Opp: Housing Board Colony, By-pass Road, KARIMNAGAR.
Developed By
R.SRINIVAS
Asst.Prof
LLIINNUUXX PPRROOGGRRAAMMMMIINNGG LLAABB MMAANN UUAALL
Department of
Computer Science & Engineering
&
Information Technology
1. Write a shell script that accepts a file name, starting and ending line numbers as arguments and displays all the lines
between the given line numbers.
Vi week1.sh
echo “enter the file name:”
read file
if [ -f $file ]
then
echo “enter starting line number:”
read snum
echo “enter ending line number:”
read enum
if [ $snum –lt $enum ]
then
echo “the selected lines from $snum line to $enum line
in $file:”
sed –n „„$snum‟,„$enum‟‟p‟‟ $file
else
echo “enter proper starting & ending line numbers:”
fi
else
echo “the file `$file` doesnot exists”
fi
output:
cat student abcd efgh ijkl mnop qrst
sh number.sh
enter the file name:student
enter starting line number:
2
enetr ending line number:
4
the selected lines from 1 line to 5 line in student:
efgh
ijkl
mnop
2. Write a shell script that deletes all lines containing a
specified word in one or more files supplied as arguments to
it.
Vi week2.sh
if [ $# -eq 0 ]
then
echo "No arguments"
else
echo "enter a deleting worg or char"
read y
for i in $*
do
grep -v "$y" "$i" > temp
if [ $? ne 0 ] then
echo "pattern not found." else cp temp $i rm temp fi done fi
OUTPUT:
[071216@localhost lp]$cat>a
welcome
to
lab
[071216@localhost lp]$ sh week2.sh s
enter a deleting worg or char
welcome
s
[071216@localhost lp]$ cat a
to
lab
3. Write a shell a script that displays a list of all the files in the current directory to which the user has read, write
and execute permissions.
echo "the list of file names in current director"
echo "which have read, write & execute permissions"
for file in *
do
if [ -f $file ]
then
if [ -r $file -a -w$file ]
then
ls -l $file
fi
else
echo "file not exit"
fi
done
OUTPUT:
[071216@localhost lp]$ sh week3.sh
the list of file names in current director
which have read, write & execute permissions
-rw-rw-r-- 1 071216 071216 0 Oct 20 09:43 a
-rw-rw-r-- 1 071216 071216 7 Oct 20 09:43 s
-rw-rw-r-- 1 071216 071216 220 Oct 20 09:41 week2.sh
-rw-rw-r-- 1 071216 071216 221 Oct 20 09:47 week3.sh
4. Write a shell script that receives any number of file names as arguments checks if
every argument supplied is a file or a directory and reports accordingly. Whenever
the argument is a file, the number of lines on it is also reported.
Vi week4.sh
echo "enter a file name"
read file
lines=0
exec < $file
cat $file
while read line
do
lines=`expr $lines + 1`
done
echo "number of lines in. $file . is =" $lines
output:
[071216@localhost lp]$ sh week4.sh
enter a file name s
to
lab
number of lines in. s . is = 2
5. Write a shell script that accepts a list of file names as its arguments, counts and reports the occurrence of each
word that is present in the first argument file on other
argument files.
if [ $# -eq 0 ]
then
echo "no arguments"
else
tr " " "\n" < $1 > temp
shift
for i in $*
do
tr " " "\n" < $i > temp1
y='wc -l , temp'
j=1
while [ $j -le $y ]
do
x='head -n $j temp | tail -1'
c='grep -c "$x" temp1'
echo $x $c
j=`expr $j + 1`
done
done
fi
output
sh week5.sh s
cat s
ui
dsfg
gfh
tyrert
yuk
sdg
cat temp
ui
dsfg
gfh
tyrert
yuk
sdg
cat temp1
dfgh
hjhg
ser
fgfd
ui
ew
klo
qwvf
vghty
6. Write a shell script to list all of the directory files in a directory.
echo "enter the file name"
read fname
if test -f $fname
then
echo "$fname is file"
elif test -d $fname
then
cd "$fname"
ls -p | grep /
fi
output:
[071216@localhost lp]$ sh week6.sh
enter the file name
cd
lp1/
[071216@localhost lp]$ cd cd
[071216@localhost cd]$ ls
lp1 s
7. Write a shell script to find factorial of a given integer.
Vi week7.sh
echo "Enter the number:"
read n
fact=1
i=1
while [ $i -le $n ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo "Factorial:$fact"
output
sh week7.sh
Enter the number:
5
Factorial:120
8. Write an awk script to count the number of lines in a file
that do not contain vowels.
vowel=0
while IFS="\n" read line
do
length=${#line}
if [[ $length -eq 1 ]] ; then
continue
fi ;
new_line=$(echo "$line" | tr -d 'aeiouAEIOU')
new_length=${#new_line}
vowel=$(($vowel + $length - $new_length))
done < input.txt
echo Vowels=$vowel
OUTPUT:
cat input.txt
welcome to lab
sh week8.sh
Vowels=5
9.Write an awk script to find the number of characters, words
and lines in a file. $awk '{ x+=length($0); y+=NF} END{ print NR, x, y }' s
OUTPUT
6 21 6
cat s
ui
dsfg
gfh
tyrert
yuk
sdg
it display “no of words,no of characters, no of lines” as output
10. Write a C program that makes a copy of a file
standard I/O and system calls.
Vi week10.c
#include<stdio.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
int source,destination,n;
char buf[256];
if(argc!=3)
{
printf("No sufficient Arguments\n");
return(1);
}
source=open(argv[1],O_RDONLY);
if(source==-1)
{
perror(argv[1]);
return(1);
}
destination=open(argv[2],O_WRONLY|O_CREAT,0640);
if(destination==-1)
{
perror(argv[2]);
return(1);
}
OUTPUT
cc week10.c
./a.out s p
s succesfully copied to p
cat s
ui
dsfg
gfh
tyrert
yuk
sdg
cat p
ui
dsfg
gfh
tyrert
yuk
sdg
11. Implement in C the following Unix commands using System
calls
a) cat
b) Is
c) mv
vi week11.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<fcntl.h>
#include<dirent.h>
#include<sys/stat.h>
int main(int stdin,int stdout)
{
int fd,fd1,rc;
char ch[5];
fd1=open("sss",O_WRONLY | O_CREAT);
while((rc=read(stdin,ch,5))>0)
write(fd1,ch,rc);
close(fd1);
fd1=open("sss",O_RDONLY);
while((rc=read(fd1,ch,5))>0)
write(stdout,ch,rc);
close(fd1);
}
output:
12. Write a program that takes one or more file/directory names
as command line input and reports the following information on
the file.
File type
Number of links
Time of last access
Read, Write and Execute permissions.
Vi week12.c
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(int argc,char *argv[])
{
DIR *dp;
struct dirent *d;
if(((dp=opendir("sss")))==NULL)
printf("cannodt open\n");
while((d=readdir(dp))!=NULL)
{
printf("%s\t",d->d_name);
printf("%d\n",d->d_ino);
}
closedir(dp);
return(0);
}
OUTPUT
cc week12.c
./a.out
. 8222852
.. 8189131
14. Write a C Program to list for every file in a directory, its
inode number and file name.
vi week14.c
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
DIR *d;
struct stat buf;
struct dirent *de;
d=opendir(".");
if(d==NULL)
{
perror("DIR");
return (1);
}
printf("Files and their respective inodes no's in the Current directory are \n");
while((de=readdir(d))!=NULL)
{
stat(de->d_name,&buf);
printf("%10d - %s\n",buf.st_ino,de->d_name);
}
OUTPUT:
cc week14.c ./a.out Files and their respective inodes no's in the Current directory are 8195349 - input.txt 8195361 - week3.sh 8195338 - week11.c 8195391 - week15.c 8195321 - wee8.awk 8195262 - week1.sh 8195355 - week13.c 8195371 - week10.c 8195408 - week7.sh 8195416 - week9.sh 8195374 - week4.sh 8195396 - week28.c 8195411 - week12.c 8195402 - week10.sh 8195360 - week8.sh 8195336 - week14.sh 8195373 - week100.sh 8195310 - week6.sh 8195370 - week18.c 8195401 - week21.c 8195389 - week2.sh 8195398 - week22.c 8195420 - week14.c 8195386 - week17.c 8195277 - fact.sh 8195393 - week16.c 3339289 - filename.l 8191470 - ex.s 8195388 - wek8.sh
15. standard output to a file
a. EX:Is>fi Write a C Program that demonstrates
redirection of
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd; /*file descriptor to the file we will redirect ls's
output*/
if((fd = open("dirlist.txt", O_RDWR | O_CREAT))==-1){ /*open
the file */
perror("open");
return 1;
}
dup2(fd,STDOUT_FILENO); /*copy the file descriptor fd into
standard output*/
dup2(fd,STDERR_FILENO); /* same, for the standard error */
close(fd); /* close the file descriptor as we don't need it
more */
/*execl ls */
execl( "/bin/ls" , "ls" , "-la" , (char *) 0 );
return 0;
}
OUTPUT
cc week15.c ./a.out cat dirlist.txt total 1612 -rw-rw-r-- 1 07501 07501 0 Sep 30 2011 ; drwxr-xr-x 32 07501 07501 4096 Oct 16 12:09 . drwxr-xr-x 618 root root 20480 Jul 3 12:17 .. -rw-rw-r-- 1 07501 07501 0 Oct 16 11:55 weeek11.c -rw-rw-r-- 1 07501 07501 27 Oct 16 11:48 weeek66.sh
-rw-rw-r-- 1 07501 07501 515 Oct 9 11:32 week100.sh -rw-rw-r-- 1 07501 07501 513 Oct 16 11:16 week10.c -rw-rw-r-- 1 07501 07501 513 Oct 9 11:22 week10.sh -rw-rw-r-- 1 07501 07501 412 Oct 11 11:28 week11.c -rw-rw-r-- 1 07501 07501 336 Oct 9 12:19 week12.c -rw-rw-r-- 1 07501 07501 294 Oct 6 14:41 week13.c -rw-rw-r-- 1 07501 07501 417 Oct 9 12:34 week14.c -rw-rw-r-- 1 07501 07501 415 Oct 6 14:34 week14.sh -rw-rw-r-- 1 07501 07501 575 Oct 16 12:09 week15.c -rwxrwxrwx 1 07501 07501 449 Oct 6 14:55 week16.c -rw-rw-r-- 1 07501 07501 201 Oct 11 11:29 week17.c -rw-rw-r-- 1 07501 07501 254 Oct 6 15:02 week18.c -rw-rw-r-- 1 07501 07501 405 Oct 11 11:33 week19.c -rwxrwxrwx 1 07501 07501 0 Oct 8 14:23 week1.sh -rw-rw-r-- 1 07501 07501 393 Oct 9 12:52 week20a.c -rw-rw-r-- 1 07501 07501 375 Oct 9 12:52 week20b.c -rw-rw-r-- 1 07501 07501 0 Oct 11 11:35 week20.c -rw-rw-r-- 1 07501 07501 772 Oct 9 12:55 week21.c -rw-rw-r-- 1 07501 07501 949 Oct 11 11:39 week22.c -rw-rw-r-- 1 07501 07501 631 Oct 9 14:27 week28.c -rw-rw-r-- 1 07501 07501 139 Oct 8 14:23 week2.sh -rw-rw-r-- 1 07501 07501 211 Oct 9 10:56 week3.sh -rw-rw-r-- 1 07501 07501 233 Oct 9 12:04 week4.sh -rw-rw-r-- 1 07501 07501 254 Oct 16 10:36 week5.sh -rw-rw-r-- 1 07501 07501 30 Oct 16 11:35 week66.sh -rwxrwxrwx 1 07501 07501 178 Oct 11 11:47 week6.sh -rw-rw-r-- 1 07501 07501 135 Oct 6 16:04 week7.sh -rw-rw-r-- 1 07501 07501 276 Oct 16 11:15 week8.sh -rw-rw-r-- 1 07501 07501 0 Oct 9 12:13 week9.sh -rw-rw-r-- 1 07501 07501 0 Oct 4 11:37 wek8.sh
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. Vi week16.c
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main( )
{
int pid;
printf("original process with pid %d ppid %d\n",
getpid(),getppid());
pid=fork();
if(pid!=0)
printf("parent process with pid %d ppid %d \n",getpid(),getppid());
else
{
sleep(5);
printf("child process with pid %d ppid %d\n",getpid(),getppid());
}
printf("pid %d terminates",getpid());
}
Output:
[07501@localhost ~]$ vi week16.c
[07501@localhost ~]$ cc week16.c
[07501@localhost ~]$ ./a.out
original process with pid 22895 ppid 3300
parent process with pid 22895 ppid 3300
pid 22895 terminates[07501@localhost ~]$ child process with pid 22896 ppid 1
pid 22896 terminates
17 . Write a C Program to create a Zombie process
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
pid_t child_pid;
child_pid=fork();
if (child_pid > 0) {
sleep(15); }
else {
return(0);
}
return(0);
}
output
cc week17.c
./a.out
19. Write a C Program that illustrates how to execute two commands concurrently with a
command pipe. Ex: ls-l|sort.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int fd[2],pid,k;
k=pipe(fd);
if(k==-1)
{
perror("pipe");
return(1);
}
pid=fork();
if(pid==0)
{
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp(argv[1],argv[1],NULL);
perror("execl");
}
else
{
wait(2);
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
execlp(argv[2],argv[2],NULL);
perror("execl");
}
}
output:
-rw-rw-r-- 1 07501 07501 513 Oct 9 11:22 week10.sh
-rw-rw-r-- 1 07501 07501 575 Oct 16 12:12 week15.c
-rw-rw-r-- 1 07501 07501 614 Oct 20 11:37 week24.c
-rw-rw-r-- 1 07501 07501 633 Oct 20 11:15 week28.c
-rw-rw-r-- 1 07501 07501 772 Oct 20 10:21 week21.c
-rw-rw-r-- 1 07501 07501 87 Sep 13 2011 file.sh
-rw-rw-r-- 1 07501 07501 899 Oct 20 10:42 week27client.c
-rw-rw-r-- 1 07501 07501 900 Oct 9 14:29 client.c
-rw-rw-r-- 1 07501 07501 948 Oct 20 10:27 week22.c
-rwxrwxrwx 1 07501 07501 0 Oct 8 14:23 week1.sh
-rwxrwxrwx 1 07501 07501 178 Oct 11 11:47 week6.sh
total 11
---x--x--x 1 07501 07501 4 Aug 1 2008 ab
[07501@localhost ~]$
21. Write a C Program to create a message queue with read and write permissions to write 3 messages to it with different priority numbers.
Vi week21.c
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
#include<string.h>
#include<stdlib.h>
int main(int argc,char* argv[])
{
int q_id=msgget(IPC_PRIVATE,0600);
perror("msgget");
struct msgbuf
{
long mtype;
char mtext[20];
};
struct msgbuf* msg;
struct msgbuf* recv_msg;
int rc;
if(q_id==-1)
{
perror("main:msgget");
return(1);
}
printf("message queue created %d\n",q_id);
msg=(struct msgbuf*)malloc(sizeof(struct msgbuf)+strlen("hello world"));
msg->mtype=1;
strcpy(msg->mtext,"helloworld");
rc=msgsnd(q_id,msg,strlen(msg->mtext)+1,0);
if(rc==-1)
{
perror("main:msgsend");
return(1);
}
free(msg);
printf("message placed on the queue successfully");
recv_msg=(struct msgbuf*)malloc(sizeof(struct msgbuf) +strlen("hello world"));
rc=msgrcv(q_id,recv_msg,strlen("helloworld")+1,0,0);
if(rc==-1)
{
perror("main:msgrcv");
return(1);
}
printf("msgrcv:received message:\nmtype'%d'\n;mtext'%s'\n",recv_msg->mtype,recv_msg-
>mtext);
return(0);
}
output:
[07501@localhost ~]$ cc week211.c
week211.c: In function âmainâ:
week211.c:25: warning: incompatible implicit declaration of built-in function âmallocâ
[07501@localhost ~]$ vi week211.c
[07501@localhost ~]$ cc week211.c
[07501@localhost ~]$ ./a.out
msgget: Success
message queue created 0
message placed on the queue successfullymsgrcv:received message:
mtype'1'
; mtext'helloworld'
22&23.Write a C Program to allow cooperating processes to lock a resource for exclusive use, using a)Semaphors b)flock or lockf system calls.
Vi week23.c
#include <stdio.h> #include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
/* l_type l_whence l_start l_len l_pid */
struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0 };
int fd;
fl.l_pid = getpid();
if (argc > 1)
fl.l_type = F_RDLCK;
if ((fd = open("lockdemo.c", O_RDWR)) == -1) {
perror("open");
return(1);
}
printf("Press <RETURN> to try to get lock: ");
getchar();
printf("Trying to get lock...");
if (fcntl(fd, F_SETLKW, &fl) == -1) {
perror("fcntl");
return(1);
}
printf("got lock\n");
printf("Press <RETURN> to release lock: ");
getchar();
fl.l_type = F_UNLCK; /* set to unlock same region */
if (fcntl(fd, F_SETLK, &fl) == -1) {
perror("fcntl");
return(1);
}
printf("Unlocked.\n");
close(fd);
return(0);
}
Output:
[07501@localhost ~]$ cc week22.c
[07501@localhost ~]$ ./a.out
Press <RETURN> to try to get lock:
Trying to get lock...got lock
Press <RETURN> to release lock:
Unlocked.
26 &27 rite client and server programs (using c) for interaction between server and client processes using
Unix Domain sockets. Vi server.c
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<unistd.h>
#define MYPORT 2400
void readstring(int ,char *);
int main(int c,char *v[])
{
int listensocket,connectionsocket,retbind,len;
struct sockaddr_in serveraddress,cliaddr,
socklen_tlen;
char buf[100],databuf[1024];
listensocket=socket(AF_INET,SOCK_STREAM,0);
if(listensocket<0)
{
perror("socket");
exit(1);
}
memset(&serveraddress,0,sizeof(serveraddress));
serveraddress.sin_family=AF_INET;
serveraddress.sin_port=htons(MYPORT);
serveraddress.sin_addr.s_addr=htonl(INADDR_ANY);
retbind=bind(listensocket,(struct sockaddr*)&serveraddress,sizeof(serveraddress));
if(-1==retbind)
{
perror("BIND ERROR\n");
exit(1);
}
listen(listensocket,1);
for(;;)
{
printf("Server:I am waiting....Start of Main Loop\n");
len=sizeof(cliaddr);
connectionsocket=accept(listensocket,(struct sockaddr*)&cliaddr,&len);
if(connectionsocket<0)
{
if(errno==EINTR)
printf("Interrupted system call??");
continue;
}
printf("Connection from %s\n",inet_ntop(AF_INET,&cliaddr.sin_addr,buf,sizeof(buf)));
readstring(connectionsocket,databuf);
close(connectionsocket);
printf("Finished serving one client \n");
}
}
void readstring(int connectionsocket,char *fname)
{
int pointer=0,n;
int len=0,a,b;
char rev[50],temp[50],temp1[50];
int k,i;
while((n=read(connectionsocket,(fname+pointer),1024))>0)
{
pointer=pointer+n;
}
fname[pointer]='\0';
printf("Enter the string\n");
printf("Server:Received %s \n",fname);
k=strlen(fname);
a=0;
for(i=k-1;i>=0;i--)
temp[a++]=fname[i];
temp[a]='\0';
printf("\n Rev is %s \n",temp);
}
vi client.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#define MAXBUFFER 1024
void sendstring(int,char*);
int main(int C,char *v[])
{
int sd,fd;
char c;
struct sockaddr_in serveraddress;
char text[100];
int i=0;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror("socket");
exit(1);
}
if(v[1]==NULL)
{
printf("Please specify the server's IP address\n");
exit(0);
}
if(v[2]==NULL)
{
printf("Please specify the server's Port No.\n");
exit(0);
}
//if(v[3]==NULL)
//{
//printf("Please specify the string to be sent to the server\n");
//exit(0);
//}
memset(&serveraddress,0,sizeof(serveraddress));
serveraddress.sin_family=AF_INET;
serveraddress.sin_port=htons(atoi(v[2]));
serveraddress.sin_addr.s_addr=inet_addr(v[1]);
if(connect(sd,(struct sockaddr*)&serveraddress,sizeof(serveraddress))<0)
{
printf("Cannot connect to server");
exit(1);
}
printf("enter sentence to end enter #");
while(1)
{
c=getchar();
if(c=='#')
break;
text[i++]=c;
}
text[i]='\0';
sendstring(sd,text);
close(sd);
return(0);
}
void sendstring(int sd,char *fname)
{
int n,byteswritten=0,written;
char buffer[MAXBUFFER];
strcpy(buffer,fname);
n=strlen(buffer);
while(byteswritten<n)
{
written=write(sd,buffer+byteswritten,(n-byteswritten));
byteswritten+=written;
}
printf("string:%s sent to server\n",buffer);
}
output
[071216@localhost ~]$ cc tcpserver.c
[071216@localhost ~]$ ./a.out
Server:I am waiting....Start of Main Loop
Received hi
Rev : ih
[071216@localhost ~]$ cc tcpserver.c
[071216@localhost ~]$ ./a.out 2400 192.168.2.1
enter sentence to end enter # hi #
hi is sent to server