Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
67 views

Aksingh@Fet-Mits Aksingh@Fet-Mits: Mba-Tech Cse 3 Year System Programming Lab

System Programming Laboratory Ajay Kumar Singh FET Mody Institute of Technolgy & Science Laxmangarh

Uploaded by

greataks
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Aksingh@Fet-Mits Aksingh@Fet-Mits: Mba-Tech Cse 3 Year System Programming Lab

System Programming Laboratory Ajay Kumar Singh FET Mody Institute of Technolgy & Science Laxmangarh

Uploaded by

greataks
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

MODY INSTITUTE OF TECHNOLOGY & SCIENCE, LAXMANGARH

FACULTY OF ENGINEERING & TECHNOLOGY

MBA-TECH CSE 3rd YEAR SYSTEM PROGRAMMING LAB


(Autumn Sem 2013-14)

AKSingh@FET-MITS

SUBMITTED TO: Mr. Ajay Kumar Singh Asst. Prof. FET

Submitted By: Divyanti Chauhan

UNIX BASIC COMMANDS

1. cat This is one of the most flexible Unix commands. We can use to create, display and concatenate files. It is used to merge the contents of two files and even merge already appended file. It is used in following 4 forms and syntax: To create a file SYNTAX: cat > filename E.g. cat >colors Typing this command will let you create a file in which you can write the contents simultaneously Then after writing the content, press CTRL+D to save the contents.

To simply view an already created file SYNTAX: cat filename E.g. cat colors Typing this command will display the contents of the file.

To merge the content of two files SYNTAX: cat file1 file2> new file E.g.cat> test1 (creates first file and write its content) cat> test2 (creates second file and write its content) cat test1 test2> test Typing this command will create a new file having the content of test1 followed by test2 in test file. To merge an already appended file SYNTAX: cat file1 file2>>new file E.g.cat test2 cat test test2>> test3 Typing this command will merge an already appended file test followed test2 in test3.

AKSingh@FET-MITS AKSingh@FET-MITS

DivyantiChauhan (110319)

Page 1

2. touchIt is used to create a ew e pty file. You ca t write a ythi g i the file si ulta eously. SYNTAX: touch filename E.g. touch test For multiple files: E.g. touch test1, test2, test3

3. dateTo show date and time of the system. SYNTAX: date<enter> Typing this command will show the date and time in a particular format. Fri Jan 6 08:52:42 MST 2013

4. calTo display the calendar of present month with the present day highlighted. SYNTAX: cal<enter> Typing this command will show the calendar as shown below.

AKSingh@FET-MITS
5. whoTo show the various connection being done on the UNIX remote server. SYNTAX: who<enter> Typing this command will generate a huge list of connections as shown below:

DivyantiChauhan (110319)

Page 2

6. lsTo list all the files and directories present on this particular path. SYNTAX: ls<enter> Typing this command will generate the names of the files and directories as shown below.

Another way to use ls is when you are not sure about the filename or directory name you can use two symbols with the characters of the file you are sure. ? - this symbol can replace a single character E.g. lssyst?m Typing this command will generate a list of all the files with the missing probable character such that other specified characters match.

* - this symbol can replace a string of characters E.g. ls sys* Typing this command will generate a list of all the files with the missing probable string such that other specified characters match.

7. rm

AKSingh@FET-MITS
To remove a file from the current path. SYNTAX: rm filename E.g. rm test1 Typing this command will remove the specified file permanently. rm can also be used in an interactive manner. SYNTAX: rm i filename E.g. rm i test1 Typing this command will assure interactively that whether you want to delete this file or not and then you have to write y or n for yes and no respectively as required.

8. mkdirTo make a new directory SYNTAX: mkdir filename E.g. mkdirsyslab

DivyantiChauhan (110319)

Page 3

9. cdTo change the directory path from current directory to a new directory SYNTAX: cd dirname E.g. cd syslab Typing this command will change the workspace from current directory to a new one and will show subsequent change in path line too. cd is also used to simply come back from any sub directory to the home directory. SYNTAX: cd<enter> Similarly if you want to come back from current directory to the home directory step wise one by one then we can use cd.. SYNTAX: cd..

10. mv

To rename a file as well as directory SYNTAX: mv oldnamenewname E.g. mv test1 tests

AKSingh@FET-MITS
To copy the contents of a file to another file. SYNTAX: cp file1 file2 E.g. cp tests student Typing this command will move the content of file1 in file2

