Introducing UNIX and Linux |
Introduction to shellsOverview |
Shell syntaxAny computer language has a syntax - that is, a set of rules defining what you can write in the language, and what you are forbidden to write. The shell uses symbols and words in a very precise way. We will not attempt to give a formal definition of the shell's syntax here, but we will describe most of its features. Recall that a script is a file containing commands the shell is able to interpret. If a script is two or three lines long, it is likely to be clear to anyone reading the file what the commands do, and therefore the purpose of the script. If a script is a hundred lines long, it will not be so easy to see what is happening. Not only will it be difficult for someone else reading your scripts but if you make an error while writing it, you may yourself find trouble discovering exactly where the error has occurred. It is good practice to include comments in your scripts. These are messages the shell ignores - they are there merely for the benefit of anyone reading the scripts. If you write a script, and include a
causes a message to be displayed on the terminal screen. The next script does exactly the same thing, but has had comments included.
The sort of information that should appear in comments includes
For very short scripts, this may appear rather trivial, but for
longer scripts comments are essential, and you should get into the
habit of commenting all your scripts. Comments only work in
scripts, and will not work interactively, when you are giving the
shell instructions from a terminal. Try creating a file
(
When the shell reads input, it reads the input line-by-line
looking for commands, and each line is first of all stripped of
comments. A command is normally terminated by the end of the line
on which it appears, with the exception that if the end of the line
is premature, and the shell knows you haven't completed typing in
the command, it will recognise this and expect you to continue the
command on the next line. If the shell is interactive, it will
prompt you with a
You can also try creating a file containing echo "Hello Chris" and executing the file as a shell script. Remember that the only difference between a shell script (which is a file containing shell commands) and the commands you type in on your terminal, is where the input to the shell comes from. In the case of an interactive shell, such as your login shell, the shell commands are typed in by you at your terminal; when a script is run, the shell reads commands from the script file. You can also terminate a command with the symbols
A sequence of commands separated by semicolons or Newlines is called a sequential list, and a sequence separated by ampersands is an asynchronous list. |
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck