Writing tcsh shell scripts
Most people find themselves using some sequences of commands over
and over again.
- A programmer may always use the same commands to compile source code, and link the resulting object code.
- A bookkeeper may have to go through the same sequence of shell commands each week to update the books and produce a report.
To simplify such jobs, the shell lets you run a sequence of commands that have been stored in a text file. For example, the programmer could store all the appropriate compiling and linking commands in a file. A file containing commands in this way is called a shell script. After such a file is completed and it is made “executable,” the programmer can run all the commands in the file by entering the file name on the command line.
Putting commands in a shell script has several advantages over
typing the commands individually. Using a shell script:
- Reduces the amount of typing you have to do. You have to type in the shell script only once. Then you can run all the commands in the script by entering the name of the file as a single shell command. A shell script can save you a lot of time and effort if you are working with many files, or if some command lines have several options.
- Reduces the number of errors. If you are typing in ten commands, you have ten chances to make a mistake. With a shell script, however, you can take your time, edit the file carefully, and get it right before you try to run it.
- Makes it easy for other people to do what you do. For example, consider the bookkeeper example. When the bookkeeper goes on vacation, someone else has to fill in. It is much easier for the substitute bookkeeper to type a single command that does everything correctly than to try to type in the full sequence of commands.
For all these reasons, you will probably find that the use of shell scripts makes your work easier and more productive. This topic provides only a brief overview, but it should give you an idea of how to write and use shell scripts.