mv can also be used to move a file from current directory to any other directory as specified. SYNTAX: mv filename dirname E.g. mv tests syslab

11. cp-

cp can also be used to copy a file from one directory to another directory. SYNTAX: cp filename dirname E.g. cp tests syslab Typing this command will copy tests file to the directory syslab also

DivyantiChauhan (110319)

Page 4

12. echo To display a string YNTAX: echo stri g or echo stri g E.g. echo my name is divyanti Typing this command will generate the above written string on the terminal. To display a value of a variable on the terminal SYNTAX: echo $varname E.g. a=10 echo $a Typing this command will show 10 as the output on the screen. 13. readTo take input from the user. SYNTAX: read varname E.g. read a Typing this command will give the space to the user to enter the value. ***In UNIX, no pre declaration of variables is required.

AKSingh@FET-MITS

DivyantiChauhan (110319)

Page 5

INTRODUCTION TO UNIX

Basic features of Unix: Multiple users to be connected simultaneously at a time. Multitasking system Security- secured file system

Basic structure of Unix operating system:

Application Program

Shell

Kernel

AKSingh@FET-MITS
Hardware

Kernel is the only program that can directly interact with the hardware. Shell is interpreters for instructions specifiedby application program. Kernel executes the instructions on hardware. Actual task is processed by hardware.

***We will work on shell level.


Types of shell: bourne shell- most popular C shell vi editor In vi editor we will be writing shell script. ***The extension of the program is .sh To run the file we simply write sh<filename>. Filename is specified with extension.

DivyantiChauhan (110319)

Page 6

SHELL SCRIPTS
AKSingh@FET-MITS

DivyantiChauhan (110319)

Page 7

AKSingh@FET-MITS

DivyantiChauhan (110319)

Page 8

AKSingh@FET-MITS AKSingh@FET-MITS

Command: ls -l 1st column file and directory permission - in start is File d in start is- directory r-read w-write x-execute next column is user id next column is group id next column is no of bytes file will contain next columns is last date modified and last time modified last column is the filename y11csmba15 11 is the no of link provided to that file -rw r- - r- 1st three bits- user permission

2nd three bits - group permission 3rd three bits- permission( in others group is not included) r-4 w-2 x-1 total 7 numeric weight permissions to change the permission of any particular user ch mode eg. We want owner group others 7 2 0 Ch mod 740 filename.sh

AKSingh@FET-MITS

1. Write Message can be sent from one UNIX user to other Syntax: Write <userid> Then ctrl d

2 prerequisites: - The user whom you are sending must be logged in else an error message will be generated - Secondly the user must have provided permission to send message to that user 2. mesg n

AKSingh@FET-MITS

To set the receiving permission as no to receive messages. 3. Finger command It is used to check which users are logged in. It is only for super user. 4. Who T Same as finger command for normal users. + denotes the users who permit to receive the message - Who dont permit pts/0 pts/1 etc they are terminal names

5. write (In case one user is logged in one more than one system) write <user id><terminal name>

AKSingh@FET-MITS

6. wall To send messages to all users even though the receiving permission is not availed. Meant for super users or administrators only. Syntax: Wall <Message> Ctrl d 7. mail To send mail to any UNIX user.

SYNTAX: mail <userid> Subject: _______________________

Ctrl+d For more than one user Mail <userid1><userid2>. 8. mail userid <prog.sh To send your program to another user as an attachment 9. mail and enter U- This symbol signifies the mail being sent when the recipient was logged in N- This symbol signifies the mail being sent when the recipient was not logged in Mail in front of which a greater than symbol is displayed is the recent one.

AKSingh@FET-MITS

****Press q to come out of mail display. 10. MAILCHECK TIME Syntax: MAILCHECK = Time in seconds 11. Mail f This will show the seen/ read mails. 12. After ? symbol d The current mail will be deleted.

AKSingh@FET-MITS

AKSingh@FET-MITS

PROGRAM-1
WRITE A SHELL SCRIPT TO PRINT YOUR NAME ON TERMINAL echo input your name read name echo your name is $name

OUTPUT: input your name divyanti your name is divyanti

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 1

PROGRAM-2
WRITE A SHELL SCRIPT TO PRINT THE SUM OF THE TWO NUMBERS echo input two number read a read b c=`expr $a + $b` echo sum is $c

OUTPUT: input two number 5 7 sum is 12

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 2

PROGRAM-3
WAP TO PRINT THE DIFFERENCE OF THE TWO NUMBERS

echo input two number read a read b c=`expr $a - $b` echo difference is $c

