Developed By R.SRINIVAS Asst.Prof

VIVEKANANDA INSTITUTE OF TECHNOLOGY & SCIENCE [College Code: N9] Opp: Housing Board Colony, By-pass Road, KARIMNAGAR. LINUX PROGRAMMING LAB MAN UAL ...
Author: Andra Lester
0 downloads 0 Views 501KB Size
VIVEKANANDA INSTITUTE OF TECHNOLOGY & SCIENCE [College Code: N9] Opp: Housing Board Colony, By-pass Road, KARIMNAGAR.

LINUX PROGRAMMING LAB MAN UAL

Developed By R.SRINIVAS Asst.Prof

Department of Computer Science & Engineering & Information Technology

[email protected]

1

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:” [email protected]

2

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 [email protected]

3

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" [email protected]

4

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" [email protected]

5

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 [email protected]

6

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

[email protected]

7

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

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

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

[email protected]

8

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 standard I/O and system calls.

makes

a

copy

of

Vi week10.c #include #include 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); } [email protected]

9

a

file

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

Implement in C the following Unix commands using System calls a) cat 11.

b) Is c) mv vi week11.c #include #include #include #include #include #include #include int main(int stdin,int stdout) { [email protected]

10

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:

Write a program that takes one or more file/directory names as command line input and reports the following information on the file. 12.

File type Number of links Time of last access Read, Write and Execute permissions. Vi week12.c #include #include #include #include int main(int argc,char *argv[]) { DIR *dp; struct dirent *d; [email protected]

11

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

Write a C Program to list for every file in a directory, its inode number and file name. 14.

vi week14.c #include #include #include #include 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) { [email protected]

12

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 redirection of

C

Program

that

demonstrates

#include #include [email protected]

13

#include #include 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); standard output*/

/*copy

the

file

descriptor

fd

into

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 [email protected]

14

-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 #include #include #include 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());

[email protected]

15

} 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 #include #include 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 #include #include int main(int argc,char *argv[]) { int fd[2],pid,k; k=pipe(fd); if(k==-1)

[email protected]

16

{ 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 -rw-rw-r-- 1 07501 07501 -rw-rw-r-- 1 07501 07501 -rw-rw-r-- 1 07501 07501 -rw-rw-r-- 1 07501 07501 -rw-rw-r-- 1 07501 07501 -rw-rw-r-- 1 07501 07501 -rw-rw-r-- 1 07501 07501 -rw-rw-r-- 1 07501 07501 -rwxrwxrwx 1 07501 07501 -rwxrwxrwx 1 07501 07501 total 11 ---x--x--x 1 07501 07501 [07501@localhost ~]$

513 575 614 633 772 87 899 900 948 0 178

Oct Oct Oct Oct Oct Sep Oct Oct Oct Oct Oct

9 16 20 20 20 13 20 9 20 8 11

4 Aug

1

11:22 12:12 11:37 11:15 10:21 2011 10:42 14:29 10:27 14:23 11:47

week10.sh week15.c week24.c week28.c week21.c file.sh week27client.c client.c week22.c week1.sh week6.sh

2008 ab

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 #include #include #include #include #include int main(int argc,char* argv[]) {

[email protected]

17

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.

[email protected]

18

Vi week23.c

#include #include #include #include #include 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 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 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); [email protected]

19

} Output:

[07501@localhost ~]$ cc week22.c [07501@localhost ~]$ ./a.out Press to try to get lock: Trying to get lock...got lock Press 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 #include #include #include #include #include #include #include #include #include #include #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;i--) temp[a++]=fname[i]; temp[a]='\0'; printf("\n Rev is %s \n",temp); }

vi client.c #include #include #include #include #include #include #include #include #include #include #include #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