Friday, October 14, 2016

How to check whether a number is prime in UNIX shell script

Dear readers, thanks for your response. The pre-school blog about fabonacci series in unix shell script was a hit! 

Today we will see another script that is asked on a regular basis in various interviews and examinations. We will write a script in UNIX shell scripting to check whether a supplied number is prime or not. But, before we start lets have a look on the basics of prime number.

A number is prime, if it is divisible only by itself and 1.

Thus, to check the primality of a number n, we start dividing the with 2 and continue till n-1
Also, 2 is the only even number which is the smallest prime number. So let get down to wrote the code.


#!/bin/bash
######################################################
# SCRIPT : prime_number.bash
# USAGE  : ./prime_number.bash [Number]
# PURPOSE: Checks whether the given number is prime or not
# AUTHOR : Shashank Rawlani
######################################################


# Test the number of arguements recieved by the script

## Variable declarations
RC=1
ctr=0
num=$1

if [ $# -eq 1 ]; then

        ## Check whether the supplied number is an integer
        expr $num + 1  &> /dev/null

        RC=$?

        while [ $RC -ne 0 ]
        do

                echo "Sorry, you have supplied a non numerical value"
                echo ""
                echo -n "Enter a number: "
                read num

                ctr=`expr $ctr + 1`

               expr $num + 1  &> /dev/null
                RC=$?

                if [ $RC -ne 0 && $ctr -eq 5 ]; then
                        echo "Maximum tries reached"
                        exit 1;
                fi

        done


fi

## Actual primality test starts here 
i=2

while [ $i -lt $num ]
do
  if [ `expr $num % $i` -eq 0 ]
  then
      echo "$num is not a prime number"
      echo "Since it is divisible by $i"
      exit
  fi
  i=`expr $i + 1`
done


echo "$num is a prime number "



You can also find the script on www.rawlani.com.
Do let me know your comments below or write me on shashank@rawlani.com.

Wednesday, October 12, 2016

How to generate fabonacci series in UNIX shell scripting

It has been a long time, since I last posted a blog. It feels to be in pre-school again, since I made up my mind to write something again.
Well, unlike pre-school, we all have had the privilege to study and write the code to print the famous - "Fabonacci Series" in different scripting languages and technologies.

By definition in mathematics, the Fibonacci Numbers are the numbers in the below sequence: 0,1,1,2,3,5,8,13,21,34,55,89,144, ...... and so on...

The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. Some sources omit the initial 0, instead beginning the sequence with two 1s.

Thus, in mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation -
Fn = Fn-1 + Fn-2
With seed values F0 = 0 and F1 = 1.

Let us see today, how to to write and interactive program in UNIX shell script -



#!/bin/bash#!/bin/bash
#####################################################
# SCRIPT:  fabonacci.bash
# USAGE:   fabonacci.bash [Number]
# PURPOSE: Generate Fibonacci sequence till the number of iterations entered by user.
# AUTHOR:  Shashank Rawlani
#####################################################

# Test the number of arguements recieved by the script

if [ $# -eq 1 ]
then
    Num=$1
else
    echo -n "Enter the number of iterations needed :"
    read Num
fi

a=0
b=1

echo "The Fibonacci sequence for the number $Num is : "

for (( i=0;i<=Num;i++ ))
do
     echo -n "$a "
     c=$((a+b))
     a=$b
     b=$c
done


echo ""




You can also find the script on www.rawlani.com.
I will be publishing a few such UNIX scripts and a few generic scripts that I use in my daily life to help me quickly do certain tasks.

Do let me know your comments below or write me on shashank@rawlani.com.

Thursday, October 9, 2014

The neverending distance and the tenacity of love!

How long can you wait for someone? A day, a month, probably a couple of years! But, how can you decide or conclude? When to end the prolonged wait? How far can you stretch to keep the relationship on? Why does the distance between you keeps on increasing? On every step closer, you are pushed miles apart!

The neverending distance and the tenacity of love!
The two things that a long distance relationship has to live with, until someone takes a leap and does something to cut the distance short. The one, hard step, that sets things apart and makes way for the one, for whom, you have been waiting. The one that makes you happy, truly happy.

Whatever you do to make things right, sometimes they don't matter. Your efforts are not counted as EFFORTS, while rest of the things, make no sense and sometimes, things go South!
What to do in those times? How you handle those situations? How you bring things, back on track? And the most important thing is, how to channel all your emotions, LOVE, ANGER, AGONY, DESPAIR, etc., to the right direction and in the same time express yourself.


Love is all about the journey and your actions, define your relation and vice versa. 

It has been several long years, of togetherness, without actually being together. A lot of ups and downs have kept our lives interesting, happening, a bit slow and sometimes a bit low. But through these years, one thing that has been there & has constantly kept this relation alive was "THE JOURNEY". Love is all about the journey, the little rides of emotions and a bunch of rants and ramblings...

Please share your comments and feedbacks.
If you too have a love story, share a few lines on our Facebook page with #MyLoveStoryMyWords, a teaser with a few lines in your own words, a trailer(a BiiiiiG teaser), or a sneak peak with a small video in your own way or a quotation(with a courtesy tag, if copied). Check out my teaser.

Do remember, love is not restricted to your ought to be! It can be about the one, about that one, your BFF, your family members, it can even be hypothetical, any one of them.

Keep reading, keep sharing and keep the life-lanterns fueled up!

Saturday, September 20, 2014

My Story.. A glimpse from the past..

The last week was quite a roller coaster ride for me. A lot of rises and falls, really rocked their way into my world and into my work. They sometimes, gave me a sense of numbness and on the other hand, peace of mind and carelessness. These events gave me a flashback of tons of memories from past few years and one thing lead to another, and I ended up on my collection of poems. Since I am not a frequent writer, I would share, some of my work from different genre, on this blog.

Starting with my very first step, the baby-step, into the world of literature and writings, my first ever poem. My आगाज़ -


ये ज़िन्दगी भी कितनी अजीब है यहाँ

ये ज़िन्दगी भी कितनी अजीब है यहाँ, 
मौसम बदलते है, semesters  की तरह । 

teachers का मूड भी उन, seasonal winds की तरह है, 
जो कभी ठंडी, कभी गरम, क्या पता, कब किस तरह।  

वो किताबें हमारी, जिनसे लगती हमें बिमारी,
लेते थे, बस कुछ एक dose, भूले भटके, याद आया तो, कही किसी रोज़ । 

वो mess, जहा मिलता था , कुछ कुछ खाने जैसा,
क्या अरहर, क्या मूंग, सब कुछ बस, एक जैसा । 

रोटियों की शक्ल देख कर ही मन्न भर जाता था 
हमारे हॉस्टल का पालतू कुत्ता भी, उसे अपना पेट भरने क लिए खाता था । 


Now during  the  college timings कुछ ऐसा होता था -


वो सुबह की गर्मी, वो पढ़ाई में नरमी,
वो class की खिड़कियाँ, और बाहर टहलती college की लडकियां । 

फिर teachers की cross - questioning
then bunk & canteen । 

फिर आते थे M. S. T. (ease ), बस ख़त्म हमारी सारी ease,
then पढ़ाई की tensions , जो करती हमें decrease । 

फिर results का suspense , क्या बताऊँ यारों, I was so tense 
But I was sure about my clearance । 

पर घबराता था, कभी - कभी,
हो जाए न कोई incidence । 

But I got through , 
with percentage 82 [ Just to rhyme better ] । 

फिर हुई placement,
that was something I meant । 

फिर काम का भोझ, और अपने लिए वक़्त की खोज!

ये ज़िन्दगी  अजीब है यहाँ,
क्या पता किस मोड़ पे, राही मिले कब किस तरह। 

- शशांक रावलानी 


Please share your comments and feedback below and other social platforms: Google +, Facebook and Twitter.

Thursday, September 11, 2014

The long distance numbness...

"Love", the four letter word,  the one word, that makes you feel, your heart beats become more audible, the world around us, more HD, people around you,  less audible and your cheeks held up high until it pains. It shifts everything apart and acquires the most of you and your life. It feels great to have someone around. Doesn't it?
But, what happens, when the one you love, is away from you? When they can't be with you for many reasons? If they can't meet you for months and for years? How would it make you feel? What would you do?
The prolonged wait period, for months and sometimes years, creates a large gap, a numbness, the feeling, where you actually don't feel. You hear every single noise around you, you hardly smile, there is no HD in the world around  anymore and certainly you hear your heart pounding, a little less though.


To be in a long distance is not a big deal! Thousands of couples stay in long distance and sustain, trying to keep their love fresh and as charming as ever and cherish every moment they spent together, hoping to get married one day. But is that all? Is that all about love? Getting married someday? Was that the aim? 
There are many such questions that pop up every now and then. I think you too are thinking about the same,  but all we can do, is to wait & think!

For different people, LOVE has different definitions and a different perception. For me, LOVE is about the JOURNEY, that you have together, the feelings that you share, the memories, that you create, that you might cherish sometime in your life, remembering old stories. Getting married is a MILESTONE! It's still not the destination of LOVE. We still have a long way to go after that. 

Being in a long distance relation, sometimes leaves blank spaces in our lives. Spaces like the best friend(s) you have, the most likeable person you know, the best company you can think of, the best conversation you can have with and even it can sometimes, push you to rethink on the best person who should have been by your side instead!

There are a lot more words that can be wrenched up, but that won't fill up the spaces in our lives. It's the long distance numbness that needs to be vanquished, else our lives would be just a pursuit of happiness! 

Do share your thoughts in the comment section below. 

Sunday, September 7, 2014

Lost in transition...

"Lost in transition...", with three periods at the end, depicting the past, present & future.

These are moments of life, my life while writing this blog, your life while skimming it and of those who might not even have a look at it thinking would it be worth reading!
For some, these are mere words, as they literally don't connect to one's perspective and to others, it is a whole new experience.

"It's like, enjoying all the ingredients of your dessert, together".

My statement might seem to be incorrect or intriguing. It depends on your perspective.
Today we live a life, we try to enjoy every bit of it, but what we do really?
We run after materialistic sources of being happy. We watch a movie and rate it as per the cost we spent, we grab a bite in the finest restaurants, but rate it as per the ambiance first, the service second, the cost third and lastly comes the taste of the food served. But in all cases, do we ever count the quality of time we spent, the feelings or even the words we exchanged with others?

Now read the first line again.
Don't be - "Lost in transition..."

I have stayed in a lot of different places, met a lot of people, made some great friends and acquaintances. I have kept myself busy, thinking a lot of stuff, while in transition. As per the astrological gurus, being a "GEMINI", I am best at THINKING, being VERSATILE and have LACK of CONSISTENCY and a lot more traits. So with this blog, I will try to be fair with myself and just be me and share whatever I feel like and no specific audience!

I have not written any blog, for more than an year now and these, are kind-of rusty thoughts.
And all I have to say is - "Keep reading, keep sharing, keep feeling your heartbeat and stay alive".