OUTPUT: input two number


8 5

AKSingh@FET-MITS

difference is 3

Divyanti Chauhan (110319)

Page 3

PROGRAM-4
WAP TO PRINT THE PRODUCT OF THE TWO NUMBERS

echo input two number read a read b c=`expr $a \* $b` echo product is $c

OUTPUT:
input two number 5 10

AKSingh@FET-MITS

product is 50

Divyanti Chauhan (110319)

Page 4

PROGRAM-5
WRITE A SHELL SCRIPT TO DISPLAY THE QUOTIENT OF TWO NUMBERS echo input two number read a read b c=`expr $a / $b` echo quotient is $c

OUTPUT: input two number


8 4 quotient is 2

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 5

PROGRAM-6
WRITE A SHELL SCRIPT TO FIND THE LARGEST AMONG THE THREE NUMBERS echo input the values of a,b,c read a read b read c if test $a -gt $c then { if test $a -gt $b then echo $a is largest elif test $b -gt $c then echo $b is largest fi }

AKSingh@FET-MITS

elif test $c -gt $b then fi OUTPUT: input the values of a,b,c 5 7 8 8 is largest
Divyanti Chauhan (110319) Page 6

echo $c is largest

PROGRAM-7
WRITE A SHELL SCRIPT TO CALCULATE THE PERCENTAGE OF A STUDENT AND DISPLAY ITS DIVISION ACCORDINGLY echo enter the marks of the students in 5 subjects out of 100 read a read b read c read d read e sum=` expr $a + $b + $c + $d + $e ` f=` expr $sum \* 100 ` percent=` expr $f / 500 ` echo percentage of the student is $percent if test $percent -ge 60

AKSingh@FET-MITS

then echo The student has got first division elif test $percent -ge 50 -a $percent -lt 60 then echo The student has got second division elif test $percent -ge 40 -a $percent -lt 50 then echo The student has got third divison fi

Divyanti Chauhan (110319)

Page 7

OUTPUT: enter the marks of the students in 5 subjects out of 100 78 79 86 98 86 percentage of the student is 85 The student has got first division

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 8

PROGRAM-8
WRITE A SHELL SCRIPT TO INITIATE A MENU DRIVEN PROGRAM THAT PERFORMS A SET OF COMMANDS MENTIONED USING SWITCH CASE echo "1.date 2. cal 3. ls 4. mkdir ENTER THE SERIAL NO OF THE OPERATION YOU WANT TO PERFORM read a case $a in 1)date;; 2)cal;; 3)ls;;

4)echo "enter the name of directory" read d mkdir $d;; *)echo "Wrong choice" esac

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 9

OUTPUT: 1.date 2. cal 3. ls 4. mkdir ENTER THE SERIAL NO OF THE OPERATION YOU WANT TO PERFORM 4 enter the name of directory collegedocs

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 10

PROGRAM-9
WRITE A SHELL SCRIPT TO SHOW BASIC ARITHMETIC OPERATIONS USING SWITCH CASE echo "1. ADDITION 2.SUBTRACTION 3.MULTIPLICATION 4.DIVISION" echo enter the no.s u want to operate on read a read b echo enter your choice read c case $c in 1)sum=` expr $a + $b ` echo sum is $sum;; 2)diff=` expr $a - $b ` echo diff is $diff;; 3)prod=` expr $a \* $b ` echo prod is $prod;; 4)quotient=` expr $a / $b ` echo quotient is $quotient;; *)echo wrong choice esac

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 11

OUTPUT: 1. ADDITION 2.SUBTRACTION 3.MULTIPLICATION 4.DIVISION enter the no.s u want to operate on 8 7 enter your choice 3 prod is 56

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 12

PROGRAM-10
WRITE A SHELL SCRIPT TO FIND THE FACTORIAL OF A NUMBER AND PRINT IT ON THE TERMINAL echo enter the number whose factorial you want read a f=1 i=1 while [ $i -le $a ] do f=` expr $f \* $i ` i=` expr $i + 1 `

done echo the factorial is $f

AKSingh@FET-MITS

OUTPUT: enter the number whose factorial you want 4 the factorial is 24

Divyanti Chauhan (110319)

Page 13

PROGRAM-11
WRITE A SHELL SCRIPT TO ACCESS A FILE AND PRINT ITS CONTENT ON THE TERMINAL echo input the filename readfname terminal=`tty` exec<$fname while read line do echo $line done exec<$terminal

OUTPUT:

