Few Shell Programming for Computer Science Students

download Few Shell Programming for Computer Science Students

of 28

description

Few Shell Programming for Computer Science Students

Transcript of Few Shell Programming for Computer Science Students

  • 1

    1

    ASSIGNMENT : 1

    PROBLEM: Write a shell script to find out whether a number is even or odd number.

    ALGORITHM: EVENODD(N)

    Here N is the given number. This algorithm checks whether N is even or odd.

    1. Set R = N mod 2.

    2. If R = 0 , then :

    Print : N is Even.

    Else :

    Print : N is Odd.

    3. Exit.

    CODE: clear

    echo -e "\t\tCHECK WHETHER EVEN OR ODD NUMBER\n"

    echo -en 'Enter Number : '

    readnum

    if [ `expr $num % 2` -eq 0 ];then

    echo $num 'is Even'

    else

    echo $num 'is Odd'

    fi

    echo -e

    OUTPUT: Output 1 :

    CHECK WHETHER EVEN OR ODD NUMBER

    Enter Number : 56

    56 is Even

    Output 2 :

    CHECK WHETHER EVEN OR ODD NUMBER

    Enter Number : 89

    89 is Odd

    DISCUSSION: 1. A number is even if it is divisible by 2 otherwise it is an odd number.

    2. We have used mod(%) operation to determine whether the number is divisible by 2 or not.

  • 2

    2

    ASSIGNMENT : 2

    PROBLEM: Write a shell script to calculate the FACTORIAL of some user given numbers.

    ALGORITHM: FACTORIAL(N)

    Here N is user given number. This algorithm calculates the factorial of N.

    4. Set I = 1. [ Initialize Counter. ]

    Set FACTORIAL = 1.

    5. Repeat Step (a) to (b) while I

  • 3

    3

    ASSIGNMENT : 3

    PROBLEM: Write a shell script to generate the FIBONACCI SERIES upto nth term.

    ALGORITHM: FIBONACCI ( N )

    This algorithm finds the Fibonacci series uptoNth term.

    8. Set FIRST =0 and SECOND = 1.

    9. Print the first two Fibonacci numbers FIRST and SECOND.

    10. Repeat step 5 to 7 forI = 2 to N 1 :

    11. THIRD = FIRST + SECOND

    12. Show the next Fibonacci number THIRD.

    13. Set FIRST = SECOND, SECOND = THIRD.

    14. Exit.

    CODE: clear

    echo -en 'Enter number of terms : '

    read n

    a=0

    b=1

    echo 'The fibonacci Series is :'

    echo -en $a '\t' $b

    i=2

    while [ $i -lt $n ]

    do

    c=`expr $a + $b`

    echo -en '\t' $c

    a=$b

    b=$c

    i=`expr $i + 1`

    done

    echo -e

    OUTPUT: FIBONACCI SEQUENCE

    Enter number of terms : 9

    The fibonacci Series is :

    0 1 1 2 3 5 8 13 21

    DISCUSSION: 6. The Fibonacci series is a series whose first two numbers are 0 and 1 respectively, and the next

    numbers are the sum of the two previous numbers.

    7. Thus the Fibonacci series up to nth term is :

    0, 1, (1st + 2nd number),, ((n-2)th + (n-1)th number)

    8. This shell script generates the Fibonacci series up to nth term, where n is a user input number.

  • 4

    4

    ASSIGNMENT : 4

    PROBLEM: Write a shell script to generate the factors of a number.

    ALGORITHM: FACTOR ( N )

    Here N is a user given number. This algorithm finds all the factors of N.

    1. Set I = 1.

    2. Repeat Step 3 to 4 while I

  • 5

    5

    ASSIGNMENT : 5

    PROBLEM: Write a shell script to find whether a number is prime or not.

    ALGORITHM: PRIME(N)

    Here N is the user given number. This algorithm finds whether N is prime or not.

    1. Set I = 2. [ Initialize counter. ]

    2. Repeat step 3 to 6 while I < N:

    3. Set REM = N mod I.

    4. If REM = 0 then:

    Set FLAG = 1 and Goto step 7.

    5. Else

    Set FLAG=0

    6. Set I = I +1. [ Increase Counter. ]

    7. If FLAG = 0 then :

    Write : N is Prime Number.

    8. Else

    Write : N is not Prime Number.

    9. Exit.

    CODE: clear

    echo -en "Enter number : "

    read n

    i=2

    while [ $i -lt $n ];do

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

    flag=1

    break

    else

    flag=0

    fi

    i=`expr $i + 1`

    done

    if [ $flag -eq 0 ];then

    echo $n "is not a prime number"

    else

    echo $n "is a prime number"

    fi

    OUTPUT: Output 1 :

    CHECK WHETHER PRIME NUMBER OR NOT

    Enter number : 77

    77 is not a prime number

    Output 2 :

    CHECK WHETHER PRIME NUMBER OR NOT

  • 6

    6

    Enter number : 97

    97 is a prime number

    DISCUSSION: 11. A number is called a Prime number when it is divisible by 1 and that number only.

    12. A Prime number has only two factors, i.e. 1 and that number.

  • 7

    7

    ASSIGNMENT : 6

    PROBLEM: Write a shell script to sort an array of integers in ascending order using BUBBLE SORT.

    ALGORITHM: BUBBLE(LIST,N)

    Here LIST is an array with N elements. This algorithm sorts the elements in LIST.

    15. Repeat Steps 2 and 3 for I=0 to N-1.

    16. Set J=0. [Initializes pass pointer J]

    17. Repeat while JLIST[J+1], then:

    Interchange LIST[J]>LIST[J+1].

    [End of If structure.]

    (b) Set J=J+1.

    [End of inner loop.]

    [End of Step 1 outer loop.]

    18. Exit.

    CODE: clear

    echo -e "\t\t\tBUBBLE SORT\n"

    echo -en "Enter no. of elements : "

    read n

    echo -e "Enter elements in array :"

    i=0

    while [ $i -lt $n ]; do

    read a[$i]

    i=`expr $i + 1`

    done

    echo -e "You have entered :"

    i=0

    while [ $i -lt $n ]; do

    echo -en ${a[$i]} "\t"

    i=`expr $i + 1`

    done

    i=0

    while [ $i -lt $n ];do

    j=0

    r=`expr $n - $i`

    t=`expr $r - 1`

    while [ $j -lt $t ];do

    k=`expr $j + 1`

    if [ ${a[$j]} -gt ${a[$k]} ];then

    temp=${a[$j]}

    a[$j]=${a[$k]}

    a[$k]=$temp

    fi

    j=`expr $j + 1`

    done

    i=`expr $i + 1`

    done

    echo -e "\n\nThe sorted array :"

    i=0

  • 8

    8

    while [ $i -lt $n ]; do

    echo -en ${a[$i]} "\t"

    i=`expr $i + 1`

    done

    echo -e "\n"

    OUTPUT: BUBBLE SORT

    Enter no. of elements : 7

    Enter elements in array :

    59

    44

    -16

    70

    0

    32

    81

    You have entered :

    59 44 -16 70 0 32 81

    The sorted array :

    -16 0 32 44 59 70 81

    DISCUSSION: 13. Bubble sort is very easy to implement, and this is reasonable to the user for small number of

    elements to sort.

    14. This program sorts the integers in ascending order.

    15. In the best case, where the array is already sorted, the program requires only one pass. In that

    pass, (n-1) comparisons are made. Thus the complexity of Bubble sort for best case is : O(n) .

    16. In the worst case, where the array is sorted in the reverse order required, the program requires

    (n-1) passes. Thus the complexity for worst case is : O(n2).

    17. In the average case, also the complexity is : O(n2).

  • 9

    9

    ASSIGNMENT : 7

    PROBLEM: Write a shell script to checkwhether a given string is palindrome or not.The string should be taken

    input as command line argument.

    ALGORITHM: PALINDROME ( STR )

    Here STR is an user given string. This algorithm checks whether STR is palindrome or not.

    19. Set L = Length of STR.

    20. Set FLAG = 0 and I = 1.

    21. Repeat Step (a) to (c) while I

  • 10

    10

    Output 2 :

    [atreyi@localhost ~]$ sh Palindrome.sh SHELL

    This is not a Palindrome String

    DISCUSSION: 1. Here cut command is used to cut out nth character form the string, where n is a parameter of

    cut, supplied by the user.

    2. We have compared the 1st character of the string with the last one, 2nd with the 2nd last, and so

    on. If these couples of characters get matched to each other, then it is a palindrome string,

    otherwise, not palindrome.

  • 11

    11

    ASSIGNMENT : 8

    PROBLEM: Write a shell script to find the largest number from three user given numbers, which are taken input

    as command line argument.

    ALGORITHM: LARGEST ( A, B, C )

    Here A, B and Care three user given numbers. This algorithm finds the largest number among them.

    1. If A > B, then :

    Go to Step 2.

    Else :

    Go to Step 3.

    2. If A > C, then :

    Write : A is Largest.

    Else :

    Write : C is Largest.

    [End of inner If structure.]

    3. Go to Step 5.

    4. If B> C, then :

    Write : B is Largest.

    Else :

    Write : C is Largest.

    [ End of inner If structure. ]

    [ End of outer If structure. ]

    5. Exit.

    CODE: a=$1

    b=$2

    c=$3

    if [ $a -gt $b ];then

    if [ $a -gt $c ];then

    echo -e $a "is largest"

    else

    echo -e $c "is largest"

    fi

    else

    if [ $b -gt $c ];then

    echo -e $b "is largest"

    else

    echo -e $c "is largest"

    fi

    fi

    echo -e

    OUTPUT: Output 1 :

    [atreyi@Atreyi ~]$ sh Largest.sh 54 12 45

    54 is largest

  • 12

    12

    Output 2 :

    [atreyi@Atreyi ~]$ sh Largest.sh 23 17 65

    65 is largest

    Output 3 :

    [atreyi@Atreyi ~]$ sh Largest.sh 38 95 32

    95 is largest

    DISCUSSION: 1. This shell script finds the largest number from three given numbers.

    18. The numbers are taken as input using command line argument.

  • 13

    13

    ASSIGNMENT :9

    PROBLEM: Write a shell script to find all the numbers within a user given range that are divisible by 7 and

    calculate their sum.

    ALGORITHM: DIVSEVEN ( LOWER, UPPER )

    Here LOWER and UPPER are two user given numbers. This algorithm finds all the numbers between

    LOWER and UPPER that are divisible by 7 and calculate their sum.

    24. Set Sum = 0 and I = LOWER.

    25. Repeat Step 3 to 5 while I

  • 14

    14

    The numbers divisible by 7 are :

    21 28 35 42 49 56 63 70 77 84 91 98

    105 112

    Sum of the numbers = 931

    DISCUSSION: 19. This program takes two numbers as input from user and finds all the numbers that are divisible

    by 7.

    20. This program checks whether the remainder after dividing a number by 7 is 0 or not. If a number

    gives 0 as remainder then it prints it.

    21. It also finds the sum of the printed numbers.

  • 15

    15

    ASSIGNMENT :10

    PROBLEM: Write a shell script to reverse a string.

    ALGORITHM: REVERSE(A,B)

    Here A and B are two character arrays. This algorithm reverses the string A and stores it into B. LEN

    holds the length of the string to be reversed.

    32. [Initialize variables]

    Set LEN=0.

    33. While A[L]NULL :

    Set LEN=LEN+1.

    [End of while loop. ]

    34. Set J=0.

    35. Repeat step 5 and 6 for I=LEN-1 to 0.

    36. Set B[J]=A[I].

    37. Set J=J+1. [Increase counter.]

    38. Set I=I+1. [Increase pass counter.]

    [End of Step 4 loop. ]

    39. Set B[J]=NULL.

    40. Exit.

    CODE: clear

    echo -e "\t\t\tREVERSE A STRING\n"

    echo -en "Enter String : "

    readstr

    len=`echo $str|wc -c`

    len=`expr $len - 1`

    flag=0

    while [ $len -gt 0 ];do

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

    rev=$rev$ct

    len=`expr $len - 1`

    done

    echo -e "Reversed String : "$rev

    echo -e

    OUTPUT: REVERSE A STRING

    Enter String : THIS PROGRAM REVERSES A STRING

    Reversed String : GNIRTS A SESREVER MARGORP SIHT

    DISCUSSION: 1. This program takes a string input and displays the string in reversedorder.

    2. To reverse the given string cut command is used here. This command cuts out nth character

    form the value passed through it, where n is a parameter of cut, supplied by the user.

  • 16

    16

    ASSIGNMENT : 11

    PROBLEM: Write a shell script to perform arithmetical operations on two numbers.

    ALGORITHM: 1. Take input two numbers.

    2. Take users choice to perform one of the mentioned operation : addition, subtraction,

    multiplication and division.

    3. Show the result.

    4. Exit.

    CODE: clear

    echo -e "\t\tCALCULATOR"

    while [ 1 ];do

    echo -e "\nChoose one option"

    echo -e "1. Addition.\n2. Subtraction."

    echo -e "3. Multiplication.\n4. Division.\n5. Exit."

    echo -en "Enter your choice : "

    read ch

    case $ch in

    1) echo -en "\nEnteraddend : "

    read n1

    echo -en "Enter augend : "

    read n2

    res=`expr $n1 + $n2`

    echo -e "Sum =" $res

    ;;

    2) echo -en "\nEnter minuend : "

    read n1

    echo -en "Enter subtrahend : "

    read n2

    res=`expr $n1 - $n2`

    echo -e "Difference =" $res

    ;;

    3) echo -en "\nEntermultiplicant : "

    read n1

    echo -en "Enter multiplier : "

    read n2

    res=`expr $n1 \* $n2`

    echo -e "Product =" $res

    ;;

    4) echo -en "\nEnter dividend : "

    read n1

    echo -en "Enter divisor : "

    read n2

    res=`expr $n1 / $n2`

    echo -e "Quotient =" $res

    ;;

    5) echo -e

    break

    ;;

    *) echo -e "\nWrong input. Try again."

  • 17

    17

    ;;

    esac

    done

    OUTPUT: CALCULATOR

    Choose one option

    1. Addition.

    2. Subtraction.

    3. Multiplication.

    4. Division.

    5. Exit.

    Enter your choice : 1

    Enter addend : 13

    Enter augend : 15

    Sum = 28

    Choose one option

    1. Addition.

    2. Subtraction.

    3. Multiplication.

    4. Division.

    5. Exit.

    Enter your choice : 2

    Enter minuend : 45

    Enter subtrahend : 23

    Difference = 22

    Choose one option

    1. Addition.

    2. Subtraction.

    3. Multiplication.

    4. Division.

    5. Exit.

    Enter your choice : 3

    Enter multiplicant : 3

    Enter multiplier : 8

    Product = 24

    Choose one option

    1. Addition.

    2. Subtraction.

    3. Multiplication.

    4. Division.

    5. Exit.

    Enter your choice : 4

    Enter dividend : 96

    Enter divisor : 16

    Quotient = 6

    Choose one option

    1. Addition.

    2. Subtraction.

    3. Multiplication.

  • 18

    18

    4. Division.

    5. Exit.

    Enter your choice : 7

    Wrong input. Try again.

    Choose one option

    1. Addition.

    2. Subtraction.

    3. Multiplication.

    4. Division.

    5. Exit.

    Enter your choice : 5

    DISCUSSION: 22. This is a menu driven program where multiple tasks can be done within a program.

    23. In this program we can do addition, subtraction, multiplication, division of two user given

    numbers.

  • 19

    19

    ASSIGNMENT : 12

    PROBLEM: Write a shell script to checkwhether a given number is an Armstrong number or not.The number

    should be taken input as command line argument.

    ALGORITHM: ARMSTRONG(N)

    Here N is an integer. This algorithm finds if N is an ARMSTRONG number or not.

    41. [Initialize variable]Set SUM=0, FLAG=0 and T=N.

    42. Repeat Steps 3 to 5 while T0.

    43. Set D=T mod 10.

    44. Set CUBE=D*D*D.

    45. Set SUM=SUM+CUBE.

    46. Set T=T/10.

    [End of Step 2 loop.]

    47. If SUM=N, then:

    Write : ARMSTRONG NUMBER.

    Else

    Write: NOT ARMSTRONG NUMBER.

    [End of If structure.]

    48. Exit.

    CODE: num=$1

    n=$num

    sum=0

    while [ $n -gt 0 ];do

    d=`expr $n % 10`

    cube=`expr $d \* $d \* $d`

    sum=`expr $sum + $cube`

    n=`expr $n / 10`

    done

    if [ $sum -eq $num ];then

    echo -e $num "is an Armstrong Number\n"

    else

    echo -e $num "is not an Armstrong Number\n"

    fi

    OUTPUT: Output 1 :

    [atreyi@Atreyi ~]$ sh Armstrong.sh 153

    153 is an Armstrong Number

    Output 2 :

    [atreyi@Atreyi ~]$ sh Armstrong.sh 245

    245 is not an Armstrong Number

    DISCUSSION: 3. The number is equal to the sum of the cube of its each digits.

  • 20

    20

    4. This programs takes a number as command line argument and check if it is an Armstrong

    number or not.

  • 21

    21

    ASSIGNMENT : 13

    PROBLEM: Write a shell script to print the following pattern,

    1

    2 2

    3 3 3

    4 4 4 4

    5 5 5 5 5

    ALGORITHM: PATTERN1(N)

    Here N is number of lines the pattern should contain. This algorithm prints the required pattern.

    49. Set J = 1.

    50. Repeat Step 3 to 8 for I = 0 to N-1 :

    51. Repeat Step 4 and5 for K = 0 to I :

    52. Print J.

    53. Increment K by 1.

    [ End of inner loop. ]

    54. Increment J by 1.

    55. Increment I by 1.

    56. Print New Line Character.

    [ End of outer loop. ]

    57. Exit.

    CODE: clear

    echo -e "\t\tPRINT PATTERN 1\n"

    echo -en "Enter number of lines : "

    read n

    i=0

    j=1

    while [ $i -lt $n ]

    do

    k=0

    while [ $k -le $i ]

    do

    echo -en $j" "

    k=`expr $k + 1`

    done

    j=`expr $j + 1`

    i=`expr $i + 1`

    echo -e

    done

    echo -e "\t~~~ END ~~~\t\n"

  • 22

    22

    OUTPUT: PRINT PATTERN 1

    Enter number of lines : 6

    1

    2 2

    3 3 3

    4 4 4 4

    5 5 5 5 5

    6 6 6 6 6 6

    ~~~ END ~~~

    DISCUSSION: 24. Two loops are required, one for rows and another for columns.

    25. Here we have used nested while loops.

  • 23

    23

    ASSIGNMENT :14

    PROBLEM: Write a shell script to print the following pattern,

    |_

    | |_

    | | |_

    | | | |_

    | | | | |_

    ALGORITHM: PATTERN3(N)

    Here N is number of lines the pattern should contain. This algorithm prints the required pattern.

    58. Repeat Step 2 to 8 for I = 0 to N-1 :

    59. Repeat Step 4 to 6 for K = 0 to I :

    60. Print | .

    61. Increment K by 1.

    [ End of inner loop. ]

    62. Increment I by 1.

    63. Print _.

    64. Print New Line Character.

    [ End of outer loop. ]

    65. Exit.

    CODE: clear

    echo -e "\t\tPRINT PATTERN 3\n"

    echo -en "Enter number of lines : "

    read n

    i=0

    while [ $i -lt $n ]

    do

    k=0

    while [ $k -le $i ]

    do

    echo -en "| "

    k=`expr $k + 1`

    done

    i=`expr $i + 1`

    echo -e "\b_"

    done

    echo -e "\n\t~~~ END ~~~\t\n"

  • 24

    24

    OUTPUT: PRINT PATTERN 3

    Enter number of lines : 6

    |_

    | |_

    | | |_

    | | | |_

    | | | | |_

    | | | | | |_

    ~~~ END ~~~

    DISCUSSION: 26. Two loops are required, one for rows and another for columns.

    27. Here we have used nested while loops.

  • 25

    25

    ASSIGNMENT : 15

    PROBLEM: Write a shell script to print the following pattern,

    * * * * * * * * * * * * * * * * * * * * *

    ALGORITHM: PATTERN3 (N)

    Here N is number of lines the pattern should contain. This algorithm prints the required pattern.

    66. Repeat Step 2 to 9 for I = 0 to N-1 :

    67. Repeat Step 4and5 for K = 0 to I :

    68. Print *.

    69. Increment K by 1.

    [ End of inner loop. ]

    70. Increment I by 1.

    71. Print New Line Character.

    [ End of outer loop. ]

    72. Exit.

    CODE: clear

    echo -e "\t\tPRINT PATTERN 4\n"

    echo -en "Enter number of lines : "

    read n

    i=0

    while [ $i -lt $n ]; do

    k=0

    while [ $k -le $i ]; do

    echo -en "* "

    k=`expr $k + 1`

    done

    i=`expr $i + 1`

    echo -e

    done

    echo -e "\n\t~~~ END ~~~\t\n"

    OUTPUT: PRINT PATTERN 4

    Enter number of lines : 6 * * * * * * * * * * * * * * * * * * * * * ~~~ END ~~~

    DISCUSSION:

  • 26

    26

    28. Two loops are required, one for rows and another for columns.

    29. Here in this programme we used nested while loops.

  • 27

    27

    ASSIGNMENT : 16

    PROBLEM: Write a shell script to print the following pattern,

    1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6

    ALGORITHM: PATTERN3 (N)

    Here N is number of lines the pattern should contain. This algorithm prints the required pattern.

    73. Set C = 0.

    74. Repeat Step 3 to 11 for I = 0 to N-1 :

    75. Set J = N I.

    76. Repeat Step 5and6whileJ> 1 :

    77. Print blank space.

    78. Decrement J by 1.

    [ End of Step 4 inner loop. ]

    79. Repeat Step 8 and 9 for K = 0 to I :

    80. Print C.

    81. Increment K by 1.

    [ End of Step 7 inner loop. ]

    82. Increment C and I by 1.

    83. Print New Line Character.

    [ End of outer loop. ]

    84. Exit.

    CODE: clear

    echo -e "\t\tPRINT PYRAMID PATTERN\n"

    echo -en "Enter number of lines : "

    read n

    c=1

    i=0

    while [ $i -lt $n ]

    do

    j=`expr $n - $i`

    while [ $j -gt 1 ]

    do

    echo -en " "

    j=`expr $j - 1`

    done

    k=0

    while [ $k -le $i ]

    do

    echo -en $c" "

    k=`expr $k + 1`

    done

    c=`expr $c + 1`

    echo -e

    i=`expr $i + 1`

  • 28

    28

    done

    echo -e "\n\t~~~ END ~~~\t\n"

    OUTPUT: PRINT PYRAMID PATTERN Enter number of lines : 6

    1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6

    ~~~ END ~~~

    DISCUSSION: 30. Three loops are required, one for rows,one for columns and the rest for keeping track of the

    printed numbers.

    31. Here we have used nested while loops.