Introduction to Shell Scripting
{LANG_NAVORIGIN} Operating System Linux
Colin Sauze
06/27/2005
This series focuses on shell scripting in Unix/Linux and focuses on using the bash scripting language. The assumption is that you are new to both Unix and programming. However you will still find things easier to understand if you have some programming experience in a high level language like Basic, C/C++, Java or Pascal/Delphi and some experience with Unix or Linux.
What is a shell?
In Unix (that includes Linux, OSX, BSD etc) , after you have been authenticated, you are presented with an interactive shell. If your login is a text based one then you will probably be presented with your shell as soon as you've logged in, if you use a graphical logon as many modern Unix systems do then you can access a command shell through a graphical front end such as X-Term, KDE's konsole or gnome-terminal. The shell allows you to issue commands to the system and instigate other processes. Each user on a Unix system has a default shell, (specified in ‘/etc/passwd’) that they will be launched into at login time. For most systems this is either bourne shell, also known as sh or the GNU Bourne Again Shell often known as bash, which is an extension upon the original bourne shell. There are several other common shells including csh, ksh and zsh, tcsh, these all offer pretty much the same features as bash and only tend to be used by people who started using Unix before bash was mature (mid 1990s). You can find out what your default shell is by typing “echo $SHELL” at the command line. From here on this tutorial will focus on bash.
What is a shell script?
A shell script is a file which consists of commands you could normally type into the shell. By running the shell script all of these commands are then executed automatically as if you had typed them in one by one. Shell scripting languages include a number of commands similar to those found in most programming languages, these include features to allow you to loop to make an action execute several times, to test if a condition is true or not and perform different actions either way and the ability to jump to another part of the program rather than having to run the program in the exact order the commands are written.
Most shells also provide a number of facilities to ease interactive use (where you type commands one by one), this tutorial doesn't aim to describe these features in any great detail. However a few things you may find useful:
- If you start typing the name of a file or command and press TAB the shell will either finish it off for you or list several possible suggestions if there is more than one.
- Pressing up shows you the previous command you typed, pressing up again the one before that and so on.
- You can make command aliases, so typing one thing actually runs a very different command. This is useful for commonly used commands which are very long or for making one shell seem like another (a common example is aliasing dir which is a Dos command to ls its Unix equivalent).
Why would I need a shell script?
Shell scripts are ideal for writing programs to automate tasks that require you to type in sequences of commands to your shell. Some examples of this might be backing up a directory, compressing it, encrypting it and copying it to another computer or removing a log files older than a certain date. Shell scripts are in use all over the place in most Unix systems, the best example being when the system boots. When most Unix systems boot up they execute a series of shell scripts to start-up all the services they require (known as daemons) such services might include web servers, email servers and hardware monitoring programs (e.g. Programs to look handle the user connecting a new USB device). Shell scripts are very good at manipulating text data, processing the output of other programs and “gluing” bigger programs together to do something much more useful. They often prove to be a much quicker way of writing a program than using a high level language such as C or Java. They also tend to be very portable between different Unix compatible operating systems (e.g. Between Linux and Solaris) and between different types of hardware, this is because shell scripts are interpreted by the shell as they are run whereas most programs are compiled to a machine code which only certain types of processor understand.
When shouldn't I use a shell script?
Shell scripts do not execute particularly fast and are not well suited to number crunching programs such as data analysis tools, compression, image manipulation etc. They offer little in the way of support for graphical user interfaces or even text mode interfaces and shouldn't really be used to build graphical applications although there are some exceptions to this which will be discussed in part 3. Shell scripts often don't offer the best memory efficiency and if your application really must run with the absolute least amount of memory (or its dealing with so much data that this make a big difference) then you should consider writing it in a compiled programming language like C.
What are the main features of shell scripting languages?
Like most programming languages shell scripts can perform loops so that a given piece of code is run multiple times without the programmer having to write it out multiple times. They can perform logic and take one course of action if something is true and a different one if its false. They can be used to launch other programs, other shell scripts or other parts of the same script. Some shells (such as bourne shell) cannot actually perform arithmetic operations on their own but must enlist the support of extra helper programs to do this, this is not the case with BASH, however for certain arithmetic operations, a lightweight tool such as ‘bc’ is always useful.
More Linux tutorials and guides
E-Mail Link
Your IP address will be sent with this e-mail