AKSingh@FET-MITS

input the filename student1.sh My name is DivyantiChauhan

Divyanti Chauhan (110319)

Page 14

PROGRAM-12
WRITE A SHELL SCRIPT TO PRINT THE NUMBER OF LINES PRESENT IN A FILE ON THE TERMINAL echo input the filename readfname terminal=`tty` exec<$fname count=0 while read line do count=` expr $count + 1 ` done echo no of lines is $count exec<$terminal

AKSingh@FET-MITS

OUTPUT: input the filename student1.sh no of lines is 1

Divyanti Chauhan (110319)

Page 15

PROGRAM-13
WRITE A SHELL SCRIPT TO COUNT THE NUMBER OF WORDS IN A FILE echo input the filename readfname terminal=`tty` exec<$fname w=0 while read line do set -- $line w=`expr $w + $#` done echo The number of words in the file are $w exec<$terminal

AKSingh@FET-MITS

OUTPUT: input the filename files.sh The number of words in the file are 5

Divyanti Chauhan (110319)

Page 16

PROGRAM-14
WRITE A SHELL SCRIPT TO PRINT THE SQUARE ROOT OF A PERFECT SQUARE NUMBER echo enter the number whose square root you want to calculate read n m=`expr $n / 2` while [ $m -ge 1 ] do f=` expr $m \* $m ` if test $f -eq $n then echo $m is the squareroot m=0 elif test $f -ne $n then m=` expr $m - 1` fi done if test $m -eq 1 then echo number provided is not a perfect square fi

AKSingh@FET-MITS

OUTPUT: enter the number whose square root you want to calculate 256 16is the squareroot

Divyanti Chauhan (110319)

Page 17

PROGRAM-15
WRITE A SHELL SCRIPT TO ENTER AN ARRAY AND DISPLAY ITS ELEMENTS USING FOR LOOP

echo enter the elements in the array for i in {1..5} do read a[$i] done echo The elements in the array are: for i in {1..5} do echo ${a[$i]} done

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 18

OUTPUT: enter the elements in the array 4 5 6 7 8 The elements in the array are: 4 5 6 7 8

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 19

PROGRAM-16
WRITE A SHELL SCRIPT TO ENTER AN ARRAY AND DISPLAY ITS ELEMENTS USING WHILE LOOP

echo enter the array size read n i=1 echo enter the array values while test $i -le $n do read a[$i] i=`expr $i + 1` done i=1

AKSingh@FET-MITS
echo ${a[$i]} i=`expr $i + 1`

echo the array values are while test $i -le $n do

done

Divyanti Chauhan (110319)

Page 20

OUTPUT: enter the array size 5 enter the array values 4 5 6 7 8 the array values are 4 5 6 7 8

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 21

PROGRAM-17
WRITE A SHELL SCRIPT TO ENTER AN ARRAY AND DISPLAY ITS ELEMENTS IN REVERSE ORDER

echo enter the elements in the array for i in {1..5} do read a[$i] done echo The elements in the array are: for i in {5..1} do echo ${a[$i]} done

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 22

OUTPUT: enter the elements in the array 4 5 6 7 8 The elements in the array are: 8 7 6 5 4

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 23

PROGRAM-18
WRITE A SHELL SCRIPT TO CHECK WHETHER A GIVEN FILE IS A FILE OR A DIRECTORY echo input file name read filename if [ -f $filename ]

then echo $filename is a file

elif [ -d $filename ]

then echo $filename is a directory

else

AKSingh@FET-MITS
echo $filename is not a file

fi

Divyanti Chauhan (110319)

Page 24

OUTPUT: #1 input file name syslab syslab is a file

#2 input file name sys sys is a directory

#3 input file name sys1 sys1 is not a file

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 25

PROGRAM-19
WRITE A SHELL SCRIPT TO CHECK WHETHER A GIVEN FILE HAS WRITE PERMISSION. IF SO, THEN APPEND THAT FILE echo enter the filename read fname if [ -w $fname ] then echo $fname has a write permission echo write the matter and press ctrl d to save cat>> $fname else echo no write permission for $fname fi

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 26

OUTPUT: enter the filename test test has a write permission write the matter and press ctrl d to save test file updated successfully (ctrl + d) ( cat test) test
This is my first program. test file updated successfully

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 27

