OS Lab Manual 06 07

Embed Size (px)

Citation preview

  • 8/8/2019 OS Lab Manual 06 07

    1/62

    SHELL PROGRAMS

    EXPERIMENT 1

    AIM : To swap two numbers with out using temporary variable

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY :

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument1]

    The shell executes the program CommandName and passes argument on the command line toit. Then the shell waits for the command to exit.

    This program is for swapping two numbers using only two variables

    ALGORITHM :

    STEP 1. Read two numbers a and b

    STEP 2. a=`expr $a + $b`

    STEP 3.b=`expr $a -$b`

    STEP 4. c=`expr $a-$b`

    STEP 5. Echo the swapped values as $a and $b

    DEPARTMENT OF COMPUTER SCIENCE

    90

  • 8/8/2019 OS Lab Manual 06 07

    2/62

    PROGRAM:

    echo -n "enter two numbers"read a

    read becho "Before swapping"echo " A = $a B = $b"a=`expr $a + $b`

    b=`expr $a - $b`a=`expr $a - $b`echo "After swapping "echo "A = $a B = $b"

    OUTPUT :

    [balu@krishna-edu shell]$ sh swap.shenter two numbers45Before swappingA = 4 B = 5After swappingA = 5 B = 4

    DEPARTMENT OF COMPUTER SCIENCE

    91

  • 8/8/2019 OS Lab Manual 06 07

    3/62

    EXPERIMENT 2

    AIM: To Swap two numbers using temporary variable

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY :

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument1]

    The shell executes the program CommandName and passes argument on the command lineto it. Then the shell waits fro the command to exit.

    This program is for swapping two numbers using three variables

    ALGORITHM:

    STEP 1. Read two numbers a and b

    STEP 2. Echo the numbers a and b

    STEP 3. c=$aSTEP 4. a=$b

    STEP 5. b=$c

    STEP 6. Echo a and b after swapping

    DEPARTMENT OF COMPUTER SCIENCE

    92

  • 8/8/2019 OS Lab Manual 06 07

    4/62

  • 8/8/2019 OS Lab Manual 06 07

    5/62

  • 8/8/2019 OS Lab Manual 06 07

    6/62

    PROGRAM:

    echo -n "enter a number"

    read numsqr=`expr $num \* $num`cube=`expr $sqr \* $num`echo "the squre of $num is $sqr"echo "the cube of $num is $cube"

    OUTPUT:

    [balu@krishna-edu shell]$ sh sqcu.sh

    enter a number 4

    the squre of 4 is 16

    the cube of 4 is 64

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    95

  • 8/8/2019 OS Lab Manual 06 07

    7/62

    EXPERIMENT 4

    AIM : To check whether a given number is even or odd .

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command line toit. Then the shell waits fro the command to exit.

    This program is to check whether a given number is even or odd .

    ALGORITHM :

    STEP 1. Read the number n

    STEP 2. Perform modulo division on n and store it in r

    STEP 3. If r is equal to zero then

    Echo the given number is even

    STEP 4. If the condition is not satisfied then

    Echo the output is an odd number

    DEPARTMENT OF COMPUTER SCIENCE

    96

  • 8/8/2019 OS Lab Manual 06 07

    8/62

    PROGRAM:

    clearecho enter the numberread n

    r=1r=`expr $n % 2`if [ $r -eq 0 ]then

    echo evenelse

    echo oddfi

    OUTPUT:

    [krishna-edu-balu]$ sh eorodd

    enter the number

    5

    odd

    [krishna-edu balu]$

    DEPARTMENT OF COMPUTER SCIENCE

    97

  • 8/8/2019 OS Lab Manual 06 07

    9/62

    EXPERIMENT 5

    AIM : To find whether the given input name is a file or a directory.

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit. Files are the passivecontainers of data on disk. Many operating systems name hardware devices in the same wayas files and allow access to devices using the same interface as to files: open, read, write,close.

    This program checks whether the given name is file or directory

    ALGORITHM:

    STEP 1. Read a file name.

    STEP 2. Check for the type of file using f option.

    STEP 3. If condition is satisfied then

    Echo file as an ordinary file

    Else

    Echo file as a directory

    .

    DEPARTMENT OF COMPUTER SCIENCE

    98

  • 8/8/2019 OS Lab Manual 06 07

    10/62

    PROGRAM

    clear

    echo enter a filenameread $fnameif test f $fnamethen

    echo file is a ordinary fileelse

    echo directoryfi

    OUTPUT:

    [krishna-edu balu]$ sh fd

    enter a filename

    string

    file is a ordinary file

    [krishna-edu balu]$

    DEPARTMENT OF COMPUTER SCIENCE

    99

  • 8/8/2019 OS Lab Manual 06 07

    11/62

    EXPERIMENT 6

    AIM: To check the type of character entered.

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit.

    This program is to check the type of input character using case statement.

    ALGORITHM:

    STEP 1. Read a character from the keyboard.

    STEP 2. Use case statement to checkfor the type of character

    STEP 3. Echo the character as alphabet or number or special character

    STEP 4. End case

    DEPARTMENT OF COMPUTER SCIENCE

    100

  • 8/8/2019 OS Lab Manual 06 07

    12/62

    PROGRAM:

    clearecho enter a character from the keyboardread chcase $ch in

    [a-z]) echo its a alphabet;;[0-9]) echo its a number;;

    ?) echo it is a special character;;

    *) echo u entered more;;

    esac

    OUTPUT:

    [krishna-edu balu]$ sh case1

    enter a character from the keyboard

    a

    its a character

    [krishna-edu balu]$

    DEPARTMENT OF COMPUTER SCIENCE

    101

  • 8/8/2019 OS Lab Manual 06 07

    13/62

    EXPERIMENT 7

    AIM: To illustrate the usage of case statement.

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit.

    This program is an example for using case statement.

    ALGORITHM:

    STEP 1: Read the choice

    STEP 2: If choice is 1 then display the fileSTEP 3: If choice is 2 then copy one file to another.

    STEP 4: If choice is 3 then create a directory.

    STEP 5: End case

    DEPARTMENT OF COMPUTER SCIENCE

    102

  • 8/8/2019 OS Lab Manual 06 07

    14/62

    PROGRAM

    clearecho 1.display a fileecho 2.copy a file

    echo 3.create a directoryecho enter ur choiceread chcase $ch in

    1) echo enter filenameread fnamecat $fname;;

    2) echo enter two filesread file1 file2cp $file1 $file2;;

    3) echo enter the directoryread dnamemkdir $dname

    ;;*) echo invalid option

    esac

    OUTPUT:

    [krishna-edu balu] sh case

    1.print a file

    2.copy a file

    3.create a directory

    enter ur choice

    2

    enter two files

    c2 ch

    [krishna-edu balu]$ vi ch

    This is unix environment.

    DEPARTMENT OF COMPUTER SCIENCE

    103

  • 8/8/2019 OS Lab Manual 06 07

    15/62

    EXPERIMENT 8

    AIM: To compare two strings.

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it(via the argv array of strings). Then the shell waits for the command toexit.

    This program is to compare two strings

    ALGORITHM:

    STEP 1:Read string1.STEP 2:Read string2.

    STEP 3:compare the two strings.

    STEP 4:If string is equal to string2 then

    Echo strings are equal

    Echo strings are not equal

    DEPARTMENT OF COMPUTER SCIENCE

    104

  • 8/8/2019 OS Lab Manual 06 07

    16/62

    PROGRAM

    clearecho "enter a string1"read first

    echo "enter a string2"read secondif [ $first = $second ]

    thenecho equal

    elseecho unequal

    fi

    OUTPUT:

    [krishna-edu balu]$ sh string

    enter a string1

    hello

    enter a string2

    krishna

    unequal

    [krishna-edu balu]$

    DEPARTMENT OF COMPUTER SCIENCE

    105

  • 8/8/2019 OS Lab Manual 06 07

    17/62

    EXPERIMENT 9

    AIM: To compare two strings by reading strings from the command line .

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command lineto it. Then the shell waits fro the command to exit

    This program is for comparing two strings by reading strings from the commandline

    ALGORITHM:

    STEP 1. Read the value of first string

    STEP 2. Read the value of second string

    STEP 3. If string1 is equal to string2

    Echo strings are equal

    STEP 4. Else echo strings as unequal

    DEPARTMENT OF COMPUTER SCIENCE

    106

  • 8/8/2019 OS Lab Manual 06 07

    18/62

    PROGRAM:

    if [ $1 = $2 ]then

    echo "Strings are equal ...."elseecho "Strings are not equal....."

    fi

    [balu@krishna-edu shell]$ sh comparecmd.sh krishna krishnaStrings are equal ....[balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    107

  • 8/8/2019 OS Lab Manual 06 07

    19/62

    EXPERIMENT 10

    AIM: To find the length of a string

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command lineto it. Then the shell waits fro the command to exit.

    This program is for finding the length of the string .

    ALGORITHM:

    STEP 1. Read the value of the string

    STEP 2. Count the number of characters in the string using the wc c command and store

    the result in the variable length

    STEP 3. Subtract the new line character

    STEP 4. Echo the value of the length of the string

    DEPARTMENT OF COMPUTER SCIENCE

    108

  • 8/8/2019 OS Lab Manual 06 07

    20/62

    PROGRAM

    echo -n "Enter the string "read strlen=`echo $str | wc -c`

    len=`expr $len - 1`echo " the length of the string is $len"

    OUTPUT

    [balu@krishna-edu shell]$ sh length.sh

    Enter the string krishna

    the length of the string is 6

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    109

  • 8/8/2019 OS Lab Manual 06 07

    21/62

    EXPERIMENT 11

    AIM: To calculate the value of X n

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command lineto it. Then the shell waits fro the command to exit.

    This program is for finding the value of Xn

    ALGORITHM:

    STEP 1. Read the values of x and n

    STEP 2. Initialize sum and I to 1

    STEP 3. Check for the condition [ $I -le $n ]

    STEP 4.Repeat the following steps until the above condition becomes false

    Sum=`expr $sum \* $x`

    i=`expr $i + 1

    STEP 5. Echo the value of sum as the value of x power n

    DEPARTMENT OF COMPUTER SCIENCE

    110

  • 8/8/2019 OS Lab Manual 06 07

    22/62

    PROGRAM:

    echo -n "enter ther value of x "

    read xecho -n "enter the value of n:"read nsum=1i=1while [ $i -le $n ]do

    sum=`expr $sum \* $x`i=`expr $i + 1`doneecho "the value of $x power $n is $sum"

    OUTPUT:

    [balu@krishna-edu shell]$ sh power.sh

    enter ther value of x 5

    enter the value of n:2

    the value of 5 power 2 is 25[balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    111

  • 8/8/2019 OS Lab Manual 06 07

    23/62

    EXPERIMENT 12

    AIM: To read 10 numbers from the user and to find the sum andaverage of the numbers .

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command line toit.

    This is the program for reading 10 numbers from the user and finding the sum and average of

    the numbers

    ALGORITHM:

    STEP 1. Initialize the values n to 1 and sum to 0

    STEP 2. Repeat the following steps until the condition [ $n -le 10 ] becomes false

    Read the number

    Sum=`expr $sum + $num`

    Num=0N=`expr $n + 1`

    STEP 3. Calculate the average

    avg=`expr $sum / 10`

    STEP 4. Echo the average of 10 numbers

    DEPARTMENT OF COMPUTER SCIENCE

    112

  • 8/8/2019 OS Lab Manual 06 07

    24/62

    PROGRAM:

    n=1sum=0while [ $n -le 10 ]do

    echo -n "enter number $n"read numsum=`expr $sum + $num`num=0;n=`expr $n + 1`

    doneavg=`expr $sum / 10`echo "ther sum of 10 numbers is $sum"echo "the average of 10 numbers is $avg"

    OUTPUT :

    [balu@krishna-edu shell]$ sh sumarg.sh

    enter number 1 is : 1

    enter number 2 is : 2

    enter number 3 is : 3

    enter number 4 is : 4

    enter number 5 is : 5

    enter number 6 is : 6

    enter number 7 is : 7

    enter number 8 is : 8

    enter number 9 is : 9

    enter number 10 is : 10

    ther sum of 10 numbers is 55the average of 10 numbers is 5

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    113

  • 8/8/2019 OS Lab Manual 06 07

    25/62

    EXPERIMENT 13

    AIM: To accept a number from the user and display the list of even numbersbelow that number

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command line toit.

    This is the program for accepting numbers from the user and display the list of even numbersbelow that number

    ALGORITHM:

    STEP 1. Read the limit value

    STEP 2. Initialize x to 2

    STEP 3. Read the number

    STEP 4. Repeat the following steps until the condition x

  • 8/8/2019 OS Lab Manual 06 07

    26/62

    PROGRAM:

    echo -n "enter the limit:"x=2read numwhile [ $x -lt $num ]do

    echo -n "$x \t"x=`expr $x + 2`

    done

    OUTPUT :

    [balu@krishna-edu shell]$ sh evenbelow.sh

    enter the limit:10

    2 4 6 8

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    115

  • 8/8/2019 OS Lab Manual 06 07

    27/62

    EXPERIMENT 14

    AIM: To find the count and sum of even and odd numbers separately

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command line toit.

    This is the program for finding the count and sum of even and odd numbers separately

    ALGORITHM

    STEP 1. Read the limit n

    STEP 2. Initialize the values of i ,j ,sumodd and sumeven as 0

    STEP 3. calculate modulo division for each number

    STEP 4. If the answer is 0 then add the number to sumeven

    else add it to sumodd

    STEP 5.Echo the values of i, j ,sumeven and sumodd

    DEPARTMENT OF COMPUTER SCIENCE

    116

  • 8/8/2019 OS Lab Manual 06 07

    28/62

    PROGRAM:

    echo -n "enter the limit"read necho -n "enter the number"

    i=0j=0k=0sumodd=0sumeven=0while [ $i -lt $n ]do

    read numif [ `expr $num % 2` -eq 0 ]then

    sumodd=`expr $sumodd + $num`

    k=`expr $k + 1`else

    sumeven=`expr $sumeven + $num`j=`expr $j + 1`

    fi

    i=`expr $i + 1`

    OUTPUT :

    [balu@krishna-edu shell]$ sh oddeven.sh

    enter the limit5

    enter the number1

    2

    3

    4

    5

    total even number is 2

    total odd numbers is 3

    the sum of even numbers is 9

    the sumof odd number is 6

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    117

  • 8/8/2019 OS Lab Manual 06 07

    29/62

    EXPERIMENT 15

    AIM: To count the number of digits in an integer

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.

    The shell executes commands of the form:CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command line toit.

    This is the program for counting the number of digits in an integer

    ALGORITHM:

    STEP 1.Read the numberSTEP 2.Initialize digit to 0

    STEP 3.Repeat the following steps until the condition num > 0 becomes false

    a=`expr $num % 10`

    digit=`expr $digit + 1`

    num=`expr $num / 10`

    STEP 4.Echo the number of digits in the integer

    DEPARTMENT OF COMPUTER SCIENCE

    118

  • 8/8/2019 OS Lab Manual 06 07

    30/62

    PROGRAM:

    echo -n "Enter an interge:"read num

    digit=0while [ $num -gt 0 ]do

    a=`expr $num % 10`digit=`expr $digit + 1`num=`expr $num / 10`

    doneecho "the number of digits in the integer is $digit"

    OUTPUT:

    [balu@krishna-edu shell]$ sh count.sh

    Enter an interge:12345

    the number of digits in the integer is 5

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    119

  • 8/8/2019 OS Lab Manual 06 07

    31/62

    EXPERIMENT 16

    AIM : To find the sum of digits in an integer

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command line toit.

    This is the program for finding the sum of digits in an integer.

    ALGORITHM:

    STEP 1.Read the number

    STEP 2.Initialize sum to 0

    STEP 3.Repeat the following steps until the condition num > 0 becomes false

    a=`expr $num % 10`

    sum=`expr $sum + a`

    num=`expr $num / 10`

    STEP 4.Echo the sum of digits in the integer

    DEPARTMENT OF COMPUTER SCIENCE

    120

  • 8/8/2019 OS Lab Manual 06 07

    32/62

    PROGRAM:

    echo -n "enter an integer"read numsum=0

    while [ $num -gt 0 ]do

    a=`expr $num % 10`sum=`expr $sum + $a`num=`expr $num / 10`

    doneecho "the sum of digits in the integer is $sum"

    OUTPUT :

    [balu@krishna-edu shell]$ sh sumdigit.shenter an integer12345the sum of digits in the integer is 15[balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    121

  • 8/8/2019 OS Lab Manual 06 07

    33/62

    EXPERIMENT 17

    AIM: To print the multiplication table of a given number .

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command line toit.

    This is the program for printing the multiplication table of the given number

    ALOGRITHM :

    STEP 1.Read the values of table number and limit as x and n

    STEP 2.Initialize the value of I to 1

    STEP 3.Repeat the following statements until the condition [ $I le $n ] becomes false

    Sum=`expr $x \* $I`

    Echo $x x $I = $sum

    Increment the value of I

    DEPARTMENT OF COMPUTER SCIENCE

    122

  • 8/8/2019 OS Lab Manual 06 07

    34/62

    PROGRAM:

    echo -n "Enter the table number"read xecho -n "Enter the limit"read nechoecho "The multiplication table for $x is "echoi=1while [ $i -le $n ]do

    sum=`expr $x \* $i`echo "$x x $i = $sum "

    i=`expr $i + 1`done

    OUTPUT :

    [balu@krishna-edu shell]$ sh multiply.sh

    Enter the table number5

    Enter the limit10

    The multiplication table for 5 is

    5 x 1 = 5

    5 x 2 = 10

    5 x 3 = 15

    5 x 4 = 20

    5 x 5 = 25

    5 x 6 = 30

    5 x 7 = 35

    5 x 8 = 40

    5 x 9 = 45

    5 x 10 = 50

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    123

  • 8/8/2019 OS Lab Manual 06 07

    35/62

    EXPERIMENT 18

    AIM: To print all the divisors of a given integer.

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command line toit.

    This program for printing all the divisors of a given integer.

    ALGORITHM:

    STEP 1. Read the number

    STEP 2. Initialize the value of I to 1

    STEP 3.Repeat the following statements until the condition I

  • 8/8/2019 OS Lab Manual 06 07

    36/62

    PROGRAM :

    echo -n "Enter the number"read numecho "The divisors are"

    i=1while [ $i -le $num ]do

    temp=`expr $num % $i`if [ $temp -eq 0 ]then

    echo -n " $i "fii=`expr $i + 1`

    done

    OUTPUT :

    [balu@krishna-edu shell]$ sh divisor.sh

    Enter the number5

    The divisors are

    1 5 [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    125

  • 8/8/2019 OS Lab Manual 06 07

    37/62

    EXPERIMENT 19

    AIM : To find the factorial of a give number.

    REQUIREMENTS:

    HARDWARE: P-IV Processor, 128 MB RAM, 40 GBSOFTWARE: OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit.

    This program is to find the factorial of a given number.

    ALGORITHM:

    STEP 1:Read the numberSTEP 2:Initialize res to 1 and i to 1

    STEP 3:Repeat the following steps until the condition i

  • 8/8/2019 OS Lab Manual 06 07

    38/62

    PROGRAM

    clearecho enter the numberread n

    res=1i=1while [ $i -le $n ]dores=`expr $res \* $i`i=`expr $i + 1`

    doneecho factorial=$res

    OUTPUT:

    [krishna-edu balu]$ sh fact

    enter the number

    6

    factorial=720

    [krishna-edu balu]$

    DEPARTMENT OF COMPUTER SCIENCE

    127

  • 8/8/2019 OS Lab Manual 06 07

    39/62

    EXPERIMENT 20

    AIM: To generate fibonacci series.

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY :

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on the command line toit. Then the shell waits for the command to exit.

    This program is to generate fibonacci series upto a given limit.

    ALGORITHM:

    STEP 1:Enter a number upto which the series has to be generated.

    STEP 2:Initialize a=0,b=1,d=2.

    STEP 3:Repeat the following steps until the condition d

  • 8/8/2019 OS Lab Manual 06 07

    40/62

    PROGRAM:

    clearecho enter the no. of numbers in the seriesread n

    a=0b=1d=2echo $aecho $bwhile [ $d -lt $n ]do

    c=`expr $a + $b`echo $ca=$bb=$c

    d=`expr $d + 1`done

    OUTPUT:

    [krishna-edu balu]$ sh fibo

    enter the no. of numbers in the series

    50

    1

    1

    2

    3

    DEPARTMENT OF COMPUTER SCIENCE

    129

  • 8/8/2019 OS Lab Manual 06 07

    41/62

    EXPERIMENT 21

    AIM: To find the reverse of number.

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit.

    This program is to find the reverse of a number.

    ALGORITHM:

    STEP 1:Read the number.STEP 2:Initialize the value of reverse to 0.

    STEP 3:Repeat the following steps until the condition n>0 becomes false

    dig=`expr $n % 10`

    rev=`expr $rev \* 10 + $dig`

    n=`expr $n / 10`

    STEP 4:Echo Reverse

    DEPARTMENT OF COMPUTER SCIENCE

    130

  • 8/8/2019 OS Lab Manual 06 07

    42/62

  • 8/8/2019 OS Lab Manual 06 07

    43/62

    EXPERIMENT 22

    AIM: To find whether a given number is Armstrong number or not .

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit. This program is to find thereverse of a number.

    This program is to find whether a given number is Armstrong number or not .

    ALGORITHM:

    STEP 1.Read a numberSTEP 2.Initialize x to num and sum to 0

    STEP 3.Repeat the following statements until the condition [ $num -gt 0 ]

    becomes false

    y=`expr $num %10`

    z=`expr $y \* $y \* $y`

    sum=`expr $sum + $z`

    num=`expr $num / 10`

    STEP 4. If x is equal to sum then echo x as armstrong

    Else echo x as not Armstrong

    DEPARTMENT OF COMPUTER SCIENCE

    132

  • 8/8/2019 OS Lab Manual 06 07

    44/62

    PROGRAM

    echo -n "Enter a number :"read numx=$numsum=0while [ $num -gt 0 ]do

    y=`expr $num % 10`z=`expr $y \* $y \* $y`sum=`expr $num + $z`num=`expr $num / 10`

    doneif [ $x -eq $sum ]then

    echo "$x is an armstrong number"elseecho " $x is not an armstrong number"

    fi

    OUTPUT:

    [balu@krishna-edu balu]$ sh amstrong.sh

    enter a number :153153 is armstrong number

    [balu@krishna-edu balu]$

    DEPARTMENT OF COMPUTER SCIENCE

    133

  • 8/8/2019 OS Lab Manual 06 07

    45/62

    EXPERIMENT 23

    AIM: To check whether a give number is prime or not.

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit.

    This program is to check whether a given number is prime or not.

    ALGORITHM:

    STEP 1:Read the value of n.

    STEP 2:If the flag is equal to 0

    then the echo $n is not a prime.

    STEP 3:If the flag is equal to 1

    Then the echo $n is prime.

    STEP 4:End program.

    DEPARTMENT OF COMPUTER SCIENCE

    134

  • 8/8/2019 OS Lab Manual 06 07

    46/62

    PROGRAM:

    clearecho enter a numberread n

    i=2flag=1m=1

    j=`expr $n / 2`while [ $i -le $j ]do

    m=`expr $n % $i`if [ $m -eq 0 ]then

    flag=0break

    fii=`expr $i +1`

    doneif [ $flag -eq 1 ]then

    echo $n is primeelse

    echo $n is not a primefi

    OUTPUT:

    [krishna-edu balu]$ sh prime

    enter the number

    79

    79 is prime

    [krishna-edu balu]$

    DEPARTMENT OF COMPUTER SCIENCE

    135

  • 8/8/2019 OS Lab Manual 06 07

    47/62

    EXPERIMENT 24

    AIM: To read numbers from the user from command line and to displayits sum using until statement.

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit

    This program is for reading a number from the command line and to display its sum usinguntil statement.

    ALGORITHM:

    STEP 1. If the count of number of command line arguments is 0

    Then echo as invalid numbers

    STEP 2. Initialize the values of sum to 0 and I to 1 and n to $#

    STEP 3. Repeat the following statements until the condition becomes true

    sum=`expr $sum + $1`

    Increment the count of command line arguments

    STEP 4. .If n!=0 then echo the value of sum.

    DEPARTMENT OF COMPUTER SCIENCE

    136

  • 8/8/2019 OS Lab Manual 06 07

    48/62

    PROGRAM :

    if [ $# -eq 0 ]then

    echo " Invalid numbers"

    fisum=0i=1n=$#until [ $# -eq 0 ]do

    sum=`expr $sum + $i`shift

    done

    if [ $n -ne 0 ]

    thenecho " The sum of $n numbers is $sum"

    fi

    OUTPUT :

    [balu@krishna-edu shell]$ sh until_cmd_sum.sh 3 4

    The sum of 2 numbers is 7

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    137

  • 8/8/2019 OS Lab Manual 06 07

    49/62

    EXPERIMENT 25

    AIM:To read n numbers from the user from the command line and to display itssum using while statement

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shell

    executes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit

    This program is for reading numbers from the user from the command lineand to display its sum using while statement.

    ALGORITHM:

    STEP 1. If the count of the number of command line arguments is 0 then Echo as Invalid

    Numbers

    STEP 2. Initialize the values of sum to 0 and n to $#

    STEP 3. While $# is not equal to zero repeat the following steps

    sum=`expr $sum + $i`

    Increment the value of $#

    STEP 4. If n is not equal to 0 then echo the value of sum

    DEPARTMENT OF COMPUTER SCIENCE

    138

  • 8/8/2019 OS Lab Manual 06 07

    50/62

    PROGRAM:

    if [ $# -eq 0 ]then

    echo "Invalid numbers"fisum=0i=1n=$#while [ $# -ne 0 ]do

    sum=`expr $sum + $1`shift

    doneif [ $n -ne 0 ]then

    echo "The sum of $n numbers is $sum"fi

    OUTPUT :

    [balu@krishna-edu shell]$ sh while_cmd_sum.sh 4 5

    The sum of 2 numbers is 9

    balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    139

  • 8/8/2019 OS Lab Manual 06 07

    51/62

    EXPERIMENT 26

    AIM :-To read a numbers from the user from the command line and to display itssum using for statement

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface. The shellexecutes commands of the form:

    CommandName [argument1,..]

    The shell executes the program CommandName and passes argument on thecommand line to it. Then the shell waits for the command to exit

    This program is for reading a number from the user from the command line and to display itssum using for statement

    ALGORITHM :

    STEP 1. If the count of the number of command line arguments is 0 thenEcho as Invalid numbers

    STEP 2. Initialize the values of sum to 0 and n to $#

    STEP 3. for I in $*

    sum=`expr $sum + $I`

    Increment the value of $#

    STEP 4. If n is not equal to 0 then echo the value ofsum

    DEPARTMENT OF COMPUTER SCIENCE

    140

  • 8/8/2019 OS Lab Manual 06 07

    52/62

    PROGRAM:

    if [ $# -eq 0 ]then

    echo " Invalid numbers"fi

    sum=0n=$#for i in $*do

    sum=`expr $sum + $i`shift

    done

    if [ $n -ne 0 ]thenecho "The sum of $n numbers is $sum"

    fi

    OUTPUT :

    [balu@krishna-edu shell]$ sh for_cmd_sum.sh 4 5

    The sum of 2 numbers is 9

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    141

  • 8/8/2019 OS Lab Manual 06 07

    53/62

    EXPERIMENT 27

    AIM: To copy a string into another string, character by character.

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.

    The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command lineto it. Then the shell waits fro the command to exit.

    This program is to copy one string to another

    ALGORITHM:

    STEP 1.Read the value of the string

    STEP 2. Find the length of the string

    STEP 3. Initialize the value of a to 1

    STEP 4.Repeat the following statements until the condition a

  • 8/8/2019 OS Lab Manual 06 07

    54/62

    PROGRAM:

    echo -n "Enter the string :"read str

    len=`echo $str | wc -c `a=1while [ $a -lt $len ]do

    rev=`echo $str | cut -c$a `echo $reva=`expr $a + 1`temp=`echo $temp$rev`

    doneecho " The copied string is $temp"

    [balu@krishna-edu balu]$ sh strcpy.shEnter the string :krishnavasaviThe copied string is krishna[balu@krishna-edu balu]$

    DEPARTMENT OF COMPUTER SCIENCE

    143

  • 8/8/2019 OS Lab Manual 06 07

    55/62

    EXPERIMENT 28

    AIM: To reverse the given string

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command lineto it. Then the shell waits fro the command to exit.

    This program is for reversing the given string

    ALGORITHM:

    STEP 1.Read the value of string

    STEP 2. Find the length of the stringSTEP 3.Initialize the value of a

    STEP 4. Repeat the following steps until the condition length > 0 becomes false

    rev=`echo $str | cut -c $len`

    len=`expr $len - 1`

    temp=`echo $temp$rev`

    STEP 5.Echo the reversed string

    DEPARTMENT OF COMPUTER SCIENCE

    144

  • 8/8/2019 OS Lab Manual 06 07

    56/62

    PROGRAM :

    echo -n "Enter the string "read strlen=`echo $str | wc -c`

    while [ $len -gt 0 ]dorev=`echo $str | cut -c $len`len=`expr $len - 1`temp=`echo $temp$rev`

    done

    echo "The reversed string is $temp"

    [balu@krishna-edu shell]$ sh revers.shEnter the string abc

    The reversed string is cba[balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    145

  • 8/8/2019 OS Lab Manual 06 07

    57/62

    EXPERIMENT 29

    AIM: To convert lowercase letters to uppercase .

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command lineto it. Then the shell waits fro the command to exit.

    This program converts lowercase letters to uppercase

    ALGORITHM:

    STEP 1.Read the value of the string

    STEP 2.Use tr command to translate from lower letters to upper letters

    STEP 3.Echo the changed string

    DEPARTMENT OF COMPUTER SCIENCE

    146

  • 8/8/2019 OS Lab Manual 06 07

    58/62

    PROGRAM:

    echo -n "Enter a string "read strtemp=`echo $str | tr a-z A-Z`

    echo " The case changed string is $temp"

    OUTPUT:

    [balu@krishna-edu shell]$ sh lower_upper.sh

    Enter a string krishna

    The case changed string is KRISHNA

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    147

  • 8/8/2019 OS Lab Manual 06 07

    59/62

    EXPERIMENT 30

    AIM: To count the number of vowels in the string

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command lineto it. Then the shell waits fro the command to exit.

    This program is for counting the number of vowels in the string

    ALGORITHM:

    STEP 1. Read the value of the string

    STEP 2. count the characters of the string using wc command

    STEP 3. caculate the length of the string by subtracting the new line character

    STEP 4. Initialize the value of vowel as 0

    STEP 5. Repeat the following steps until the condition len>0 becomes false

    Cut each character of the string and store in temp

    Check if the value of temp is any of the letters a,e,i,o,u

    And increment the value of vowel

    Decrement the value of length

    STEP 6. Echo the number of vowels in the string

    DEPARTMENT OF COMPUTER SCIENCE

    148

  • 8/8/2019 OS Lab Manual 06 07

    60/62

    PROGRAM:

    echo -n "Enter th string "read strlen=1echo $str | wc -c`

    len=`expr $len - 1`vowel=0while [ $len -gt 0 ]do

    temp=`echo $str | cut -c $len`if [ $temp = athen

    vowel=`expr $vowel + 1`elif [ $temp = e ]then

    vowel=`expr $vowel + 1`

    elif [ $temp = i ]then

    vowel=`expr $vowel + 1`elif [ $temp = o ]then

    vowel=`expr $vowel + 1`elif

    [ $temp = u ]then

    vowel=`expr $vowel + 1`filen=`expr $len - 1`

    doneecho " the number of vowels in the string $str is $vowel"

    OUTPUT:

    [balu@krishna-edu shell]$ sh vowel.sh

    Enter th string aeiou

    the number of vowels in the string aeiou is 5

    [balu@krishna-edu shell]$

    DEPARTMENT OF COMPUTER SCIENCE

    149

  • 8/8/2019 OS Lab Manual 06 07

    61/62

    EXPERIMENT 31

    AIM: To replace a particular character in the string

    REQUIREMENTS :

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    THEORY:

    Shell is a user interface to the operating system built on the programming interface.The shell executes commands of the form:

    CommandName [ argument]

    The shell executes the program CommandName and passes argument on the command lineto it. Then the shell waits fro the command to exit.

    This program for replacing a particular character in the string

    ALGORITHM:

    STEP 1.Read the value of string

    STEP 2.Read the character to be changed

    STEP 3.Read the character to be replaced

    STEP 4.find the length of the string

    STEP 5.Initialize the value of count to 0 and I to 1

    STEP 6.Repeat the following statements until the condition I

  • 8/8/2019 OS Lab Manual 06 07

    62/62

    PROGRAM:

    echo -n "Enter the string :"read strecho -n "Enter the character to be counted:"

    read clen=`echo $str | wc -c`len=`expr $len - 1`count=0while [ $len -gt 0 ]do

    temp=`echo $str | cut -c $len`if [ $temp = $c ]then

    count=`expr $count + 1`fi

    len=`expr $len - 1`doneecho "The number of occurrence of the character $c in the string $str is $count"

    OUTPUT:

    [balu@krishna-edu last]$ sh count_char.sh

    Enter the string :krishna

    Enter the character to be counted:v

    The number of occurrence of the character v in the string krishna is 2

    [balu@krishna-edu last]$

    151