Friday, January 8, 2010

Testing a Condition

You want to write a script that checks for certain conditions such as the number of parameters passed to a script. Based on the condition, you want to perform an action such as display an informational message or exit the script.

The if/then/else structure is an important programming technique. However, it is the combination of if/then/else with a condition that can be tested that gives you a much more powerful tool to automate DBA tasks. The test command gives you the ability to check a condition within an if command.
Here is the basic syntax for the test command:
test operand1 operator operand2
The test command can also be written with the [ ] syntax. This syntax uses a left brace to start the command and then finishes the command with a right brace. Its syntax is as follows:[ operand1 operator operand2 ]

Note :- The shell script examples in this note will use the [ ] form of the test command.
For some test conditions, an operand1 isn't required. The syntax for this condition is as follows:

[ operator operand2 ]

The previous test conditional checks will exit with a status of 0 (true) or 1 (false), depending on the evaluation of the condition. Ensure that you have a space between the operands, operators, and brackets. The space is how the shell knows where the operator and operand are separated. If there is no space between the operator, operand, and brackets, then the shell will interpret the value as one string, which will result in erroneous outcomes.

Verifying the Number of Parameters

To bring if/then/else and test together, you'll write a small—but useful—piece of code that checks to see whether the correct number of parameters are passed to a script. The script will use the $# parameter. The $# parameter automatically gets assigned to the number of positional parameters typed at the command line. The $# variable is handy when you want to check for the correct number of parameters passed to a script.
The following bit of code uses the -ne conditional check to determine whether the number of variables passed to the script is not equal to 1:

#!/bin/bash
if [ $# -ne 1 ]
then
echo "Wrong number of parameters passed to script."
exit 1
fi

The $0 parameter is often used in conjunction with the $# parameter to display the correct syntax required when invoking a script. Within a shell script, the $0 parameter contains the name of the shell script being executed. Here's a slight variation of the previous code that uses the $0 variable to display the name of the script:

if [ $# -ne 1 ]
then
echo "Wrong number of parameters passed to script."
echo "Usage: $0 ORACLE_SID"
exit 1
fi

The -ne operator is an arithmetic operator and is used to test whether the operands are not equal. If the script is called without passing exactly one parameter to it, then the following output is displayed:

Wrong number of parameters passed to script.
Usage: ./ss.bsh ORACLE_SID
Notice that there is a ./ in front of the script name in the previous output. To scrape the ./ out of the output, use the basename command as follows:

Pgm=$(basename $0)
if [ $# -ne 1 ]
then
echo "Wrong number of parameters passed to script."
echo "Usage: $Pgm ORACLE_SID"
exit 1
fi
In the previous piece of code, notice the first line of code uses a shell technique known as command substitution. Command substitution allows you to take the output of a command and load it into a variable. The basic syntax for doing this is as follows:

variable=$(shell commands)

Verifying the Amount of Physical Memory
Another arithmetic check that you may want to do is to verify the amount of physical memory on your database server. Here's a simple script that verifies that the database server memory is greater than 1 gigabyte:

#!/bin/bashthresh=1048576
totMem=$(grep MemTotal /proc/meminfo awk '{print $2}')
if [ $totMem -lt $thresh ];
then
echo "Total Memory $totMem is less than: $thresh"
exit 1
else
echo "Total Memory is: $totMem"
fi

Several arithmetic operators are available with the Bash shell.

Checking the Name of User Running the Script
You can also use strings with test conditions. There are a wide variety of ways to use string comparisons. Below is the lists test operations for strings and their descriptions. For example, you may want to check to ensure that you're logged on as a certain operating system user before you run commands in a script. This example checks to see whether the user running the script is oracle:

#!/bin/bash
checkUser=oracle
curWho=$(whoami)
if [ "$curWho" != "$checkUser" ];
then
echo "You are currently logged on as: $curWho"
echo "Must be logged in as $checkUser to run this script.."
exit 1
fi

In the previous bit of code, the curWho variable is assigned to the name of the user running the script. That string variable is then checked to see whether it matches the string of oracle. If user doesn't match, then the script displays informational messages and exits the script.

Accepting Input from the Command Line

Another useful example of a string comparison is to read input from a user and verify an operation. Suppose you want to check the current Oracle SID variable before continuing to run more commands within the script. This is useful if you work with multiple databases contained on one physical server. This script displays the value of ORACLE_SID and asks whether you want to continue running the script:
#!/bin/bash
keepGoing=n
echo "Current value of ORACLE_SID: $ORACLE_SID"
echo -n "Do you want to continue? y/n "
read keepGoing
if [ "$keepGoing" = "y" ];
then
echo "Continue to run script."
else
echo "Exiting script"
exit 1
fi

How It Works
In addition to arithmetic and string comparisons, you can also perform various tests on operating system files. The test command allows you to perform checks such as the availability of a file, the file type, and so on. Below table contains descriptions of the Bash shell tests for file operations.
For example, you may want to determine whether an error log file exists and, if it does, then send an e-mail to the appropriate support person. This script uses the -e parameter of the test command to determine this:
#!/bin/bash
checkFile=/home/trace/error.log
if [ -e $checkFile ];
then
mail -s "errors" bbill@gmail.com <$checkFile
else
echo "$checkFile does not exist"
fi

If you want your shell script to do nothing after checking for a condition, then use the colon (:) command (sometimes called no-operation or null). For example, the following bit of code does nothing if it detects that the given file exists:
#!/bin/bash
checkFile=/home/oracle/.bashrc
if [ -e $checkFile ];
then
:
else
echo "$checkFile does not exist"
fi

Tip

The test options will vary by vendor and version of Linux. For a complete listing of available test operations in your environment, use the help test command or the man test command.

No comments:

Post a Comment