m.sc Shell Program

Embed Size (px)

Citation preview

  • 7/28/2019 m.sc Shell Program

    1/45

    Assignment no :Date :Assignment name : Write a shell program to check agiven no is odd or even

    echo "enter a no."read nc=` expr $n % 2 `if [ $c -eq 0 ];thenecho "The no is even"elseecho "The no is odd"fi

  • 7/28/2019 m.sc Shell Program

    2/45

    Output:-

    $ evenodd.shenter a no.65The no is odd$ evenodd.shenter a no.12The no is even

  • 7/28/2019 m.sc Shell Program

    3/45

    Assignment no :Date :Assignment name : Write a shell program to

    calculate the Factorial of a given number

    echo "Enter a number"read fif [ $f -lt 0 ];thenecho "Improper number"exitelif [ $f -eq 0 ];thenecho "The factorial of zero is 1"elsei=1c=1while [ $c -le $f ];doi=` expr $i \* $c|bc `c=` expr $c + 1 `doneecho "The factorial of $f is $i"fi

  • 7/28/2019 m.sc Shell Program

    4/45

    Output:-

    $ fact.shEnter a number0The factorial of zero is 1$ fact.shEnter a number-5Improper number

    $ fact.shEnter a number6The factorial of 6 is 720

  • 7/28/2019 m.sc Shell Program

    5/45

    Assignment no :Date :

    Assignment name : Write a shell program to print thefirst n Fibonacci number

    echo "Enter the nth position of the Fibonacci series"read nf=` expr $n - 1 `i=1a=0

    b=1

    echo "The series is "echo $awhile [ $i -le $f ];doc=` expr $a + $b `echo $ba=$b

    b=$ci=` expr $i + 1 `done

  • 7/28/2019 m.sc Shell Program

    6/45

    Output :-

    $ fib.shEnter the nth position of the Fibonacci series10The series is0112

    358132134

  • 7/28/2019 m.sc Shell Program

    7/45

    Assignment no :Date :Write a shell program to check whether anumber is Prime or not

    echo "Enter a number"read ni=2if [ $n -eq 1 ];thenecho "Improper number"elsef=` expr $n / 2 `

    while [ $i -lt $f ];dot=` expr $n % $i `if [ $t -eq 0 ];thenecho "The given number $n is not a prime number"exitfii=` expr $i + 1 `doneecho "The given number $n is a prime number"fi

  • 7/28/2019 m.sc Shell Program

    8/45

    Output :-

    $ prime.shEnter a number1Improper number$ prime.shEnter a number65The given number 65 is not a prime number$ prime.sh

    Enter a number23The given number 23 is a prime number

  • 7/28/2019 m.sc Shell Program

    9/45

    Assignment no :Date : Assignmentname : Write a shell program to display the non-fibonacci number up to a given number

    echo "Enter the number"read ni=1a=0

    b=1d=1echo "The non-fibonacci numbers are"

    while [ $d -le $n ];doc=` expr $a + $b `while [ $c -ne $d ];doecho $dd=` expr $d + 1 `if [ $d -eq $n ];thenif [ $c -ne $d ];thenecho $dexitfifidoned=` expr $d + 1 `a=$b

    b=$ci=` expr $i + 1 `done

  • 7/28/2019 m.sc Shell Program

    10/45

    Output :-

    $ nonfib.shEnter the number10The non-fibonacci numbers are467

    910$ nonfib.shEnter the number13The non-fibonacci numbers are467

    9101112

  • 7/28/2019 m.sc Shell Program

    11/45

    Assignment no :

    Date :Assignment name : Write a shell program to checkwhether a given number is palindrome or not

    echo "Enter a number"read nf=$nrev=0while [ $f -gt 0 ];doa=` expr $f % 10 `

    f=` expr $f / 10 `f1=` expr $rev \* 10 `rev=` expr $f1 + $a `doneecho "The reverse of $n is $rev"if [ $n -eq $rev ];thenecho "The given number is palondrome"elseecho "The given number is not palindrome"

    fi

  • 7/28/2019 m.sc Shell Program

    12/45

    Output:-

    $ palindrome.shEnter a number6254526The reverse of 6254526 is 6254526The given number is palondrome$ palindrome.shEnter a number

    54123The reverse of 54123 is 32145The given number is not palindrome

  • 7/28/2019 m.sc Shell Program

    13/45

    Assignment no : Date

    : Assignment name :Write a shell program to check whether a givennumber is Armstrong or not

    echo "Enter a number"read narm=0l=` expr $n|wc -c `temp=$n

    while [ $temp -gt 0 ];dotemp1=` expr $temp % 10 `temp2=$temp1i=2while [ $i -lt $l ];dotemp1=` expr $temp1 \* $temp2 `i=` expr $i + 1 `donearm=` expr $arm + $temp1 `temp=` expr $temp / 10 `

    doneif [ $arm -eq $n ];thenecho "The given number is armstrong number"elseecho "The given number is not a armstrong number"fi

  • 7/28/2019 m.sc Shell Program

    14/45

    Output:-

    $ armstrong.shEnter a number452The given number is not a armstrong number$ armstrong.shEnter a number

    153The given number is armstrong number

  • 7/28/2019 m.sc Shell Program

    15/45

    Assignment no :Date : Assignmentname : Write a shell program to convert a givennumber from decimal binary

    echo "Enter a number"read necho "Enter Input Base"read ib

    echo "Enter Output Base"read obout=` echo "obase=$ob;ibase=$ib;$n"|bc echo "The number $n is converted into $out"

  • 7/28/2019 m.sc Shell Program

    16/45

    Output:-

    $ d2b.shEnter a number69Enter Input Base10Enter Output Base2The number 69 is converted into 1000101

  • 7/28/2019 m.sc Shell Program

    17/45

    Assignment no :Date:Assignment name : Write a shell program to checkwhether a year is leap year or not

    echo "Enter a Year"

    read ya=` expr $y % 400 `

    b=` expr $y % 100 `c=` expr $y % 4 `if [ $a -eq 0 ];thenecho "$y is a Leap-year"elif [ $b -ne 0 -a $c -eq 0 ];thenecho "$y is a Leap-year"else

    echo "$y is not a Leap-year"fi

  • 7/28/2019 m.sc Shell Program

    18/45

    Output:-

    $ leapyear.shEnter a Year20002000 is a Leap-year$ leapyear.shEnter a Year19981998 is not a Leap-year$ leapyear.shEnter a Year19801980 is a Leap-year

  • 7/28/2019 m.sc Shell Program

    19/45

    Assignment no :Date : Assignmentname: Write a shell program to calculate GCD of twonumbers

    echo "Enter two numbers"read a bif [ $a -gt $b ];thent=$aa=$b

    b=$tfir=` expr $b % $a `while [ $r -ne 0 ];do

    b=$aa=$rr=` expr $b % $a `doneecho "The GCD of two numbers is $a"

  • 7/28/2019 m.sc Shell Program

    20/45

    Output:-

    $ gcd.sh

    Enter two numbers36 9The GCD of two numbers is 9$ gcd.shEnter two numbers16 96The GCD of two numbers is 16$ gcd.shEnter two numbers23 63

    The GCD of two numbers is 1

  • 7/28/2019 m.sc Shell Program

    21/45

    Assignment no :Date : Assignmentname :Write a shell program to copy the content onefile to another file. Filename given as command lineargument

    sfile=$1dfile=$2echo "Display the source file"cat $sfileecho "Copy the file"cp $sfile $dfileecho "Display the destination file"cat $dfile

  • 7/28/2019 m.sc Shell Program

    22/45

    Output:-

    $ cf.sh f1 f2Display the source fileWE ARE THE STUDENT OF VIDYASAGAR UNIVERSITYCopy the fileDisplay the destination fileWE ARE THE STUDENT OF VIDYASAGAR UNIVERSITY

  • 7/28/2019 m.sc Shell Program

    23/45

    Assignment no :

    Date : Assignmentname : Write a shell program to calculate GCD andLCM of two numbers

    echo "Enter two numbers"read a bif [ $a -gt $b ];thent=$aa=$b

    b=$tfi

    p=` expr $a \* $b `r=` expr $b % $a `while [ $r -ne 0 ];do

    b=$aa=$rr=` expr $b % $a `donel=` expr $p / $a `

    echo "The GCD of two numbers is $a"echo "The LCM of two numbers is $l"

  • 7/28/2019 m.sc Shell Program

    24/45

    Output:-$ gcd.shEnter two numbers5 6The GCD of two numbers is 1The LCM of two numbers is 30$ gcd.shEnter two numbers

    36 6The GCD of two numbers is 6The LCM of two numbers is 36$ gcd.shEnter two numbers28 6The GCD of two numbers is 2The LCM of two numbers is 84

  • 7/28/2019 m.sc Shell Program

    25/45

    Assignment no :Date : Assignmentname : Write a shell program to calculate NCR

    echo "Enter the value of n and r"read n rs1=1

    i=1while [ $i -le $n ];dos1=` expr $s1 \* $i `i=` expr $i + 1 `dones2=1i=1while [ $i -le $r ];dos2=` expr $s2 \* $i `i=` expr $i + 1 `

    dones3=1i=1

    p=` expr $n - $r `while [ $i -le $p ];dos3=` expr $s3 \* $i `

  • 7/28/2019 m.sc Shell Program

    26/45

    i=` expr $i + 1 `donem=` expr $s2 \* $s3 `t=` expr $s1 / $m `

    echo "The result is $t"

    Output:-

    $ ncr.shEnter the value of n and r5 3The result is 10$ ncr.shEnter the value of n and r9 4The result is 126

  • 7/28/2019 m.sc Shell Program

    27/45

    Assignment no :Date : Assignmentname : Write a shell program to calculate NPR

    echo "Enter the value of n and r"read n r

    s1=1i=1while [ $i -le $n ];dos1=` expr $s1 \* $i `i=` expr $i + 1 `dones3=1i=1

    p=` expr $n - $r `

    while [ $i -le $p ];dos3=` expr $s3 \* $i `i=` expr $i + 1 `donet=` expr $s1 / $s3 `echo "The result is $t"

  • 7/28/2019 m.sc Shell Program

    28/45

    Output:-

    $ npr.shEnter the value of n and r

    5 3The result is 60$ npr.shEnter the value of n and r9 4The result is 3024

  • 7/28/2019 m.sc Shell Program

    29/45

    Assignment no :Date: Assignmentname : Write a shell program to remove all numbersthat occurs more than one in a shorted array

    echo "Enter the array range"read ni=1while [ $i -le $n ];doread a[i]i=` expr $i + 1 `doneecho ${a[*]}i=1

    while [ $i -le $n ];dotemp=${a[$i]}echo $temp>>f2i=` expr $i + 1 `donei=0for i in `cat f2`;do

  • 7/28/2019 m.sc Shell Program

    30/45

    echo $i>>f3donesort -u f3>>f4i=1

    for j in `cat f4`;doa[$i]=$ji=` expr $i + 1 `done

    j=1echo "The final result-----------"while [ $j -lt $i ];doecho ${a[$j]}

    j=` expr $j + 1 `done

    Output:-

    $ delda.shEnter the array range765259565336533

    65 25 95 65 33 65 33The final result-----------25336595

  • 7/28/2019 m.sc Shell Program

    31/45

    Assignment no :Date :Assignment name : Write the list of files which have

    read , write and execute permission

    for fname in `ls l` ;doif [ -f $fname a r $fname a w $fname a x $fname ];thenecho "$fname has all permission"fidone

  • 7/28/2019 m.sc Shell Program

    32/45

    Output:-

    SSS.SH has all permission

  • 7/28/2019 m.sc Shell Program

    33/45

    Assignment no :

    Date :Assignment name : Write a shell program to convert agiven number from binary decimal

    echo "Enter a number"read necho "Enter Input Base"read ibecho "Enter Output Base"read ob

    out=` echo "obase=$ob;ibase=$ib;$n"|bc echo "The number $n is converted into $out"

  • 7/28/2019 m.sc Shell Program

    34/45

    Output:-

    $ d2b.shEnter a number1000101Enter Input Base2Enter Output Base10The number 1000101 is converted into 69

  • 7/28/2019 m.sc Shell Program

    35/45

    Assignment no :Date : Assignment name :Write a Shell script to apply binary search method onvirtual array.

    #binary search over an array.....clearecho -en "\n\nEnter the total no. of elements: "read n

    m=0while [ $m -lt $n ] ; dolet k=m+1echo -en "\n\nEnter the element $k: "read a[$m]m=`expr $m + 1`

    doneecho -en "\n\nEnter the element to be searched: "

  • 7/28/2019 m.sc Shell Program

    36/45

    read snbeg=0end=`expr $n - 1`while [ $beg -le $end ] ; do

    mid=`expr $beg + $end`

    mid=`expr $mid / 2`if [ $sn -eq ${a[$mid]} ] ; then

    breakelif [ $sn -lt ${a[$mid]} ] ; then

    end=`expr $mid - 1`else

    beg=`expr $mid + 1`fi

    doneif [ $beg -le $end ] ; then

    mid=`expr $mid + 1`

    echo -e "\n\nElement found at: $mid"else

    echo -e "\n\nElement is not found !!!!!"fiecho -e "\n\nEnd of program ......."

    OUTPUT:

    Enter the total no. of elements: 8

    Enter the element 1: 7Enter the element 2:47Enter the element 3: 16Enter the element 4: 20Enter the element 5: 1Enter the element 6: 14

    Enter the element 7: 2Enter the element 8: 10

    Enter the element to be searched: 2

    Element found at: 7

    End of program .......

  • 7/28/2019 m.sc Shell Program

    37/45

    Assignment no : Date :Assignment name : Write a Shell script to apply selectionsorting method on virtual array

    #Selection_sort over an array......clearecho -en "\n\nEnter the total no. of elements: "read nm=0while [ $m -lt $n ] ; do

    k=`expr $m + 1`

    echo -en "\n\nEnter element no. $k: "read a[$m]m=`expr $m + 1`

    doneecho -e "\n\nYour entered sequence is:-->"m=0while [ $m -lt $n ] ; do

    k=`expr $m + 1`

  • 7/28/2019 m.sc Shell Program

    38/45

    echo -en "\n\nThe element $k is: ${a[$m]} "m=`expr $m + 1`

    donei=0while [ $i -lt $n ] ; do

    min=${a[$i]}pos=$ij=`expr $i + 1`while [ $j -lt $n ] ; do

    if [ ${a[$j]} -lt $min ] ; thenmin=${a[$j]}pos=$j

    fij=`expr $j + 1`

    donet=${a[$i]}a[$i]=$mina[$pos]=$ti=`expr $i + 1`

    doneecho -e "\n\nSorted sequence:-->"m=0while [ $m -lt $n ] ; do

    k=`expr $m + 1`echo -en "\n\nThe element $k is: ${a[$m]} "m=`expr $m + 1`

    doneecho -e "\n\nEnd of program...."

    OUTPUT:

    Enter the total no. of elements: 6

    Enter element no. 1: 2Enter element no. 2: 15Enter element no. 3: 10Enter element no. 4: 7

    Enter element no. 5: 1Enter element no. 6: 5

    Your entered sequence is:-->

    The element 1 is: 2The element 2 is: 15The element 3 is: 10

  • 7/28/2019 m.sc Shell Program

    39/45

    The element 4 is: 7The element 5 is: 1The element 6 is: 5

    Sorted sequence:-->

    The element 1 is: 1The element 2 is: 2The element 3 is: 5The element 4 is: 7The element 5 is: 10The element 6 is: 15

    Assignment no :Date :

    Assignment name : Write a Shell script to apply insertionsorting method on virtual array

    #insertion_sort over an array......clearecho -en "\n\nEnter the total no. of elements: "read nm=0while [ $m -lt $n ] ; do

    k=`expr $m + 1`echo -en "\n\nEnter element no. $k: "read a[$m]m=`expr $m + 1`

    doneecho -e "\n\nYour entered sequence is:-->"m=0while [ $m -lt $n ] ; do

  • 7/28/2019 m.sc Shell Program

    40/45

    k=`expr $m + 1`echo -en "\n\nThe element $k is: ${a[$m]} "m=`expr $m + 1`

    donei=1

    while [ $i -lt $n ] ; dotemp=${a[$i]}j=$iwhile [ $j -gt 0 ] ; do

    h=`expr $j - 1`if [ $temp -lt ${a[$h]} ] ; then

    a[$j]=${a[$h]}else

    breakfij=`expr $j - 1`

    donei=`expr $i + 1`a[$j]=$temp

    doneecho -e "\n\nSorted sequence:-->"m=0while [ $m -lt $n ] ; do

    k=`expr $m + 1`echo -en "\n\nThe element $k is: ${a[$m]} "m=`expr $m + 1`

    done

    echo -e "\n\nEnd of program...."

    OUTPUT:

    Enter the total no. of elements: 6

    Enter element no. 1: 4Enter element no. 2: 7Enter element no. 3: 12Enter element no. 4: 17

    Enter element no. 5: 1Enter element no. 6: 5

    Your entered sequence is:-->

    The element 1 is: 4The element 2 is: 7The element 3 is: 12

  • 7/28/2019 m.sc Shell Program

    41/45

    The element 4 is: 17The element 5 is: 1The element 6 is: 5

    Sorted sequence:-->

    The element 1 is: 1The element 2 is: 4The element 3 is: 5The element 4 is: 7The element 5 is: 12The element 6 is: 17

    Assignment no :Date:

    Assignment name :Program of linear search.

    echo Enter the no. of elementsread ni=1

    while test $i -le $ndo

    num=a$iecho Enter the search elementread $numi=`expr $i + 1`

    done

  • 7/28/2019 m.sc Shell Program

    42/45

    echo Enter the itemread itemi=1

    while test $i -ie $ndo

    var =a$iif eval [\$$var -eq $item]thenecho Search is successfulecho position of item is $iexit

    fii=`expr $i + 1`

    done

    echo Search is unsuccessful

    Output

    Enter the no. of elements

    5

    Enter the element

    1220456321

  • 7/28/2019 m.sc Shell Program

    43/45

    Enter the search element

    20

    Search is successfulPosition of item is 2

    Enter the search element

    23

    Search is unsuccessful

    Assignment no :Date : Assignmentname : write a shell program for bubble sort

    echo Enter no of elementsread ni=1while [$i -le $n]do

    num=a$iecho Enter the elementread $numi=`expr $i + 1`

    done

  • 7/28/2019 m.sc Shell Program

    44/45

    i=1while [$i -lt $n]do

    j=1

    while [$j -le $ ` expr $n - $i `]dovar1=a$jvar2=a`expr $j +1`if eval [ \$$var1 -gt \$$var2]theneval t =\$$var1eval $var1=\$$var2eval $var2=$tfi

    j=`expr $j + i`done

    i=`expr $i + 1`doneecho The sorted list

    i=1

    while[$i -le $n]

    do num=a$ieval echo \$$numi=`expr $i +1 `

    done

    Output

    Enter the no. of elements

    5

    Enter the element

    12

  • 7/28/2019 m.sc Shell Program

    45/45

    20456321

    The sorted list is

    1220214563