UNIX Tutorial Six

6.1 What is a script

To a Unix user, a script is a list of commands that are to be run in that particular order. This list of commands is stored in a file, which can be editted to change the order or parameters of how the commands run. The advantages to using a script are often found when you need to run a particular set of commands more than once, or if you just want to make sure you have time to think through the logic of what you need to do to make sure you order your commands correctly. Writing a script is really the essence of computer programming.

Shell Scripting

The real nice thing about shell scripting is that you don't need to learn a computer programming language like C++ or Fortran. Shell scripting just takes the command you would normally run at the command line and puts them into a file to run them together at once. It helps a new programmer to be able to see that when you are doing shell scripting, each command could be run on its own at the command line, and this turns out to be an excellent way to test your shell scripts if they don't produce the desired result.

6.2 How to make a script

We begin making a script by opening a new file with emacs in your act2 directory.

% emacs first.csh &

We begin the file with the first line that tells which type of shell we are using

#/bin/tcsh

and then it is a good idea to add a some comments about this script. We can do that by adding lines that have the # character at the beginning of the line. This tells the shell to ignore the rest of the characters on that line. Go ahead and add these lines to your script

#
# This is my first script.
# It goes to a directory, then lists and counts the contents.
#

In order for us to do what the script says it will, we need to use some commands we have learned already. You can do that by adding the following lines


cd ~/groupwork/act1/
pwd
ls
ls | wc -l

Can you follow how these commands will allow this script to do what we said in the comments? What output do you expect from the script? It will change directories with the cd command, then print the working directory with pwd, then list the contents of that directory with ls, and finally count the number of files with wc.

Shell variables

We also use a shell variable to make the first.csh script more easy to understand and adjust. Shell variables are like the memory key on your calculator in that they hold onto a piece of information for you. Shell variables are a bit more complex because they can store either an integer (a whole number) or a string (any mixture of characters). In this case we will use a variable to store the name of the directory we are changing to. Shell variables are created using the set command. After the comments at the top of your script (after line 5), insert the following line

set directory=~/groupwork/act1

Then you will need to change the cd command you already entered with that location to be

cd $directory

Notice how we use the $ character to indicate that directory is a shell variable and that we want the information stored in that variable when we perform the cd command.

Note: Shell variables are only stored within the script, so they can only be recalled while the script is running. Once the script finished running the information stored in those variables is lost.

6.3 How to run a script

If you feel like your script is ready to run, and it's always a good idea to check it over for any mistakes before running it, then you should make sure to save the script in emacs. You can exit emacs if you want, or you can just save the file. If you don't exit, it allows you to make edits later without reopening the file, which is often what we need to do if there is a problem with our script or we want to add additional commands. The only issue is that you need to remeber to quit all of your emacs windows when you log out.

Once the script file is saved, you should click on your command line window to be able to enter text at the command line.

chmod

Next we need to let Unix know that we want to run this script, so we need to make this file executable. To do this we make a modification to the file type with the chmod command. This command has a variety of options, but for this application, you can use the following simple approach:

% chmod +x first.csh

Execution

At this point, your script should be ready to run. You can execute the script and the command therein by typing the name of the script on the command line.

% first.csh

Your script should produce a variety of output to the command line window, including the directory path, the list of files in your act1 directory, and then it should count the number of files in that directory.

Exercise 6.1

Adjust your script to change the directory variable to ~/groupwork, and then when you run the script direct the output to a file called first.out.

Summary

#/bin/tcsh first line of a script that initializes the type of shell to be tcsh
# text allows text to just be a comment and not be treated as a command
set variable=information set a shell variable to an integer or string of information
chmod +x file make the named file executable
chmod [options] file more generally, change access rights for named file

 

brudzimr@muohio.edu, 19th August 2006