Search This Blog

Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Monday, May 23, 2011

Calling one linux scipt from another.

This might not be the general practice being followed by script writer .
Most of the time it is recommended to write a function inside a script and call it whenever necessary.


Create script.sh and test.sh in /usr/local/sbin

Parent Script.
# cat script.sh
# script.sh file
[[ -f /usr/local/sbin/test.sh ]] && . /usr/local/sbin/test.sh
Var=123
echo "Calling Function"
echo "Value of Var is $Var"
my_test
echo "Returning the control"

Child Script.
# cat test.sh
my_test()
{
echo "Just testing here:)"
echo "Value of Var is $Var"
}

Now execute the parent script.
./script.sh

# ./script.sh
Calling Function
Value of Var is 123
Just testing here:)
Value of Var is 123
Returning the control

Monday, May 9, 2011

How to find VNC port for Guest machines running on Oracle VM.

Use below script for finding VNC port of all Guest Machines running on the Oracle VM server.

xm list > temp
y=`xm list | wc -l`

z=`expr $y - 2`

while [ $y -gt 1 ]
do
x=`awk -F" " '{print $1}' temp | sed -n "${y}p"`
echo -e '\E[32m'
y=`expr $y - 1`
xm list $x -l |grep 0.0:|awk -v Host=$x -F[:\)] '{print "Vnc Port for " Host " :" $2 }'
echo -e '\E[37m'
done

Monday, August 30, 2010

Date ( Linux system)

Change System Date:

There are many ways to change the system date in linux but i think following method is bit easier than others.

# date -s "31 JAN 2009 22:19:53"
-s option used to set the date.


In every case above example is sufficient .However you can use below commands for your knowledge.

# date +%T -s "23:20:57"

# date +%T%p -s "11:20:57PM"

Display Date in different format:

I would recommend to use following format while using date variable in your script.

#date '+%F-%I-%M-%S-%p'
2011-04-01-10-25-33-AM

-F : full date same as %Y-%m-%d
-I : hour (01..12)
-M : minute (00..59)
-S : second (00..60)
-p : localeâs equivalent of either AM or PM; blank if not known

Coming soon...

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

Cut command

cut command

Description:
Usage :
1)Cut command is mostly use to display columns from a file.
for example if a file contains a row column structure you can display
a column of file specifying column number (Starting with one for First column) using cut command.
2)It can also be used to display a given number of characters from each line of file.

Lets create a sample file (cut_sample.txt) for use.

Student Subject MarksObtained
Jack Spanish 80
Jill English 20
Krishna Gujrathi 30

Note:
our sample file contains 3 columns namely (Student,Subject,MarksObtained)
Each field is seperated by " "(a space).

1) cut command to display columns from a file.
-d is used to specify a delimeter(here the delimeter is a character that seperates
one field from other).A delimeter in above case is a space.
-f is used to specify the field name to be displayed.

Example 1:

# cut -d" " -f 1 cut_sample.txt
Student
Jack
Jill
Krishna

Example 2:

# cut -d" " -f 1,2 cut_sample.txt
Student Subject
Jack Spanish
Jill English
Krishna Gujrathi


2) cut command to display a given number of characters from each line of file.


# cut -c 1-8 cut_sample.txt
Student
Jack Spa
Jill Eng
Krishna

-c is used to specify characters in range 1-8.
In above example 8 character from each lines are displayed from file starting from 1st character .

Use following cut command to display the name of users in the system.
cut -d: -f1 /etc/passwd

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

Friday, August 27, 2010

How to display specific line of file specifying line number.

How to display specific line of file specifying line number.

#awk 'NR==2{print $0}' filename

This will display 2nd line of file named filename.

Another way of doing the same thing (compact one)

#awk 'NR==2' filename

1 More (complex one)

#head -2 filename | tail -1

More will be coming soon.

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

Sunday, August 22, 2010

Robust Scripting.

In This blog entry i will be focusing more on How to build a robust shell script.

According to me writing a script that actually works and writing a robust script that actually work has two different meaning.

So lets try to start writing a robust script.

A good article over writing a robust script can be found in following url.

http://www.davidpashley.com/articles/writing-robust-shell-scripts.html

You can also find following post useful while writing a robust script.

Safest way of comparing values using Variable.

Tuesday, August 17, 2010

Grep Command.

To understand grep We will create a small file as Gandhi.txt.
#touch Gandhi.txt
Above statement will create a file called Gandhi.txt