PROGRAM-20
WRITE A SHELL SCRIPT TO CHECK WHETHER TWO STRINGS ARE SAME OR NOT. ALSO CHECK WHETHER EACH STRING HAVE ZERO LENGTH OR NON ZERO LENGTH. echo enter the string 1 read str1 echo enter the string 2 read str2 if [ "$str1" = "$str2" ] then echo $str1 and $str2 have same length and same characters else echo $str1 and $str2 dont have same length and same characters fi if [ -n "$str1" ] then

echo $str1 has nonzero length

AKSingh@FET-MITS

else echo $str1 has zero length fi if [ -n "$str2" ] then echo $str2 has non zero length else echo $str2 has zero length fi

Divyanti Chauhan (110319)

Page 28

OUTPUT: #1 enter the string 1 happy day enter the string 2 happy day happy day and happy day have same length and same characters happy day has nonzero length happy day has nonzero length

#2 enter the string 1 happy

AKSingh@FET-MITS

enter the string 2 HAPPY happy and HAPPY have same length and same characters happy has nonzero length HAPPY has nonzero length

Divyanti Chauhan (110319)

Page 29

PROGRAM-21
WRITE A SHELL SCRIPT TO CHECK WHETHER TWO STRINGS ARE SAME OR NOT. ALSO CHECK WHETHER A STRING HAVE ZERO LENGTH OR NON ZERO LENGTH.PERFORM THIS WITHOUT IF CONSTRUCT. echo enter the string1 read str1 echo enter the string2 read str2 [ $str1 = $str2 ] echo $? [ $str1 != $str2 ] echo $? [ -n $str1 ] echo $?

AKSingh@FET-MITS

[ -z $str1 ] echo $?

Divyanti Chauhan (110319)

Page 30

OUTPUT: enter the string1 good enter the string2 bad 1 0 0 1

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 31

PROGRAM-22
WRITE A SHELL SCRIPT TO CHECK WHETHER THE ENTERED CHARACTER IS AN ALPHABET, DIGIT OR SPECIAL SYMBOL

echo input the character read c case $c in

[A-Z]) echo given character $c is an uppercase character ;; [0-9]) echo given character $c is a digit ;; [a-z]) echo given character $c is a lowercase character ;;

AKSingh@FET-MITS

?) echo given character $c is a special character ;; *) echo entered more than one character ;; esac

Divyanti Chauhan (110319)

Page 32

OUTPUT:

#1 input the character A given character A is an uppercase character

#2 input the character 1 given character 1 is a digit

#3

AKSingh@FET-MITS

input the character d given character d is a lowercase character

#4 input the character gh entered more than one character

Divyanti Chauhan (110319)

Page 33

PROGRAM-23
WRITE A SHELL SCRIPT TO PERFORM LINEAR SEARCH ON AN ARRAY echo enter the elements in the array flag=0 for i in {1..10} do read a[$i] done echo enter the element you want to search read n for i in {1..10} do

AKSingh@FET-MITS
if [ ${a[$i]} == $n ] then c=$i flag=1 fi done if [ $flag == 0 ] then else echo element found at $c position fi echo element not found

Divyanti Chauhan (110319)

Page 34

OUTPUT: enter the elements in the array 1 2 3 4 5 6 7 8 9 10 enter the element you want to search 5 element found at 5 position

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 35

PROGRAM-24
WRITE A SHELL SCRIPT TO PERFORM BINARY SEARCH ON AN ARRAY echo enter the elements in the array for i in {1..6} do read a[$i] done echo enter the element you want to search read n l=1 h=6 i=` expr $l + $h ` mid=` expr $i / 2 ` while [ $l -le $h ] do if [ ${a[$mid]} -gt $n ] then l=` expr $mid + 1 `

AKSingh@FET-MITS

elif [ ${a[$mid]} == $n ] then echo element found at $mid position exit else h=` expr $mid - 1 ` fi

Divyanti Chauhan (110319)

Page 36

i=` expr $l + $h ` mid=` expr $i / 2 ` done if [ $l -gt $h ] then echo element not found fi

OUTPUT:

enter the elements in the array 1 2 3 4 5 6 enter the element you want to search 5 element found at 5 position

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 37

PROGRAM-25
WRITE A SHELL SCRIPT WHICH COPIES MULTIPLE FILES IN A DIRECTORY MANUALLY echo enter the directory name in which you want to copy files read dirname m=1 while test $m -ne 0 do echo enter the filename which you want to copy read fname cp $fname $dirname echo file $fname is copied to $dirname successfully. echo do you want to enter more: enter 1 for yes and 0 for no read m done

AKSingh@FET-MITS

