Tuesday, April 7, 2009

Basic steps to write a shell script

Below is the step by step procedure of how to write shell script and to execute them.

Step 01 - Create a shell script:
Creating a shell script is done as you create any other plain text files. Normally one use any text editor like vi/vim or emacs to create a shell script. To make easy identification of shell script you can give extension of shell script as .sh or .bash
For example, to create a shell script named test.sh issue,

#vi test.sh
or,
#vim test.sh

Step 02 - Give executable permission to shell script:
After creating shell script you need to assign executable permission to the script. It is necessary step to execute the script. Based the executable permission user must have the read permission of the script who will run the script.
You can give the execute permission of test.sh by two ways.

#chmod +x test.sh
or,
#chmod 755 test.sh

Step 03 - Execute the script:
After giving execute permission to the script you can then execute it. With bash or sh or simple dot (.) you can run your shell script.
For example to run the test.sh you can issue,
#bash test.sh
or,
#sh test.sh
or,
#./test.sh

Debugging a script
While writing shell scripts you need to find errors in it i.e debug the shell script and need to correct the errors. The -v and -x option with sh/bash is really useful to debug the script.

The -v option is used to print shell commands as they are read along with the outcome of the script. So if after some command if see desired output is not coming you can note down that commands.

The -x option is used for expanding each command. It displays the command, system variable and then its expanded arguments.

Below is my sample test_echo.sh script which I see how debugging look like.

arju:/home/arju/test# cat test_echo.sh
#!/bin/bash
echo "Warning! You are installing the Fall Creek portal software"
echo -n "Enter the product installation directory: "
read InstallDir
if [ "$InstallDir" = "" ]; then

InstallDir=`pwd`
echo "$InstallDir is used as Fall Creek business portal installation directory."

fi
echo "$InstallDir"

With using -v option.

arju:/home/arju/test# sh -v test_echo.sh
#!/bin/bash
echo "Warning! You are installing the Fall Creek portal software"
Warning! You are installing the Fall Creek portal software
echo -n "Enter the product installation directory: "
Enter the product installation directory: read InstallDir

if [ "$InstallDir" = "" ]; then

InstallDir=`pwd`
echo "$InstallDir is used as Fall Creek business portal installation directory."

fi
pwd
/home/arju/test is used as Fall Creek business portal installation directory.
echo "$InstallDir"
/home/arju/test

With using both -v and -x option.

arju:/home/arju/test# sh -vx test_echo.sh
#!/bin/bash
echo "Warning! You are installing the Fall Creek portal software"
+ echo 'Warning! You are installing the Fall Creek portal software'
Warning! You are installing the Fall Creek portal software
echo -n "Enter the product installation directory: "
+ echo -n 'Enter the product installation directory: '
Enter the product installation directory: read InstallDir
+ read InstallDir

if [ "$InstallDir" = "" ]; then

InstallDir=`pwd`
echo "$InstallDir is used as Fall Creek business portal installation directory."

fi
+ '[' '' = '' ']'
pwd
++ pwd
+ InstallDir=/home/arju/test
+ echo '/home/arju/test is used as Fall Creek business portal installation directory.'
/home/arju/test is used as Fall Creek business portal installation directory.
echo "$InstallDir"
+ echo /home/arju/test
/home/arju/test

Related Documents
http://arjudba.blogspot.com/2009/04/what-is-kernel-shell-shell-script.html

Monday, April 6, 2009

What is kernel, shell, shell script

Kernel
The linux kernel is the most important part or in order word called the heart of the linux operating system. It is the piece of code that manages both hardware and software components and communicates between them. It manages memory, processors, I/O devices with any applications and decides which applications will use hardware resources.

Shell
On your windows machine you have seen command prompt (command.com or cmd.exe) where you type command like ping, ipconfig etc. Similarly, on linux there is such prompt where you can type command and that prompt is called shell. In order to define shell we can say shell is an interpreter that interprets commands as typed from a keyboard or from a text script.

The shell is provided in order to interact with the computer systems. Shell itself is not part of the kernel, but it uses kernel to executes commands.

Shell interprets user commands, not compile them. It reads commands from the script per line and execute the commands, while a compiler converts a program into machine readable form, an executable file - which may then be used in a shell script.

There are various types of shell and can be broadly defined into four categories.

1)Bourne like shell(sh): Almquist shell (ash), Bourne-Again shell (bash), Debian Almquist shell (dash), Korn shell (ksh), Z shell (zsh) falls into bourne like shell category.

