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

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

reference of this article from : www.orskl.com

What is SHELL ?

Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program’s output.

Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.

Shell Prompt

The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command.

BASH SCRIPT BASICS

Shell programs

General shell functions –

The UNIX shell program interprets user commands, which are either directly entered by the user, or which can be read from a file called the shell script or shell program. Shell scripts are interpreted, not compiled. The shell reads commands from the script line per line and searches for those commands on the system, while a compiler converts a program into machine readable form, an executable file – which may then be used in a shell script.

Apart from passing commands to the kernel, the main task of a shell is providing a user environment, which can be configured individually using shell resource configuration file

Shell types-

Just like people know different languages and dialects, your UNIX system will usually offer a variety of shell types. They are :

  • Bourne shell (sh)
  • Bourne Again shell (bash)
  • C shell (csh)
  • TENEX/TOPS C shell (tcsh)
  • Korn shell (ksh)

sh (or) Bourne Shell: the original shell still used on UNIX systems and in UNIX-related environments. This is the basic shell, a small program with few features. While this is not the standard shell, it is still available on every Linux system for compatibility with UNIX programs.

Bash (or) Bourne Again shell: the standard GNU shell, intuitive and flexible. Probably most advisable for beginning users while being at the same time a powerful tool for the advanced and professional user. On Linux, bash is the standard shell for common users. This shell is a so-called superset of the Bourne shell, a set of add-ons and plug-ins. This means that the Bourne Again shell is compatible with the Bourne shell: commands that work in sh, also work in bash. However, the reverse is not always the case. All examples and exercises in this book use bash.

csh or C shell: the syntax of this shell resembles that of the C programming language. Sometimes asked for by programmers.

tcsh or TENEX C shell: a superset of the common C shell, enhancing user-friendliness and speed. That is why some also call it the Turbo C shell.

ksh or the Korn shell: sometimes appreciated by people with a UNIX background. A superset of the Bourne shell; with standard configuration a nightmare for beginning users.

 

The file /etc/shells gives an overview of known shells on a Linux system :

Your default shell is set in the /etc/passwd file, like this line for user

To check which shell is currently running.

The default shell of  Unix is /bin/bash.

To switch from one shell to another, just enter the name of the new shell in the active terminal. The system finds the directory where the name occurs using the PATH settings, and since a shell is an executable file (program), the current shell activates it and it gets executed. A new prompt is usually shown, because each shell has its typical appearance:

 

To check  permissions on /bin/bash.

EXECUTING COMMANDS :

General :-

Bash determines the type of program that is to be executed. Normal programs are system commands that exist in compiled form on your system. When such a program is executed, a new process is created because Bash makes an exact copy of itself. This child process has the same environment as its parent, only the process ID number is different. This procedure is called forking.

 

After the forking process, the address space of the child process is overwritten with the new process data. This is done through an exec call to the system.

 

The fork-and-exec mechanism thus switches an old command with a new, while the environment in which the new program is executed remains the same, including configuration of input and output devices, environment variables and priority. This mechanism is used to create all UNIX processes, so it also applies to the Linux operating system. Even the first process, init, with process ID 1, is forked during the boot procedure in the so-called bootstrapping procedure.

Shell built-in commands :-

Built-in commands are contained within the shell itself. When the name of a built-in command is used as the first word of a simple command, the shell executes the command directly, without creating a new process. Built-in commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.

Bash supports 3 types of built-in commands:

Bourne Shell built-ins:

:, ., break, cd, continue, eval, exec, exit, export, getopts, hash, pwd, readonly, return, set, shift, test, [, times, trap, umask and unset.

Bash built-in commands:

alias, bind, builtin, command, declare, echo, enable, help, let, local, logout, printf, read, shopt, type, typeset, ulimit and unalias.

Examples :-

Special built-in commands:

When Bash is executing in POSIX mode, the special built-ins differ from other built-in commands in three respects:

These types of commands we generally use in Database.

Special built-ins are found before shell functions during command lookup.

If a special built-in returns an error status, a non-interactive shell exits.

Assignment statements preceding the command stay in effect in the shell environment after the command completes.

The POSIX special built-ins are :, ., break, continue, eval, exec, exit, export, read only, return, set, shift, trap and unset.

Most of these built-ins will be discussed in the next chapters. For those commands for which this is not the case, we refer to the Info pages.

Executing programs from a script

She-Bang – #! : this will decide in which shell the script has to be executed. It should always be defined in the first line of the script.

When the program being executed is a shell script, bash will create a new bash process using a fork. This subshell reads the lines from the shell script one line at a time. Commands on each line are read, interpreted and executed as if they would have come directly from the keyboard.

 

While the sub shell processes each line of the script, the parent shell waits for its child process to finish. When there are no more lines in the shell script to read, the sub shell terminates. The parent shell awakes and displays a new prompt.

 

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

Add Comment