OUTPUT: enter the directory name in which you want to copy files newdir enter the filename which you want to copy p1 file p1 is copied to newdir successfully. do you want to enter more: enter 1 for yes and 0 for no 0

Divyanti Chauhan (110319)

Page 38

PROGRAM-26
WRITE A SHELL SCRIPT WHICH COPIES MULTIPLE FILES IN A DIRECTORY (FOR A PARTICULAR EXTENSION USING FOR LOOP) echo enter the directory name in which you want to copy the files read dirname for i in `ls *.sh` do cp $i $dirname done echo files copied successfully

OUTPUT: enter the directory name in which you want to copy the files dir

AKSingh@FET-MITS

files copied successfully

Divyanti Chauhan (110319)

Page 39

PROGRAM-27
WRITE A SHELL SCRIPT TO COUNT NO. OF LINES, WORDS AND CHARACTERS IN A FILE echo enter the filename read fname terminal=`tty` exec<$fname echo The no of words in a file are wc -l < $fname echo The no of lines in a file are wc -w < $fname echo The no of characters in a file are wc -c < $fname exec<$terminal

AKSingh@FET-MITS

OUTPUT: enter the filename syslab The no of words in a file are 11 The no of lines in a file are 5 The no of characters in a file are 64

Divyanti Chauhan (110319)

Page 40

PROGRAM-28
WRITE A SHELL SCRIPT TO DISPLAY SUBDIRECTORIES IN THE CURRENT DIRECTORY

for entry in * do if [ -d $entry ] then echo $entry fi done OUTPUT: bin dir lab lab45 newdir public_html student sys

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 41

PROGRAM-29
WRITE A SHELL SCRIPT TO PERFORM MATRIX ADDITION echo enter the row and column of matrix read m read n i=1 j=` expr $m \* $n ` echo enter the elements in matrix 1 while [ $i -le $j ] do read y[$i] i=`expr $i + 1` done

AKSingh@FET-MITS

echo enter the elements in matrix 2 i=1 while [ $i -le $j ] do read z[$i] i=`expr $i + 1` done i=1 while [ $i -le $j ] do c[$i]=`expr ${y[$i]} + ${z[$i]}`
Divyanti Chauhan (110319) Page 42

i=`expr $i + 1` done echo display sum of 2d array i=1 while [ $i -le $m ] do echo " ${c[$i]} " i=`expr $i + 1` done k=`expr $m + 1` while [ $k -le $j ] do echo " ${c[$k]} " k=`expr $k + 1` done

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 43

OUTPUT: enter the row and column of matrix 2 2 enter the elements in the matrix 1 1 2 3 4 enter the elements in matrix 2 1 2 3 4 display sum of 2d array 2 4 6 8

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 44

PROGRAM-30
WRITE A SHELL SCRIPT TO PERFORM ADDITION USING FUNCTION echo enter the two numbers whose sum you want read m read n input() { a=$1 b=$2 } input $m $n add() { c=0

AKSingh@FET-MITS
return $c

c=`expr $1 + $2`

} add $m $n ret=$? display() { echo the sum is $1 } display $ret

Divyanti Chauhan (110319)

Page 45

OUTPUT: enter the two numbers whose sum you want 1 2 the sum is 3

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 46

PROGRAM-31
WRITE A SHELL SCRIPT TO SWAP THE VALUES OF TWO VARIABLES USING A THIRD VARIABLE. IMPLEMENT IT USING A FUNCTION. echo enter any two values read x read y swap() { temp=0 a=$1 b=$2 echo BEFORE SWAP echo the value of x is $1

echo the value of y is $2 temp=$a a=$b b=$temp echo AFTER SWAP echo the value of x is $a

AKSingh@FET-MITS

echo the value of y is $b } swap $x $y

Divyanti Chauhan (110319)

Page 47

OUTPUT: BEFORE SWAP the value of x is 21 the value of y is 22 AFTER SWAP the value of x is 22 the value of y is 21

AKSingh@FET-MITS

Divyanti Chauhan (110319)

Page 48

PROGRAM-32
WRITE A SHELL SCRIPT TO FIND FACTORIAL OF A FUNCTION USING A RECURSIVE FUNCTION

fact() { if [ $1 -gt 1 ] then i=`expr $1 - 1` j=`fact $i` f=`expr $1 \* $j` echo $f else echo 1

AKSingh@FET-MITS

return 1 fi } echo input number read n fact $n OUTPUT: input number 4 24

Divyanti Chauhan (110319)

Page 49

You might also like