3.1. Shell Scripting Introduction
3.1. Shell Scripting Introduction
OVERVIEW
Time is precious. It is non-sense-ical to waste time typing a frequently used sequence of commands at a command prompt, more especially if they are abnormally long or complex. Scripting is a way by which one can alleviate this necessity by automating these command sequences in order to make ones life at the shell easier and more productive. Scripting is all about making the computer, the tool, do the work. opefully by the end of this tutorial you should have a good idea of the kinds of scripting languages available for !nix and how to apply them to your problems.
2. ATMOSPHERE
In order to peruse this tutorial with ease it is probably necessary to know a little bit about how !nix works, if you are new to !nix you should read the documentation" Configuring A Unix Working Environment #..$unixenvars$unixenvarshome.html%, if you are not new to !nix you should browse it to check that you know everything it discusses. !nix contains many wonderful and strange commands that can be very useful in the world of scripting, the more of the tools you know and the better you know them, the more use you will find for them in your scripting. &ost of the !nix commands and many of the builtin commands have man pages, man pages contain the usage instructions and other stuff pertaining to the parent tool. They are not always very clear and may require reading several times. In order to access a man page in !nix the following command sequence is applied"
man command
If a man page exists for the command specified the internal viewer will be invoked and you will be able to read about the various options and usage instructions etc.
3. Shell Scripting
3.1. Shell Scripting Introduction
!nix uses shells to accept commands given by the user, there are quite a few different shells available. The most commonly used shells are S '(ourne S ell) *S '* S ell) and +S '+orn S ell), most of the other shells you encounter will be variants of these shells and will share the same syntax, +S is based on S and so is (,S '(ourne again shell). T*S '-xtended * S ell) is based on *S . 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. .ith all these different shells available, what shell should we script in/ That is debatable. 0or the purpose of this tutorial we will be using S because it is practically guaranteed to be available on most !nix systems you will encounter or be supported by the S based shells. 1our default shell may not be S . 0ortunately 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.
#!/path/to/shell
!sually anything following '2) is interpreted as a comment and ignored but if it occurs on the first line with a '3) following it is
treated as being special and the filename following the '3) is considered to point to the location of the shell that should interpret the 4 script. .hen a script is 5executed5 it is being interpreted by an invocation of the shell that is running it. ence the shell is said to be running non-interactively, when the shell is used 5normally5 it is said to be running interactively.
Note
There are many variations on the basic commands and extra information which is too specific to be mentioned in this short tutorial, you should read the man page for your shell to get a more comprehensive idea of the options available to you. This tutorial will concentrate on highlighting the most often used and useful commands and constructs.
6 .e use < to specify that we want the command immediately before the redirection symbol to get its input from the source specified immediately after the symbol, for instance, we could redirect the input to grep'which searches for strings within files) so that it comes from a file like this"
grep searchterm < file
6 .e use >> to append stdout to a file, for instance, if we wanted to append the date to the end of a file we could redirect the output from date like so"
date >> file