6 Some simple examples

Let’s start with a few elementary scripts to show that script writing isn’t difficult, and to illustrate some syntax and constructs.

Suppose that we have an editor such as jed which retains a copy of the previous version of a file by appending to the filename. If at some time we realise that we really want the former version and to erase the new one we could have a script called restore that would perform the operation for an arbitrary file. It might look something like this.

       #!/bin/csh
       rm $1
       mv $1~ $1

The first line demands that the C-shell be used. The $1 represents the first argument supplied on the command line. (There is more on this in Section 7.6.) If the command restore runs the script, then entering

       % restore splat.f

would remove splat.f and replace it with splat.f .

The above script makes some assumptions. First it does not check whether or not the files exist. Let’s correct that.

       #!/bin/csh
       if ( -e $1 && -e $1~ ) then
          rm $1
          mv $1~ $1
       endif

Here we have introduced the if...then...endif construct. When the expression inside the parentheses is true, the following statements are executed until there is an else or endif statement. In this case -e $1 && -e $1  is true only when both files exist. The -e is one of eight file operators for such things as file access and type, and && is a logical AND.

The second assumption in restore is that you’ll always remember to supply a file name.

       #!/bin/csh
       if ( $#argv == 0 ) then
          echo Error: no file name supplied
       else if ( -e $1 && -e $1~ ) then
          rm $1
          mv $1~ $1
       endif

The script now reports an error message  Error: no file name supplied if you forget the argument. $#argv is the number of arguments supplied on the command line. Also notice the else if construct. The == tests for equality.

Instead of an error message you could make the script prompt for a filename.

       #!/bin/csh
       if ( $#argv == 0 ) then
          echo -n "The file to restore >"
          set file = $<
       else
          set file = $1
       endif
       if ( -e $file && -e $file~ ) then
          rm $file
          mv $file~ $file
       else
          if ( ! -e $file ) then
             echo Error: $file does not exist
          endif
          if ( ! -e $file~ ) then
             echo Error: $file~ does not exist
          endif
       endif

When the number of arguments is zero, this script issues the prompt The file to restore >. The echo -n prevents a skip to a new line, and so allows you to enter your response after the prompt string. set file $< equates your response to a shell variable called file. If on the other hand you gave the filename on the command line set file = $1 assigns that name to variable file. When we come to use the variable in other commands it is prefixed with a dollar, meaning “the value of the variable”. Finally, the script now tells you which file or files are missing. Notice how the variable value can be included in the error message.

For your private scripts you need not be as rigorous as this, and in many cases the simplest script suffices. If you intend to use a script frequently and/or sharing with colleagues, it’s worth making a little extra effort such as adding commentary. Comments will help you remember what a lengthy script does after you’ve not run it for a while. It might seem tedious at the time, but you will thank yourself when you come to run it after a long interval. Comment lines begin with #. There are comment examples in some of the longer scripts later in the cookbook.