Welcome to Unix Shell Scripting Tutorials. The objective of these tutorials is to provide in depth understand of Unix Shell Scripting.
In addition to free Unix Shell Scripting Tutorials, we will cover common interview questions, issues and how to’s of Unix Shell Scripting.
Unix uses shells to accept commands given by the user, there are quite a few different shells available. The most commonly used shells are SH(Bourne SHell) CSH(C SHell) and KSH(Korn SHell), most of the other shells you encounter will be variants of these shells and will share the same syntax, KSH is based on SH and so is BASH(Bourne again shell). TCSH(Extended C SHell) is based on CSH.
The various shells all have built in functions which allow for the creation of shell scripts, that is, the stringing together of shell commands and constructs to automate what can be automated in order to make life easier for the user.
With all these different shells available, what shell should we script in? That is debatable. For the purpose of this tutorial we will be using SH because it is practically guaranteed to be available on most Unix systems you will encounter or be supported by the SH based shells. Your default shell may not be SH. Fortunately we do not have to be using a specific shell in order to exploit its features because we can specify the shell we want to interpret our shell script within the script itself by including the following in the first line.
-Usually anything following (#) is interpreted as a comment and ignored but if it occurs on the first line with a (!) following it is treated as being special and the filename following the (!) is considered to point to the location of the shell that should interpret the script.
-When a script is “executed” it is being interpreted by an invocation of the shell that is running it. Hence the shell is said to be running non-interactively, when the shell is used “normally” it is said to be running interactively.
The UNIX operating system is made up of three parts
the kernel, the shell and the programs.
The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.
As an illustration of the way that the shell and the kernel work together, suppose a user types rm myfile(which has the effect of removing the file myfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile. When the process rm myfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.
The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems).
The adept user can customise his/her own shell, and users can use different shells on the same machine. Staff and students in the school have the tcsh shell by default.
The tcsh shell has certain features to help the user inputting commands.
Filename Completion – By typing part of the name of a command, filename or directory and pressing the [Tab] key, the tcsh shell will complete the rest of the name automatically. If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab key again.
History – The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.
Files and processes
Everything in UNIX is either a file or a process.
A process is an executing program identified by a unique PID (process identifier).
A file is a collection of data. They are created by users using text editors, running compilers etc.
Examples of files:
A document (report, essay etc.)
The text of a program written in some high-level programming language
Instructions comprehensible directly to the machine and incomprehensible to a casual user, for example, a collection of binary digits (an executable or binary file);
A directory, containing information about its contents, which may be a mixture of other directories (sub directories) and ordinary files.
All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root (written as a slash / )
In the diagram above, we see that the home directory of the undergraduate student “ee51vn” contains two sub-directories (docs and pics) and a file called report.doc.
The full path to the file report.doc is “/home/its/ug1/ee51vn/report.doc”
To open an UNIX terminal window, click on the “Terminal” icon from Applications/Accessories menus
An UNIX Terminal window will then appear with a % prompt, waiting for you to start entering commands.
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data.
A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.
The name of a variable can contain only letters ( a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).
By convention, Unix Shell variables would have their names in UPPERCASE.
The following examples are valid variable names −
_ALI TOKEN_A VAR_1 VAR_2
Following are the examples of invalid variable names −
2_VAR -VARIABLE VAR1-VAR2 VAR_A!
The reason you cannot use other characters such as !,*, or – is that these characters have a special meaning for the shell.
Variables are defined as follows −
Above example defines the variable NAME and assigns it the value “Zara Ali”. Variables of this type are called scalar variables. A scalar variable can hold only one value at a time.
The shell enables you to store any value you want in a variable. For example −
VAR1="Zara Ali" VAR2=100
To access the value stored in a variable, prefix its name with the dollar sign ( $) −
For example, following script would access the value of defined variable NAME and would print it on STDOUT −
#!/bin/sh NAME="Zara Ali" echo $NAME
This would produce following value −
The shell provides a way to mark variables as read-only by using the readonly command. After a variable is marked read-only, its value cannot be changed.
For example, following script would give error while trying to change the value of NAME −
#!/bin/sh NAME="Zara Ali" readonly NAME NAME="Qadiri"
This would produce following result −
/bin/sh: NAME: This variable is read only.
Unsetting or deleting a variable tells the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you would not be able to access stored value in the variable.
Following is the syntax to unset a defined variable using theunset command −
Above command would unset the value of a defined variable. Here is a simple example −
#!/bin/sh NAME="Zara Ali" unset NAME echo $NAME
Above example would not print anything. You cannot use the unset command to unset variables that are marked readonly.
When a shell is running, three main types of variables are present
-Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at command prompt.
-Environment Variables − An environment variable is a variable that is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually a shell script defines only those environment variables that are needed by the programs that it runs.
-Shell Variables − A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.
-Easy to use.
-Quick start, and interactive debugging.
-Sys Admin task automation.
-Shell scripts can execute without any additional effort on nearly any modern UNIX / Linux / BSD / Mac OS X operating system as they are written an interpreted language.
-Compatibility problems between different platforms.
-Slow execution speed.
-A new process launched for almost every shell command executed.