2)C shell like (csh): TENEX C shell (tcsh) falls within this category.

3)Non traditional shell: es shell (es), scheme Shell (scsh) falls within this category.

4)Historical shell: Thompson shell (sh), PWB shell or Mashey shell (sh) falls within this category.

In order to know all available shells in your system issue, cat /etc/shells
On my system,
# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh

To find out current shell you are using issue, echo $SHELL or ps $
On my system,

# ps $$
PID TTY STAT TIME COMMAND
4593 pts/0 Ss 0:00 -bash
# echo $SHELL
/bin/bash

The default shell for a user is mentioned within /etc/passwd file. For example to know the default shell of a user named arju issue,
# cat /etc/passwd |grep arju
arju:x:522:522::/home/arju:/bin/bash

To switch from one shell to another shell just write the name of the shell in the shell prompt.

For example to switch into bash shell issue,
#bash

To switch into TENEX C shell issue,
#tcsh

Shell Script
Shell script is a plain text file which contains a series of commands. In this case instead of running command one by one we can store all commands in a script and we execute the script. It is similar to a batch file in MS-DOS.

Related Documents
Introduction -What is kernel, shell, shell script.
Basic Steps to write a shell script
Variables in shell script
Output text with echo command
Arithmetic operation and expression in shell script with expr
Basic shell programming commands quote, exit and read

Linux Shell Script Tutorial

Chapter 1: Getting started with shell scripting
1. Introduction -What is kernel, shell, shell script.
2. Basic Steps to write a shell script
3. Variables in shell script
4. Output text with echo command
5. Arithmetic operation and expression in shell script with expr
6. Basic shell programming commands quote, exit and read
7. Wildcard characters in linux
8. Assignment and comparison variable in shell script
9. Command writing, command line arguments in shell script
10. Input-Output rediection on linux
11. Pipe with example in linux

Chapter 2: Shells Structured Language Constructs
1. If construct in shell script
2. Checking condition in shell scipt by test or [expr]
3. If else construct in shell script
4. For and while loop in shell script
5. Case statement in shell script

Chapter 3: Essential Shell Scripting Commands
1. What is /dev/null on linux
2. Conditional execution && and || in shell script
3. Functions in shell script
4. Trap command on linux shell script
5. The shift command and uses of it in shell script
6. getopts command in linux shell script

Chapter 4: Essential Utilities
1. Retrieve column from file using cut utility in linux
2. Merge lines using paste utility on linux
3. Join utility in linux
4. Translate or replace characters using tr utility
5. Data manipulation using awk utility in linux
6. Edit file on linux using sed utility
7. Remove duplicate successive line using uniq utility
8. Find pattern from a file using grep, egrep or fgrep utility

Chapter 5: List of special characters used in shell script (Advanced)

Sunday, April 5, 2009

A simple shell script to check whether user is root

In many case inside your shell script you need to check whether the user who runs the script is root or not because based on the user you might need to take necessary action inside your shell script.

Following shell script might help you in this case.

Way 01: With system variable $LOGNAME
As echo $LOGNAME show the currently logged in user show we can use this variable to determine whether it is root user.
1)create script
# vi test_root.sh
if [[ $LOGNAME = "root" ]]
then
echo "You are root user"
else
echo "You are $LOGNAME user"
fi

2)Grant execute permission.
# chmod +x test_root.sh

3)Test it by running as root.
# whoami
root

# ./test_root.sh
You are root user

4)Test it by running as oracle user.
# su - oracle

$ whoami
oracle

$ ./test_root.sh
You are oracle user

Way 02: As system variable $UID:
As system variable $UID for root user is zero so we can use that in order to determine whether user is root.
1)create script
# vi test_root2.sh
if [[ $UID = "0" ]]
then
echo "You are root user"
else
echo "You are not root user"
fi

2)Give execute permission.
# chmod +x test_root2.sh

3)Test it by running as root user.
# ./test_root2.sh
You are root user

Way 03: With the command whoami
With the command whoami you can check the user who is logged in. So you can use that in your script.
1)Create script
# vi test_root3.sh
if [ `whoami` = "root" ]
then
echo "You have logged on as root"
else
echo "You are not root user"
fi

2)Grant execute permission.
# chmod +x test_root3.sh

3)Test the script by running it.
# ./test_root3.sh
You have logged on as root

Friday, April 3, 2009

