Thursday, January 7, 2010

Shell Script - Tips

Writing a Simple Shell Script
Problem
You're new to shell programming, and you want to write a simple script to determine whether your database is accessible.
Solution
Use an editor to create a new file (see Chapter 4 for details on using the vi file editor). Place within the file the following text:

#!/bin/bash
ORACLE_SID=SCDEV
ORACLE_HOME=/orahome/oracle/product/10.2.0.1/db_1
PATH=$ORACLE_HOME/bin:$PATH
echo "select 'DB up' from dual;" | sqlplus -s system/foo
exit 0

Modify the previous code to match your ORACLE_SID and ORACLE_HOME variables and your system schema password. After you've created the file, you'll also need to modify the permissions on the file to be executable. In this example, the file name is dbcheck.bsh:

$ chmod u+x dbcheck.bsh

This changes the permission for the owner (u) of the file to executable (x). Now you should be able to successfully run the program:
$ dbcheck.bsh
If your database is up, you should receive a message like this:

'DBUP
-----
DB up

All of the checking within the script is performed with one line of code:
echo "select 'DB up' from dual;" | sqlplus -s system/foo
The echo command pipes a valid SQL statement to the sqlplus executable. The sqlplus executable will then attempt to log on with the system schema and run the statement.


Bash Shell Exit Command

You can place an exit command at any location within a shell script to instruct the Bash shell to immediately terminate the program. A successful exit is normally specified with an exit or an exit 0. Exiting a shell script when a failure condition has been detected is indicated by a nonzero value, such as an exit 1. We recommend you explicitly place an exit 0 command within your shell script to indicate a successful completion. You should also use a nonzero value such as an exit 1 to indicate that some sort of an error condition has been detected.
Each Bash shell command that executes will also return an exit code. If a command executes successfully, it will terminate with a status of 0. If there has been some sort of a failure, the exit code will be nonzero. You can check the status of an exit code by inspecting the $? variable. The $? variable holds the exit value of the previously executed command. The nonsuccess value of an exit code will vary by each command. For example, the grep utility will return a 0 on successfully finding a match, a 1 if no matches are found, and a 2 if there has been some sort of a syntax error or missing input file.


How It Works

The first line of the shell script in the "Solution" section of this recipe needs a little more explanation. We've reproduced the line here for the discussion:

#!/bin/bash

The # character is normally used to comment out a line in a shell script. One exception to that rule is when #! appears as the first text in the shell script. When #! is placed on the first line, it can be then combined with a path and program name. The path and program name specify the location and name of the program that will interpret the commands within the script. This is important because it means you can have the script run with a designated shell regardless of the interactive shell you're using.
If you don't specify a #!//, then the shell you're currently logged on to will be used to interpret the commands within the script. We recommend you specify the path and shell program on the first line of your script so that there is no ambiguity about which shell will be used to interpret the commands within the script.
On most Linux distributions, the bash interpreter is in the /bin directory. If you don't know the location of your bash executable, you can use the which or whereis command to locate it:
$ whereis bash
bash: /bin/bash
When you first attempt to run a shell script, you may receive an error similar to the following:
-bash: dbcheck.bsh: command not found

This means your PATH variable doesn't include the current working directory. To work around this, you can reset your PATH variable to include the current working directory. This example exports the PATH variable to include the current working directory:
$ export PATH=$PATH:.
Another method for ensuring that the Bash shell can locate a script is to include the complete directory path to where the script resides. In this example, the script is located in the directory /home/oracle and is run as shown here:
$ /home/oracle/dbcheck.bsh
You can also instruct the shell to look in the current working directory to determine the location of the script. You do this by placing a ./ before the script name:
$ ./dbcheck.bsh

Note Adding the file extension .sh, .bsh, or .bash to the end of a Bash shell script is a common industry practice. Keep in mind that file extensions are meaningless in Linux/Unix environments (other than helping you document the type of script). This is different from DOS where .exe, .com, and .bat indicate executable operating system files.

No comments:

Post a Comment