Post the following lines in a file(Gandhi.txt).
#vi BhagatSingh.txt
press key i for insert copy the text below .and paste it into the file using rightclick
Save the file by pressing esc and then typing :wq

************************************************************
Bhagat Singh, a Sandhu Jat, was born on 28 September 1907 to Kishan Singh and Vidyavati at Chak No. 105, GB, Banga village, Jaranwala Tehsil in the Lyallpur district of the Punjab Province of British India.
In 1923, Bhagat Singh joined the National College in Lahore
Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.
************************************************************

Let us use some grep commands to get answer to following questions .
When was Bhagat Singh born?
What year was Bhagat Singh born?

You know the only file in your system that has any information about Bhagat Singh is BhagatSingh.txt So you type following command to get the birth date of Bhagat Singh.

# grep born BhagatSingh.txt
Bhagat Singh, a Sandhu Jat, was born on 28 September 1907 to Kishan Singh and Vidyavati at Chak No. 105, GB, Banga village, Jaranwala Tehsil in the Lyallpur district of the Punjab Province of British India.

Now lets print all lines from files with word Bhagat in it.
# grep bhagat BhagatSingh.txt
Surprised of getting nothing as output.
Well this is because grep without -i option ,considered case sensitive.
In our example we have used word gandhi as search parameter.
Now try the same with Gandhi word.(Notice capital letter G).

# grep Bhagat BhagatSingh.txt
Bhagat Singh, a Sandhu Jat, was born on 28 September 1907 to Kishan Singh and Vidyavati at Chak No. 105, GB, Banga village, Jaranwala Tehsil in the Lyallpur district of the Punjab Province of British India.
In 1923, Bhagat Singh joined the National College in Lahore
Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.

This time Mission Successful. :)

I know what you might be thinking at this stage.
Do i always need to remember the exact Case while searching through files.
Well the answer is No*.
(*:Provided you use option i)

Lets try the same example with word gandhi (Notice all small letters).
# grep -i bhagat BhagatSingh.txt
Bhagat Singh, a Sandhu Jat, was born on 28 September 1907 to Kishan Singh and Vidyavati at Chak No. 105, GB, Banga village, Jaranwala Tehsil in the Lyallpur district of the Punjab Province of British India.
In 1923, Bhagat Singh joined the National College in Lahore
Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.

-i option is used to ignore cases.


Lets assume you know the birth date of Bhagat Singh and more interested in knowing other facts about him.
Following command will help you get other information .

below command will display all lines except the lines containing born word in it.

# grep -v born BhagatSingh.txt
In 1923, Bhagat Singh joined the National College in Lahore
Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.

-v is a option used to accomplish such tasks.

Other usage.
-n to display line number.
# grep -vn born BhagatSingh.txt
2:In 1923, Bhagat Singh joined the National College in Lahore
3:Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
4:The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
5:    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.

-e
Incase you dont want to read anything about his birth and hanging you may use below command.
# grep -vn -e born -e hanging BhagatSingh.txt
2:In 1923, Bhagat Singh joined the National College in Lahore
3:Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
4:The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:


-c  To count the number of matches
# grep -vi -c born BhagatSingh.txt
4


There are many other usage of grep which can be included but the main purpose of this article was to make you aware of its common and widely used syntax.
a good article over basic grep command can be found here.
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
Expressions that can be used with greps are explained here.
http://www.thegeekstuff.com/2011/01/regular-expressions-in-grep-command/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+TheGeekStuff+%28The+Geek+Stuff%29

Monday, August 16, 2010

CPU Monitoring(Doctortux)

Following command are usefull for monitoring cpu performance so they are included in the script.

#uptime

The above command helps us find the important aspect of the machine which is How long the machine is been running? in other world when did it start running?
The more the uptime the slower the performance.

#w

The above command helps to determine how many users are logged into the system.
The more the sessions the slower the performance.

#ps auxf | sort -nr -k 3 | head -10

The above command helps to determine the top 10 processes affecting cpu utilization.


#cat /proc/cpuinfo

The above command gives you information about cpu of machine.

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

Bash and Database connectivity.

Install Database as described in below link:

http://learnlinuxwithrohan.blogspot.com/2010/08/oracle-xe-installation.html

Prerequisite:
Set following variables.

export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=XE


Description:
Script connect to the database and execute the command .


#!/bin/bash

# Validate the value of ORACLE_HOME #
if [ -z $ORACLE_HOME ]
then
echo "Set the ORACLE_HOME variable"
exit 1
fi

# If ORACLE_HOME doesn't exist #
if [ ! -d $ORACLE_HOME ]
then
echo "The ORACLE_HOME $ORACLE_HOME does not exist"
exit 1
fi

