Friday, March 9, 2018

Useful scritps

How to find the disk usage of current folder and system up time ?

1. uptime command is used for finding the system uptime
2. du command is used to find the disk usage level.
du accepts many parameters. Please do "man du" to find different paraments accepts by du command. In the below example, we display disk usage of current folder in Mega Bytes. 

[hostname:root:/tmp/spohsv] cat w.sh
#!/bin/sh
time=`uptime`
echo "The system is Up since \"$time\""
diskusage=`du -sm`
echo "Disk usage of current directory in Mega Bytes is"
echo $diskusage
echo `date`


How to append the log file name with date and timestamp ?

Please keep the below statement in the script. This will create the log file scanLog***. Example file name : "scanLog_2017-11-02-14-41-32.log" 

logFilename=/tmp/scanLog_$(date "+%Y-%m-%d-%H-%M-%S").log


How to find the total lines in a file and assign it to variable ? 
echo totalLines=`wc -l sample.txt | awk -F" " '{print $1}'`
echo $totalLines 

How to find total lines in all files in a directory ?

Code below : 
#!/bin/sh
fileList=fileList.txt
ls s*.txt > $fileList # Specify the directory in ls command. 
cflag=1
while read file
do
count=`wc -l $file | awk -F" " '{print $1}'`
echo "Total lines in $file is $count "
        if [ $cflag = 1 ]; then
                cflag=0
                totalLines=`expr $count + 0`
                continue
        fi
        if [ $cflag = 0 ]; then
                totalLines=`expr $count + $totalLines`
                cflag=0
        fi
done < $fileList
rm $fileList
echo "Total Lines in all files is $totalLines"


How to find the total lines in all files when the last line defines the total lines to be considered.
For example : 
sample1.txt : Below file has total of 5 lines. But, file defines to use total as 3. 
Hi, 
Hello, we are defining an example

Above line is blank line. 
total=3 

sample2.txt : Below file has 8 lines. But, the file defines to use total of 5 lines. 
Hello,
This text file is used for defining how to count total
when total lines are defined in end of file

Above line is blank line

Above line is blank line
total=5

In the above text files, we have blank lines. Total lines which are not blank is defined in last line of the file and it is prefixed by the string "total". In this case, below script will help is counting total lines in all files

#!/bin/sh
fileList=fileList.txt
ls s*.txt > $fileList
cflag=1
while read file
do
count=`tail -1 $file | awk -F"=" '{print $2}'`
echo "Total lines in $file is $count "
        if [ $cflag = 1 ]; then
                cflag=0
                totalLines=`expr $count + 0`
                continue
        fi
        if [ $cflag = 0 ]; then
                totalLines=`expr $count + $totalLines`
                cflag=0
        fi
done < $fileList
rm $fileList
echo "Total Lines in all files is $totalLines"

How to call a sample java program from shell script ?

#!/bin/sh
### This shell script accepts one argument - JobNumber as argument
if [ "$#" -ne 1 ]; then
  echo "Usage: $0 <jobnumber>" >&2
  echo "Example : $0 jd01"
  exit 1
fi
echo "Start of the Program : `date`"
arg1=$1
/usr/java6/bin/javac  -d . sample.java
/usr/java6/jre/bin/java sample $arg1
echo " End of shell program : `date"




Thursday, August 31, 2017

The other side of Life

There is a saying in Kannada : “doorada betta kannige nunnage” (from a distance, a hill always is soothing to the eye, looks flawless, sans any pebbles or crevices).  There is a similar english proverb : "The grass is always greener on the other side of the fence".

Personal Battles :
Everyone including me have our own personal battles. We fight within ourselves against the world, and against one's own life. The world and our life revolves around four entities - "family, friends, work and health". When a person cannot fight the personal battles within themselves, then it might end up rejecting ourselves, low self esteem, depression. These can come to anyone and everyone. Everyone suffer from these phases in their life which be because of work life, family reasons, professional relations,health conditions etc., And so, again the reasons for them are the same four entities which our life revolves around. How we handle depression is what matters. One needs to look in other perspective to come out of depression.

Our mind wants to quantify the things around those four entities. It wants to rank the external luxury, rank the people against their salary/job, rank against x's personal achievements etc., When  the rankings in mind are higher against opposite person, then the person will not be handle it. This is how everyone's mind works.

It is not mandatory that these quantitative feelings will arise to depression, rejecting ourselves. When we cannot fight against these feelings and ignore or look them in other perspective, then it can give rise to rejection in one's self.

The other perspective :
The negative feelings have to be seen in new angle. This new angle of life becomes our rebirth to come out of depression.

One needs to sit and start working on those negative feelings to turn them into a positive one.  For example, by comparing yourself with the less fortunate and realize that you are blessed ; realize and appreciate how privileged you are, understand how lucky you are to have such wonderful person in one's life etc., If only each person could look inwards and understand his/her purpose in this world, then this would be a different place.

Lets admit that our hunt for a perfect life will remain a dream.  We should celebrate our imperfections and our differences. When we are happy, we celebrate our happiness with our loved ones. But, when we are sad, we will not keep our foot one step forward to share what is happening within our-self. If we have a person who can hear us when we are sad, gives the shoulders to cry on, then (s)he is the one with whom we are close to and we admire more. It does not matter to him when we dont call him to celebrate our happiness. But, he is always there as a standing wall for our emotions of life. Everyone should have one such person whom they can go and talk for all emotions of our life; like happiness, sorrow, anger, madness etc.,. That one person is the one who can hear us for infinity, bear us for all the madness we have, keep calm when we show our anger. This one person can be parents, a friend, a sibling, spouse, or even your own younger child.

Building our-self:
Every block of stone has a statue inside it. A stone can be left as a stone for hundreds of years at one place. If its given to a sculptor, he will create a beautiful statue from the stone. Sculptor discovered the statue in a stone. It is up-to us on how we discover our-self and build our-self. What we are today are because of what we aimed to become. It does not make sense if we keep looking for a hill-top rather than trying to climb. If we just stare at hill and think of reaching atop of hill, we will never be able to reach. We will reach the hill top only if we climb. To reach the hill-top, it can take one day, one year or 5 years. It all depends on our potential.  With sound health and untapped potential by our sides, it makes no sense for us to just look at the bungalow sitting in our hut and sipping tea. We will need to try in order to reach our destiny.

We build our home, we keep it clean and tidy. Why don't we clean our mind and make it look clean and tidy? Its very illogical to say that please clean your mind and keep it tidy. But, we have to do this. How can we cleanse our mind? By something called "thought process" or by Meditation. This thought process can be achieved by closed eyes, or walking on green lawn, or watching for a water fall for n minutes etc., It can be anything which makes our mind blank and n number of thoughts flowing across to think about our emotions in positive perspective. If we are trying for meditation, it is sitting at an idle place with closed eyes. There is a kind of happiness when we do nothing. In the initial stages of meditation, Millions and trillions of thoughts will flow across the mind. We will start to think about those thoughts and emotions. Sometimes, we can even get better resolutions for them or we can get solutions on how to handle those things.