Different types of standby database in oracle data guard

In oracle data guard configuration, you need to setup one or more additional databases beside the primary database. These additional databases are called standby database. Up to nice standby database can be created for one primary database.

Using a backup of primary database you can set up standby database and then you can made standby database as part of data guard configuration. Once you configured standby database, data guard automatically maintains standby database by transmitting redo log from the primary database and then applying redo to the standby database.

A standby database can be of three types.

1)Physical Standby Database: A physical standby database is an identical copy of the primary database. The disk structures are also identical with primary database. It is kept synchronized with the primary database by Redo Apply- which means the redo data is received from the primary database and then redo is applied to the physical standby database.

Note that as of Oracle Database 11g release 1 (11.1), a physical standby database can receive and apply redo while it is open for read-only access. You can use physical standby database for query and reporting purpose along with data protection.

2)Logical Standby Database: A logical standby database is the same logical information of the primary database. The physical data structure need not to be same on the standby database. It is kept synchronized with the primary database by SQL Apply- which means the redo data is received from the primary database, transforms redo data into SQL statements and at last executes the SQL statements on the standby database.

You can use logical standby database for query and reporting purpose along with data protection. Also you have to facility to upgrade oracle database software and patch sets along with data protection with help of logical standby database.

3)Snapshot Standby Database: A snapshot standby database is a convertible copy of the physical standby database but the difference from the physical or logical standby database is, the redo data that it received does not apply into it. The redo is applied whenever it is converted back to the physical standby database. You can play with the snapshot standby database and while converting to physical standby database from snapshot standby database these local updates are discarded.

Note that in case of snapshot standby database, the time needed to perform a role transition is directly proportional to the amount of redo data that needs to be applied.

Related Documents
http://arjudba.blogspot.com/2009/04/what-is-oracle-data-guard.html

Thursday, April 2, 2009

What is oracle data guard

Oracle data guard is the term(you can call it a feature too) used by oracle and this feature is integrated with oracle RDBMS. Data guard create, maintain, manage and monitor one/more extra databases -called standby database, beside production database. The goal is to survive production databases from any disasters, failure or data corruptions.

At first, an exact replica of production database is created, which referred to as standby database later. Data guard then maintains and keep up to date of these standby databases with of production database. If production database becomes unavailable then data guard can switch standby database to production role and thus minimize the downtime without any data loss.

In addition to availability of production database data guard gives other advantages. Like we can do real time query in standby database, do backup operation in it and thus minimize the load of production database.

There is no restriction about the location of the stand by databases. They can be located thousands miles away from the production database or can be within same room, provided they can communicate each-other.

The more features, configurations, advantage about oracle data guard will be discussed gradually.
Related Documents
http://arjudba.blogspot.com/2009/04/different-types-of-standby-database-in.html

Wednesday, April 1, 2009

Object interface example in php

Think about your laptop or computer, you did not interact with the things that is inside within it. To on your machine you press the power button. Similarly you interact with your machine through mouse, keyboard etc. The methods through which you interact with the object togetherly forms an interface.

You can define an interface in this way. An interface is the group of related methods without bodies. Details implementation of the interface will be within classes that implements the interface.

Note that, all methods in the interface must be public as by it's nature and if a class implements an interface all the methods must be defined within the class. Interface methods does not tell how it will work that is details implementation of interface methods must be handle within the class that implements the interface; how that class will use the method.

You can't create instance of an interface. Interface can be subclassed.

To create an interface you have to use interface keyword followed by the curly brace. Methods declaration will be inside the curly brace. To implements an interface you have to use implements keyword with the class definition that will implement the interface.

Below is an example of using interface.

<?php
interface a{
public function method1();
}
interface b{
public function method2();
}
interface c extends a,b{
public function method3($name);
}
class d implements c{
public function method1(){
}
public function method2(){
}
public function method3($name){
}
}

?>

Related Documents
http://arjudba.blogspot.com/2009/03/abstract-class-and-abstract-method-with.html
http://arjudba.blogspot.com/2009/03/static-declaration-with-example-in-php.html
http://arjudba.blogspot.com/2009/03/class-concepts-and-usage-syntax-in-php.html
http://arjudba.blogspot.com/2009/03/class-inheritance-example-in-php.html
http://arjudba.blogspot.com/2009/03/autoloading-objects-in-php.html
http://arjudba.blogspot.com/2009/03/constructors-and-destructors-in-php.html