# Validate the value of ORACLE_SID #
if [ -z $ORACLE_SID ]
then
echo "Set the ORACLE_SID variable"
exit 1
fi

#Get SID for the database.

SID=`ps -ef|grep smon|grep -v grep|awk '{print $8}'`

if [ 'xe_smon_'$ORACLE_SID != $SID ] ; then
echo "ORACLE_SID IS NOT SET"
exit 1
fi


# Enter the username and password to login to oracle #
echo "Enter the username"
read username

echo "Enter password"
stty -echo
read password
stty echo
# Get the query , no validation applied for query #
echo "Enter the query"
read query

# Login and execute the query.
echo "set feedback off verify off heading off pagesize 0
$query
exit" | $ORACLE_HOME/bin/sqlplus -s $username/$password | while read output ;
do
echo $output
done

Output:



More will be coming soon.

Monday, August 2, 2010

Process monitoring.

use following command to check whether the process exists or not.
#
kill -0


Example:

# kill -0 5071
[root@Localhost ~]# echo $?
0
[root@
Localhost ~]# kill -0 5169
-bash: kill: (5169) - No such process
[root@
Localhost ~]# echo $?
1

Arguments in scripts.

How do you refer to the arguments passed to a shell script?
Answer - $1, $2 and so on. $0 is your script name.


How do you find out the number of arguments passed to the shell script?
Answer - $#


Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

Wednesday, July 28, 2010

Safest way of comparing values using Variable.

Method 1:
if [ "x$HAVE_CIFS" = "xyes" ]; then
...
fi

Method 2:
if [ "$HAVE_CIFS" = "yes" ]; then
...
fi

in above 2 methods method one is safest as it deals with versions of "test" that don't deal with empty strings correctly.

Now onwards i will try to use the safest way whenever is possible.

reference
http://www.linuxquestions.org/questions/showthread.php?p=4048568&posted=1#post4048568

More of Robust scripting article can be found in following blog entry.
http://learnlinuxwithrohan.blogspot.com/2010/08/robust-scripting.html

Thursday, July 22, 2010

Precaution to take before deleting file through script.

Here i m highlighting the best practice to delete a file when there is a variable involved as a file name.

#!/bin/bash
echo "Enter file or folder to delete from tmp"
read input
if [ ! -f "/tmp/$input" ]
then
echo "File Doesnt Exist"
else
rm -f "/tmp/$input"
fi


rm -f, --force
ignore nonexistent files, never prompt

Tuesday, July 20, 2010

While and For loop for reading from file.

Filename.txt
My First Line
My Second Line

::::While Loop:::
Program(whileloop.sh):

while read line
do
echo "$line"
done < Filename.txt

output:
My First Line
My Second Line

:::For Loop:::
Program(forloop.sh):
for line in $(cat Filename.txt)
do
echo $line
done

output:
My
First
Line
My
Second
Line

You might wonder why does the output generated by for loop is different.

Well in above example for has treated space as field separator .
But to achieve the above output you need to add next line as a separator.

Therefore the same code can be written as.
Program:
IFS=for i in $(cat Filename.txt)
do
echo $i;
done

output:
My First Line
My Second Line\n' # Specify next line as Field Separator.


for i in $(cat Filename.txt)
do
echo $i;
done

output:
My First Line
My Second Line

Thursday, July 15, 2010

parsing command line option .

I have decided to run the script in two mode
1)Interactive.
2)Automatic.

Automatic mode ( -a ) (default):This mode shoudnt ask any input from user and should run automatically.

Interactive mode (-i) : This mode should ask user for input .


Initially i m trying to concentrate on automatic mode which is by default.



I have used getopts method to deal with command line arguments. However this might restricted to bash script because of getopts method.
Method used in program.
if [ $# -eq 0 ]
then
echo "Automatic Mode Selected"
else
while getopts ai option 2>/dev/null
do
case "$option" in
a) echo "Automatic Mode Selected";;
i) echo "Interacive Mode Selected";;
?) echo "Please select proper option"
echo list of available options
-a automatic -i interactive ;;
esac
done
fi

However I will also post universal method to establish the same task.

while [ "$#" -gt 0 ]; do
case "$1" in
-o|--long-option)
: do something
;;
-p|--option-with-arg)
: do something with $2
shift
;;
*)
# this could be an invalid option or just a file
# if no file is expected, do something like:
echo "invalid option or argument: $1"
;;
esac
shift
done

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

Trimming a variable in shell script.

