Sep 4, 2016

Textual description of firstImageUrl

Bash Shell Script Sample Code - Part 2

1.  Write a bash script to find GCD and LCM of two number.

#!/bin/bash
#find GCD  and LCM of two numbers
printf "Enter first  nuumber: "
read n1
printf "Enter second  nuumber: "
read n2
m=$n1
n=$n2
r=$n2
while [ $r -ne 0  ];do
  r=$(( n1%n2 ))
  if [ $r -eq 0 ];then
        break
  else
     ((n1=$n2))
     ((n2=$r))
  fi
done
printf  "GCD of %d and %d is %d \n" $m $n $n2
printf  "LCM Of %d and %d is %d \n" $m $n $((($m*$n)/$n2))
Sample output:-
[zytham@s158519-vm scripts]$ sh  pgssp1.sh 
Enter first  nuumber: 30
Enter second  nuumber: 12
GCD of 30 and 12 is 6
LCM Of 30 and 12 is 60

2.Write a bash script to find GCD and LCM , provided input is passed via command line while executing this script (Pass command line arguments). 
#!/bin/bash
#find GCD of two numbers - input at runtime 
n1=$1
n2=$2
m=$n1
n=$n2
r=$n2
while [ $r -ne 0  ];do
  r=$(( n1%n2 ))
  if [ $r -eq 0 ];then
        #echo "GCD is $n2"
        break
  else
     ((n1=$n2))
     ((n2=$r))
  fi
done
printf  "GCD of %d and %d is %d \n" $m $n $n2
printf "LCM Of %d and %d is %d \n" $m $n $((($m*$n)/$n2))           

Sample output:-
[zytham@s158519-vm scripts]$ sh pgssp11.sh 30 12
GCD of 30 and 12 is 6
LCM Of 30 and 12 is 60

Explanation:- Here 30 and 12 are input to the bash script. It is accessed by script using $1 and $2. Here n1 is 30 and n2 os 20. 

3. Write a bash script to find factorial of a number. 

#!/bin/bash
#find factorial of a number - input Passed as command line arguments 
n=$1 #Input passed via command line is stored in n 
 fact=1
 for i in `seq 1 $n`;do
   ((fact=fact*i))
 done
printf "factorial of number %d is:  %d\n" $n $fact

Sample output:-
[zytham@s158519-vm scripts]$ sh fact.sh 5
factorial of number 5 is:  120
[zytham@s158519-vm scripts]$ sh fact.sh 13
factorial of number 13 is:  6227020800

4. WABS to find Binomial co-efficient of C(M,N) - value of the coefficient is given by the expression .  
#!/bin/bash
#find binomial coefficient of a two number - input Passed as command line arguments 

#function to find factorial of a number-Pass input argument to function
function factorial
{
#echo $1
n=$1
 fact=1
 for i in `seq 1 $n`;do
   ((fact=fact*i))
 done
 #printf "factorial of number %d is:  %d\n" $n $fact
}

M=$1
N=$2
#call function for finding factorial of M
factorial $1
fact1=$fact
#call function for finding factorial N
factorial $2
fact2=$fact
dif=$(($m-$n))
#call function for finding factorial M-N
factorial $dif
fact3=$fact

bico=$(($fact1/($fact2*fact3)))
printf "Binomial coefficient of C($M $N) is %d \n" $bico

Sample output:-
[zytham@s158519-vm scripts]$ sh bico.sh 9 3
Binomial coefficient of C(9 3) is 60480
[zytham@s158519-vm scripts]$ sh bico.sh 8 4
Binomial coefficient of C(8 4) is 1680
[zytham@s158519-vm scripts]$ sh bico.sh 5 2
Binomial coefficient of C(5 2) is 60

Explanation:- Two input is passed from command line and that is propagated to function one by one to find factorial of M , N and M-N. Similar to bash script function also access passed argument as $1.

5. WABS to check numbe is perfect number or not - input passed from command line. ( A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself ) 
#!/bin/bash
#To check perfect number or not input via command line
function perfectNumCheck
{
m=$1
iter=$(($m-1))
sum=0
for i in `seq 1 $iter`;do
  if (( $m%i==0)); then
      sum=$((sum + i))
  fi
done
if [ $sum -eq $m ]; then
echo "perfect"
else
echo "Not perfect"
fi
}

perfectNumCheck $1
Sample output:-
[zytham@s158519-vm scripts]$ sh perfect.sh 4
Not perfect
[zytham@s158519-vm scripts]$ sh perfect.sh 6
perfect
[zytham@s158519-vm scripts]$ sh perfect.sh 28
perfect

6. WABS to generate fibonacci numbers. (Inpiut passed via command line for number of fibonacci to be generated)

#!/bin/bash
#fibbonacci generation - Input via command line regarding how many fibbonacci number
function fibonacci
{
N=$1
a=0
b=1
if [ $N -eq 1 ]; then
 printf 1
else
 printf $((a+b))
 printf "\t"
 while [ $N -ne 1 ];do
  sum=$(($a+$b))
  printf $sum
  printf "\t"
  ((a=$b))
  ((b=$sum))
  ((N=$N-1))
 done
fi
}
fibonacci $1
printf "\n"

Sample output:-
[zytham@s158519-vm scripts]$ sh pgssp5.sh  5
1 1 2 3 5
[zytham@s158519-vm scripts]$ sh pgssp5.sh  8
1 1 2 3 5 8 13 21
[zytham@s158519-vm scripts]$ sh pgssp5.sh  9
1 1 2 3 5 8 13 21 34

7. WABS to check given number is Armstrong number or not.
#!/bin/bash
#check armstrong number 
echo -n "Enter number : "
read n
input=$n
sum=0
while [ $n -gt 0 ]; do
 (( rem= n%10 ))
 ((sum=$sum+(rem**3) ))
 # echo $sum 
 ((n=n/10))
done
if [ $input -eq $sum ];then
echo "$input is an armstrong number "
else
echo "$input is not armstrong number "

Sample output:-
[zytham@s158519-vm scripts]$ sh  ams.sh
Enter number : 153
153 is an armstrong number
[zytham@s158519-vm scripts]$ sh  ams.sh
Enter number : 145
145 is not armstrong number

Location: Hyderabad, Telangana, India