[PDC - Center for Parallel Computers, KTH]

Tutorial on Using and Writing Shell Scripts


Table of Contents


What are Shell Scripts?

Shell scripts are ASCII files that contain Unix and shell commands. Unix commands are the familiar commands like ls, rm, cat, and many others. Shell commands are those commands that are interpreted directly by the shell you specify. Shell commands are commonly used for branching, looping, decision making, etc. They are similar to the commands in programming languages, particularly C. Popular shells include the Bourne (sh), C (csh), and Korn (ksh) shells. This tutorial will concentrate on the C shell.

The commands you enter in a shell script are executed by typing the name of the script instead of each individual command. Scripts serve the same purpose as EXECs in CMS, or BAT files in MS/DOS.

Scripts are commonly used to execute a set of commands that is repeated many times. For instance, if you wanted to rename a large number of files that have uppercase names, to lowercase names, a script could be written that would save you a lot of typing.

The Bourne and C shell are similar in the features they offer. In particular, the shells let you:

Information about the commands a shell offers can be found in the man pages for the particular shell. Also, many Unix reference books give detailed explanations about how to use shell commands.


How to Run Shell Scripts

There are two ways you can execute your shell scripts. Once you have created a script file:

Method 1: Pass the file as an argument to the shell that you want to interpret your script.

For Bourne or Korn shell scripts:
	sh script_file
or for C shell scripts:
	csh script_file

For example, suppose the file myscript has C shell commands in it. It could be executed by typing:

	csh myscript
Method 2: Make your script executable using the chmod command. If the script is in a directory that is contained in your path variable, you can execute the script by just typing its name.

As an example, suppose myscript is a C shell script in the current directory.
	% chmod u+x myscript		#make it executable
	% myscript			#run it
		.
		.
	    (myscript runs)
		.
	%				#done!
To insure that your script will be interpreted by the correct shell, you should insert the following line as the very first line, starting in the very first column in your script:

For the Bourne shell:

	#!/bin/sh
For the C shell:
	#!/bin/csh

If you get the message "myscript: Command not found" where myscript is the name of your script, then your current directory is not in your path variable. You could execute the script by typing a relative pathname for the script:

	./myscript

Be sure you have a unique name for your script, or you could be executing a system command, instead of your script. For example, naming a script "test" is not a good idea, because there is a command called test.

While debugging scripts, you might want to use the verbose mode. To do this using the C shell, you can either pass the -v flag to csh, or set the special variable verbose inside your script. For example,

	% csh -v myscript		#run myscript with the verbose option
Comments are preceded by a #. Any characters after a # sign will be ignored.


An Example

The example shell script uptolow will convert any uppercase letters in an ordinary file name to lower case. Since uptolow only works on ordinary files, it will leave directory names alone (although it is trivial to modify the script to handle directories).

To create some example files, you must run the script creator first. This will create five zero length files and one directory in the current directory, if the names don't already exist in the current directory. To make cleanup easier and to avoid renaming existing files, run creator in an empty directory. For example,

uptolow needs to be given arguments, which specify the names of the files it is to process. Wildcards can be used.

To run the tutorial example, first copy the two scripts named creator and uptolow to your current directory. You can copy tutorial files from within your web browser, or use the cp command. Some browsers add or replace characters when files are copied.

Now you must create the test files:

either Method 1:

	csh creator			create the test files
or Method 2:
	chmod u+x creator		make it executable
	creator				run the script

Now you can run the uptolow script. The example below uses the * wildcard as the argument to uptolow. Try specifying a list of files also:

either Method 1:

	csh uptolow *			run uptolow on every file
					in the current directory
or Method 2:
	chmod u+x uptolow		make it executable
	uptolow *			run uptolow on every file
					in the current directory

References


Cleanup

If you ran uptolow on all the example files, then this example will leave the following files and directory in your working directory:

If you only ran uptolow on a subset of the files created by creator, then some file names will still have upper case characters.


[Copyright Statement] [Feedback]

Changed by:$Author: kinsella $,$Date: 1999/07/13 06:45:36 $