The easiest way to trim a variable in shell script.
VARIABLE=$( echo $VARIABLE )
The shell automatically removes seemingly excessive whitespaces, which is why you have to double quote variables if you want to preserve them.

External Source:
http://www.unix.com/shell-programming-scripting/138857-how-trim-variable-linux.html#post302431753

Take a look at following example.
#!/bin/bash


VARIABLEA="Ajay "
VARIABLEB=" M Pawar"
echo "$VARIABLEA"
echo "$VARIABLEB"
VARIABLEC=$(echo "$VARIABLEA $VARIABLEB")
echo "$VARIABLEC"


VARIABLEM=$(echo $VARIABLEA)
VARIABLEN=$(echo $VARIABLEB)
echo "$VARIABLEM"
echo $VARIABLEN
VARIABLEO=$(echo "$VARIABLEM $VARIABLEN")
echo $VARIABLEO



Check the output yourself.

Wednesday, July 14, 2010

One of the critical element is to know How long the machine is running.
uptime is used for this purpose.
It provides following information.
1)How long the machine is running?
2)How many users are currently logged in the machine.
3)current workload,avg workload for last 5 min,avg workload for last 15 min.

Tuesday, July 13, 2010

How to combine two file using Paste command

# cat file1
milind mumbai
rahul gujrat
mayur surat

# cat file2
10jun
22july
12december

# paste file1 file2
milind mumbai 10jun
rahul gujrat 22july
mayur surat 12december

content of file 1 and file 2 are separated by tab.

If you want to include any other delimiter please use it as follows .
In following example I have used space delimiter.

# paste -d' ' file1 file2
milind mumbai 10jun
rahul gujrat 22july
mayur surat 12december

Monday, July 12, 2010

find a file with specific string inside it

This is very useful command especially when you need to find a file with specific string inside it.

I normally use this command to find out my script which was misplaced in file system.


grep -r 'text to search for' /path/to/dir

Below command is not checked but seems to be working.
reference:
grep -oHnr "some pattern" *.txt
-o print Only the matched parts of a matching line, with each part on a separate line. -H print the filename for each match.
-n prefix each line of output with the 1 based line number.
-R, -r recursively read all files under subdirectories.

Thursday, July 8, 2010

Color Combination used in script.

To make the script more user friendly I have decided to add some colors in output.

3 Basic colors are used in showing the output.

1)Green. (echo -e "\033[32m Hello World")
2)Red. (echo -e "\033[31m Hello World")
3)White. (The "tput sgr0" restores the terminal settings to normal.)

conventions
Green : Command used for execution
Red : User Input
White : Output./Notes


**************************************
More options

BGGREEN=`echo "\033[42m"` # background to green
BGCYAN=`echo "\033[45m"` # background to cyan
BGWHITE=`echo "\033[47m"` # background to white
FGBLACK=`echo "\033[30m"` # foreground to black
FGRED=`echo "\033[31m"` # foreground to red
BGBLACK=`echo "\033[40m"` # background to black
FGGREEN=`echo "\033[32m"` # foreground to green
BGWHITEFGBLUE=`echo "\033[91m"` # background to white & foreground to blue
#BGWHITEFGBLUE=`echo "\033[94m"` # background to white & foreground to blue
BGWHITEFGMAGENTA=`echo "\033[95m"` # background to white & fg to magenta
BGYELLOW=`echo "\033[43m"` # background to yellow
FGBLUE=`echo "\033[34m"` # foreground to blue
FGMAGENTA=`echo "\033[35m"` # foreground to magenta
FGCYAN=`echo "\033[36m"` # foreground to cyan
BGBLUE=`echo "\033[44m"` # background to blue
BGRED=`echo "\033[41m"` # background to red
FGWHITE=`echo "\033[37m"` # foreground to white
BGCYAN=`echo "\033[45m"` # background to cyan
BLINK=`echo "\033[5m"` #terminal to blink
NORMAL=`echo "\033[m"` #terminal to normal
UNDERLINE=`echo "\033[4m"` #terminal to underline
NOUNDERLINE=`echo "\033[24m"` #terminal to cancel underline
REVERSE=`echo "\033[7m"` #terminal to reverse video
BGMAGENTA=`echo "\033[45m"` # background to magenta
BOLD=`echo "\033[1m"` #bold

Click on image to see high resolution version of it.

For more detailed tput information please go through this informative article.
http://www.thegeekstuff.com/2011/01/tput-command-examples/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+TheGeekStuff+%28The+Geek+Stuff%29