Shell Script Assignment Discussions
Shell Script Assignment Discussions
1.Write a shell script to find the second largest number of n numbers (without using any sorting
algorithm)
So “ head –2” provides first and second largest number and “tail -1” shows second largest number.
If the file “fnm” consists of : 20, 200, 2000, 30, 40.After numeric sort on reverse order we have, 2000,
200, 40, 30, 20 where 200 is the second largest number.
2. Write a shell script to find the Peterson numbers within a given range.
Discussion: A number is called Peterson number when the factortial of each digit of the number is added
and we get the number back. Here each number of the range is tested to have the above mentioned
condition.Within range of 50,000 two peterson numbers are 145, 40585. For example 145.
Factorial of (1) = 1
Factorial of (4) = 4 X 3 X 2 X 1 = 24
Factorial of (5) = 5 X 4 X 3 X 2 X 1= 120
∑ Factorial of (1)+ Factorial of (4)+ Factorial of (5)=145
3. Write a shell script to determine prime numbers between two given positive numbers using factor
command of UNIX/LINUX. Numbers will be provided as command line arguments.
$factor 12(enter)
12: 2 2 3 it gives the prime factors of a number when the number is prime, it only shows one factor i.e.
number itself like:
$factor 11(enter)
11: 11
Here first cut command cuts the on the basis of “:” and gives only list of factors. Second cut command
cuts first factor from list of factors like : for 12
4. Write a shell script to determine whether an entered number is divisible by 11 without dividing the
number by 11.
Discussion: Let us suppose we take number 2585, each digit of this number is stored in the array say “X”
in the reverse order i.e .5852 where variable j starts with position 0, incremented by 2 and stores the
sum of x[0] and x[2] in sum1(say). Similarly variable k starts from position 1 and incremented by 2 and
stores sum of x[1],x[3] in sum2(say).
When sum1=sum2, number is divisible by 11 otherwise the number is not divisible by 11. If sum of even
position digits and sum of odd position digits of the numbers are same, then the number is divisible by
11.
Here we see x[0]=5,x[2]=5 ,so sum1=10. Again x[1]=8,x[3]=2, so sum2=10so 2585 is divisible by 11
5. Write a shell script to find the roots of a quadratic equation. Also test for imaginary roots.
Discussion: We take the quadratic equation of the form: ax2+bx+c=0. We then calculate d= √(b2-
4*a*c)using d=$(echo "scale=0;sqrt($l)"|bc -l). Here sqrt(),a mathematical function is invoked using
“bc”(basic calculator) command.
If d>0 then we print the two roots of the equation.
If d=0 then we print the equal roots of the equation.
If d<0 then we print that the roots are imaginary.
Discussion: “sed” command `s/unix/dos/g` substitutes “unix” with “Dos” globally i.e. any where in
file “Unix” is there , it is replaced by “Dos”. After that the content of original and changed file are shown.
Here full form of “sed” is Stream Editor.
Discussion: Let us first consider a=95 and b=30 So addition is 7D and subtraction 41 Here a & b are
translated like the if a=1af, b=23a, since “bc” (basic calculator) works with only (0-9) and (A-F) So
entered numbers must be converted to their capital representation i.e. after “tr”, a=1AF and b=23A.
In file tmp1 , obase=16, two entered numbers are printed,” bc” extracts data from tmp1 and stores the
result in x whose value is printed lastly. Subtraction works in similar way. tmp1 and tmp2 are removed
each time for further execution of program.
8. Write a shell script to find the factorial of number using recursive function call.
Discussion: We know when a function calls itself; then it is known as recursive function call. First a
number is taken and checked whether it is 0 or negative, if it is 0, then factorial is 1. If it is negative
number then factorial of that number is not calculated. If the number is a positive number then the
recursive function call fact() is called and it returns the factorial of the entered number.
9. Write a shell script to simulate “wc” command with w,c,l options, options are provided as command line
arguments, $1 conatins option like w,c or l and $2 contians name of file on which simulation of “wc”
command is going to be applied.
Discussion: This program uses case-esac construct. Each case option is ended by “;;” and default option is
given by “*”.
When “$1” contains “-l” and the file name supplied by “$2” the concerned file is read until end of file and
number of lines are printed.
When “$1” contains “-w” and the file name supplied by “$2” the concerned file is read until end of file and
each line’s word are calculated and lastly total number of words are printed. Here it is assumed that words
are delimited by spaces.
When “$1” contains “-c” and the file name supplied by “$2” the concerned file is read until end of file and
number of lines are printed .Each line’s number of characters are calculated and giving total number of
characters,
10.Write a shell script to find the triangle type where coordinates of three points are given as input.
Discussion:First three points are taken. Side of triangle is calculated by “len1=$(echo "scale=0;sqrt((($x1-
$x2)*($x1-$x2))+(($y1-$y2)*($y1-$y2)))"|bc -l)”.We know the general formula is √[(x1-x2)2+(y1-y2)2] to
calculate length of line segment. Here sqrt() function of bc(basic calculator) is used. Then it is checked
whether the line segments are valid or not.
If line segments are valid; then it is checked if they can form a triangle or not by the formula: length of
side1(len1)+length of side2(len2)>length of side3(len3)