System Programming Laboratory

download System Programming Laboratory

of 65

Transcript of System Programming Laboratory

  • 8/13/2019 System Programming Laboratory

    1/65

    AKSingh@FET-MITS

    MODY INSTITUTE OF TECHNOLOGY & SCIENCE, LAXMANGARH

    FACULTY OF ENGINEERING & TECHNOLOGY

    MBA-TECH CSE 3

    rd

    YEAR

    SYSTEM PROGRAMMING LAB

    (Autumn Sem 2013-14)

    SUBMITTED TO: Mr. Ajay Kumar Singh Submitted By: Divyanti Chauhan

    Asst. Prof. FET

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    2/65

  • 8/13/2019 System Programming Laboratory

    3/65

    AKSingh@FET-MITS

    2. touch-It is used to create a ew epty file. You cat write aythig i the file siultaeously.

    SYNTAX: touch filename

    E.g. touch test

    For multiple files:

    E.g. touch test1, test2, test3

    3. date-To show date and time of the system.

    SYNTAX: date

    Typing this command will show the date and time in a particular format.

    Fri Jan 6 08:52:42 MST 2013

    4. cal-To display the calendar of present month with the present day highlighted.

    SYNTAX: cal

    Typing this command will show the calendar as shown below.

    5. who-To show the various connection being done on the UNIX remote server.

    SYNTAX: who

    Typing this command will generate a huge list of connections as shown below:

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    4/65

    AKSingh@FET-MITS

    6. ls-To list all the files and directories present on this particular path.

    SYNTAX: ls

    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 characterE.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 charactersE.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 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: rmi filename

    E.g. rmi 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. mkdir-To make a new directory

    SYNTAX: mkdir filename

    E.g. mkdirsyslab

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    5/65

    AKSingh@FET-MITS

    9. cd-To 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

    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

    mv can also be used to move a file from current directory to any other directory asspecified.

    SYNTAX: mv filename dirname

    E.g. mv tests syslab

    11.cp- 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

    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

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    6/65

    AKSingh@FET-MITS

    12. echo- To display a string

    YNTAX: echo strig or echo strig

    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 terminalSYNTAX: echo $varname

    E.g. a=10

    echo $a

    Typing this command will show 10 as the output on the screen.

    13. read-To take input from the user.

    SYNTAX: read varname

    E.g. read aTyping 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

  • 8/13/2019 System Programming Laboratory

    7/65

    AKSingh@FET-MITS

    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:

    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 editorIn vi editor we will be writing shell script.

    ***The extension of the program is .sh

    To run the file we simply write sh. Filename is specified with extension.

    ApplicationProgram

    Shell

    Kernel

    Hardware

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    8/65

    AKSingh@FET-MITS

    SHELL SCRIPTS

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    9/65

    AKSingh@FET-MITSAKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    10/65

    AKSingh@FET-MITS

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    11/65

    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-4w-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

  • 8/13/2019 System Programming Laboratory

    12/65

    AKSingh@FET-MITS

    1.WriteMessage can be sent from one UNIX user to other

    Syntax: Write

    Then ctrl d

    2 prerequisites:

    - The user whom you are sending must be logged in else anerror message will be generated

    - Secondly the user must have provided permission to sendmessage to that user

    2. mesgn

    To set the receiving permission as no to receive messages.

    3.Finger commandIt is used to check which users are logged in. It is only for super

    user.

    4.WhoTSame as finger command for normal users.

    + denotes the users who permit to receive the message

    - Who dont permitpts/0 pts/1 etc they are terminal names

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    13/65

    AKSingh@FET-MITS

    5.write(In case one user is logged in one more than one system)

    write

    6.wallTo send messages to all users even though the receiving

    permission is not availed. Meant for super users or administrators

    only.

    Syntax:

    Wall

    Ctrl d

    7.mailTo send mail to any UNIX user.

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    14/65

    AKSingh@FET-MITS

    SYNTAX: mail

    Subject: _______________________

    Ctrl+d

    For more than one user

    Mail .

    8.mail userid

  • 8/13/2019 System Programming Laboratory

    15/65

    AKSingh@FET-MITS

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    16/65

    AKSingh@FET-MITSAKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    17/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 1

    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

  • 8/13/2019 System Programming Laboratory

    18/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 2

    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 12AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    19/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 3

    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

    difference is 3

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    20/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 4

    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

    product is 50

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    21/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 5

    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 2AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    22/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 6

    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 }

    elif test $c -gt $b

    then echo $c is largest

    fi

    OUTPUT:

    input the values of a,b,c

    5

    7

    8

    8 is largest

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    23/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 7

    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

    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

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    24/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 8

    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

  • 8/13/2019 System Programming Laboratory

    25/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 9

    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

  • 8/13/2019 System Programming Laboratory

    26/65

  • 8/13/2019 System Programming Laboratory

    27/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 11

    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

  • 8/13/2019 System Programming Laboratory

    28/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 12

    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

  • 8/13/2019 System Programming Laboratory

    29/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 13

    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

    OUTPUT:

    enter the number whose factorial you want

    4

    the factorial is 24

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    30/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 14

    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

  • 8/13/2019 System Programming Laboratory

    31/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 15

    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

  • 8/13/2019 System Programming Laboratory

    32/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 16

    PROGRAM-13

    WRITE A SHELL SCRIPT TO COUNT THE NUMBER OF WORDS IN A FILE

    echo input the filename

    readfname

    terminal=`tty`

    exec

  • 8/13/2019 System Programming Laboratory

    33/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 17

    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

    OUTPUT:

    enter the number whose square root you want to calculate

    256

    16is the squareroot

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    34/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 18

    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

  • 8/13/2019 System Programming Laboratory

    35/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 19

    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

  • 8/13/2019 System Programming Laboratory

    36/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 20

    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

    echo the array values are

    while test $i -le $n

    do

    echo ${a[$i]}

    i=`expr $i + 1`

    done

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    37/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 21

    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

  • 8/13/2019 System Programming Laboratory

    38/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 22

    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

  • 8/13/2019 System Programming Laboratory

    39/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 23

    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

  • 8/13/2019 System Programming Laboratory

    40/65

  • 8/13/2019 System Programming Laboratory

    41/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 25

    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

  • 8/13/2019 System Programming Laboratory

    42/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 26

    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

    fiAKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    43/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 27

    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

  • 8/13/2019 System Programming Laboratory

    44/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 28

    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

    else echo $str1 has zero length

    fi

    if [ -n "$str2" ]

    then echo $str2 has non zero length

    else echo $str2 has zero length

    fi

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    45/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 29

    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

    enter the string 2

    HAPPY

    happy and HAPPY have same length and same characters

    happy has nonzero length

    HAPPY has nonzero length

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    46/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 30

    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 $?

    [ -z $str1 ]

    echo $?

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    47/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 31

    OUTPUT:

    enter the string1

    good

    enter the string2

    bad

    1

    0

    0

    1

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    48/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 32

    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

    ;;

    ?) echo given character $c is a special character

    ;;

    *) echo entered more than one character

    ;;

    esac

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    49/65

  • 8/13/2019 System Programming Laboratory

    50/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 34

    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

    if [ ${a[$i]} == $n ]

    then c=$i

    flag=1

    fi

    done

    if [ $flag == 0 ]

    then echo element not found

    else

    echo element found at $c position

    fi

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    51/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 35

    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

  • 8/13/2019 System Programming Laboratory

    52/65

  • 8/13/2019 System Programming Laboratory

    53/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 37

    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

  • 8/13/2019 System Programming Laboratory

    54/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 38

    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

    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

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    55/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 39

    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

    files copied successfully

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    56/65

  • 8/13/2019 System Programming Laboratory

    57/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 41

    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

  • 8/13/2019 System Programming Laboratory

    58/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 42

    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

    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]}`

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    59/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 43

    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

  • 8/13/2019 System Programming Laboratory

    60/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 44

    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

  • 8/13/2019 System Programming Laboratory

    61/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 45

    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

    c=`expr $1 + $2`

    return $c

    }

    add $m $n

    ret=$?

    display()

    {

    echo the sum is $1

    }

    display $ret

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    62/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 46

    OUTPUT:

    enter the two numbers whose sum you want

    1

    2

    the sum is 3

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    63/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 47

    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

    echo the value of y is $b

    }

    swap $x $y

    AKSingh@FET-MITS

  • 8/13/2019 System Programming Laboratory

    64/65

    AKSingh@FET-MITS

    Divyanti Chauhan (110319) Page 48

    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

  • 8/13/2019 System Programming Laboratory

    65/65

    AKSingh@FET-MITS

    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

    return 1

    fi

    }

    echo input number

    read n

    fact $n

    OUTPUT:

    input number

    AKSingh@FET-MITS