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

36 Comments

  1. DD458
    ----
    ----
    ----
    ----
    ----
    ----
    ----
    ----
    matadorbet

    ReplyDelete
  2. 8069BB99E8KennedyD6E6EFBBFDNov 27, 2024, 7:18:00 AM

    10DA6DA952
    bayan beğeni

    ReplyDelete
  3. 7036EE3682
    Sosyal medya hesaplarınızın etkileşimini artırmak için çeşitli yöntemler kullanabilirsiniz. Örneğin, bot beğeni satın alarak sayfalarınızın görünürlüğünü artırabilirsiniz. Bu sayede organik takipçilerinizin dikkatini çekmek daha kolay hale gelir ve hesabınızın güvenilirliği artar. Ancak, bu tür hizmetleri kullanırken dikkatli olmak ve platformun kurallarına uygun hareket etmek önemlidir.
    İnternet üzerinde sosyal medya hesaplarınızı daha görünür hale getirmek ve etkileşimi artırmak için çeşitli yöntemler bulunmaktadır. Bunlardan biri de, güvenilir platformlardan destek alarak kick takipçi satın al seçeneğidir. Böylece, profilinizin popülerliğini hızlıca artırabilir ve daha fazla kişiye ulaşabilirsiniz. Ayrıntılı bilgi için kick takipçi satın al sayfasını ziyaret edebilirsiniz.
    Sosyal medya stratejilerinizi güçlendirmek ve daha geniş kitlelere ulaşmak için çeşitli yöntemler deneyebilirsiniz. Özellikle Snapchat platformunda dikkat çekmek istiyorsanız, snapchat izlenme satın al seçeneğiyle daha fazla görünürlük kazanabilirsiniz. Bu sayede içeriklerinizin etkileşimi artar ve takipçi sayınız yükselir. Doğru adımlar atarak sosyal medya varlığınızı pekiştirebilirsiniz.
    Sosyal medya hesaplarınızın etkinliğini artırmak için çeşitli yöntemler denemek isteyebilirsiniz. Özellikle Twitter üzerinde daha fazla görünürlük elde etmek için, bazı kullanıcılar twitter izlenme satın al seçeneğini tercih ediyor. Bu sayede paylaşımlarınızın daha geniş kitlelere ulaşmasını sağlayabilir ve takipçi sayınızı hızla artırabilirsiniz. Ancak, bu tür hizmetleri kullanmadan önce dikkatli olmanız ve platformun kurallarına uygun hareket etmeniz önemlidir.

    ReplyDelete
Previous Post Next Post