Unix Shell Scripting For DBA’s (PART – 02)

Unix Shell Scripting For DBA’s (PART – 02)

Please refer my previous article.

 

Unix Shell Scripting For DBA’s (PART – 01)

Building blocks 

Shell building blocks:

Shell syntax

If input is not commented, the shell reads it and divides it into words and operators, employing quoting rules to define the meaning of each character of input. Then these words and operators are translated into commands and other constructs, which return an exit status available for inspection or processing. The above fork-and-exec scheme is only applied after the shell has analyzed input in the following way:

  • The shell reads its input from a file, from a string or from the user’s terminal.
  • Input is broken up into words and operators, obeying the quoting rules, see Chapter 3. These tokens are separated by meta characters. Alias expansion is performed.
  • The shell parses (analyzes and substitutes) the tokens into simple and compound commands.
  • Bash performs various shell expansions, breaking the expanded tokens into lists of filenames and commands and arguments.
  • Redirection is performed if necessary, redirection operators and their operands are removed from the argument list.
  • Commands are executed.
  • Optionally the shell waits for the command to complete and collects its exit status.

Shell commands

A simple shell command such as touch file1 file2 file3 consists of the command itself followed by arguments, separated by spaces.

More complex shell commands are composed of simple commands arranged together in a variety of ways: in a pipeline in which the output of one command becomes the input of a second, in a loop or conditional construct, or in some other grouping. A couple of examples:

ls | more

gunzip file.tar.gz | tar xvf –

 

Shell functions

Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a “regular” command. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed.

Shell functions are executed in the current shell context; no new process is created to interpret them.

Shell parameters

A parameter is an entity that stores values. It can be a name, a number or a special value. For the shell’s purpose, a variable is a parameter that stores a name. A variable has a value and zero or more attributes. Variables are created with the declare shell built-in command.

If no value is given, a variable is assigned the null string. Variables can only be removed with the unset built-in.

Shell expansions

Shell expansion is performed after each command line has been split into tokens. These are the expansions performed:

  • Brace expansion
  • Tilde expansion
  • Parameter and variable expansion
  • Command substitution
  • Arithmetic expansion
  • Word splitting
  • Filename expansion

Redirections

Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. Redirection may also be used to open and close files for the current shell execution environment.

Executing commands

When executing a command, the words that the parser has marked as variable assignments (preceding the command name) and redirection are saved for later reference. Words that are not variable assignments or redirection are expanded; the first remaining word after expansion is taken to be the name of the command and the rest are arguments to that command. Then redirection are performed, then strings assigned to variables are expanded. If no command name results, variables will affect the current shell environment.

An important part of the tasks of the shell is to search for commands. Bash does this as follows:

Check whether the command contains slashes. If not, first check with the function list to see if it contains a command by the name we are looking for.

If command is not a function, check for it in the built-in list.

If command is neither a function nor a built-in, look for it analyzing the directories listed in PATH. Bash uses a hash table (data storage area in memory) to remember the full path names of executables so extensive PATH searches can be avoided.

If the search is unsuccessful, bash prints an error message and returns an exit status of 127.

If the search was successful or if the command contains slashes, the shell executes the command in a separate execution environment.

If execution fails because the file is not executable and not a directory, it is assumed to be a shell script.

If the command was not begun asynchronously, the shell waits for the command to complete and collects its exit status.

Shell scripts

When a file containing shell commands is used as the first non-option argument when invoking Bash (without -c or -s, this will create a non-interactive shell. This shell first searches for the script file in the current directory, then looks in PATH if the file cannot be found there.

 

Creating and running script.

Writing and naming

A shell script is a sequence of commands for which you have a repeated use. This sequence is typically executed by entering the name of the script on the command line. Alternatively, you can use scripts to automate tasks using the cron facility. Another use for scripts is in the UNIX boot and shutdown procedure, where operation of daemons and services are defined in init scripts.

To create a shell script, open a new empty file in your editor. Any text editor will do: vim, emacs, gedit, dtpad et cetera are all valid. You might want to chose a more advanced editor like vim or emacs, however, because these can be configured to recognize shell and Bash syntax and can be a great help in preventing those errors that beginners frequently make, such as forgetting brackets and semi-colons.

Tip :          Syntax highlighting in vim

In order to activate syntax highlighting in vim, use the command.

Give your script a sensible name that gives a hint about what the script does. Make sure that your script name does not conflict with existing commands. In order to ensure that no confusion can rise, script names often end in .sh; even so, there might be other scripts on your system with the same name as the one you chose. Check using which, whereis and other commands for finding information about programs and files:

Writing a script.

In this example we use the echo Bash built-in to inform the user about what is going to happen, before the task that will create the output is executed. It is strongly advised to inform users about what a script is doing, in order to prevent them from becoming nervous because the script is not doing anything. We will return to the subject of notifying users.

Write this script for yourself as well. It might be a good idea to create a directory ~/scripts to hold your scripts. Add the directory to the contents of the PATH variable

If you don’t want to start a new shell but execute the script in the current shell, you source it:
[

If you  want execute in bash then :

If you want to execute in ksh then :

If you want execute in csh then :

If you did not put the scripts directory in your PATH, and . (the current directory) is not in the PATH either, you can activate the script like this:

Locating your scripts.

Create file in a particular location

  • Using FIND command to locate our scripts.
  • Here .(dot) indicates current location.

Here we specified location called /home/oracle

Using LOCATE command we can find the files.

 

It is saying that though we have script.sh file but its not showing .

Because LOCATE command will work with updated database.

Here we need to know unix operating system have internal database to map the addresses.
To update database we need root access.

Now connect as Oracle user and use locate command.

If we want to comment lines inside the file .

Here # symbol will used to comment a particular line in a file

Write a script and crontab it.

In the script1.sh will add output redirect to capture the output of date command

Check the date and add entry into crontab file.

Here i have scheduled the for 39 mints and 13 hours. check the date and new file 

 

Thank you ……

 

 

 

 

Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment