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.
There are two ways you can execute your shell scripts. Once you have created a script file:
sh script_fileor 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
% 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/shFor 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 optionComments are preceded by a #. Any characters after a # sign will be ignored.
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,
mkdir shell.tutorial
cd shell.tutorial
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 filesor 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 directoryor Method 2:
chmod u+x uptolow make it executable uptolow * run uptolow on every file in the current directory
If you ran uptolow on all the example files, then this example will leave the following files and directory in your working directory: