5 Running a script

 5.1 Shell selection
 5.2 Making a script executable
 5.3 Executing a script by name
 5.4 Using aliases to run a script
 5.5 Removing aliases
 5.6 Executing a script in the current process
 5.7 Package aliases

Unfortunately, we must start with some boring, but important technical stuff. This will tell you how to run your script using the C-shell. The stages are to select the shell, give execute access to the script, and then actually invoke the script using one of three ways.

5.1 Shell selection

The first line of your script tells UNIX which shell you want to use. The following selects the C-shell.


Somewhat perversely, the # is actually the comment character. That’s UNIX for you. However, its presence alone starting the first line will normally be sufficient to run a script in the C shell.

Most of the examples in this document are script excerpts, and so do not include the #!/bin/csh.

5.2 Making a script executable

If you want to run your script by name or with an alias (see below), you must make your script executable like this

       % chmod +x myscript

where myscript is your C-shell script. Remember that the % is a convention for the shell prompt; you do not type it yourself. You can edit the script without having to make the file executable again.

5.3 Executing a script by name

Once your script has execute privilege, thereafter you can run it like this:

       % ./myscript

or if it is situated in directory /home/user1/dro/bin say

       % /home/user1/dro/bin/myscript

would execute your script. This is long-winded if you want to run the the script frequently. To omit the directory path you need to add the current (.) or better, the specific directory to your PATH environment variable.

5.4 Using aliases to run a script

The second method is to define an alias. An alias is a shorthand for a long command to save typing. So in fact this method is just a variant of executing a script by name.

       % alias myscript /home/user1/dro/bin/myscript

Thereafter you would only enter myscript to run your script. Now this alias only applies to your current process. Even child processes derived from the current process will not inherit the alias. The way to make an alias global, so each new C-shell process recognises the alias, is to define the alias in your $HOME/.cshrc file. This file in your login directory defines the ‘environment’ for each C-shell process.

Here is a simple .cshrc file. It sets some global variables: noclobber prevents accidentally overwriting an existing file and history sets the number of previous commands you can recall. It defines three aliases. The first lets you browse through a directory listing. The second reformats the output from df on Digital UNIX so that lines do not wrap. The third lists the largest 20 files in a directory. Finally .cshrc runs the script /star/etc/cshrc to define Starlink aliases (the source command is explained in Section 5.6).

       set noclobber
       set history = 50
       alias dsd ’ls -l \!* | more’
       alias dff "df \!* |awk \
        ’{printf("\"%-20.20s%9.8s%9.8s%9.8s%9.8s %s\\n\",’$1,$2,$3,$4,$5,$6)}’ "’"
       alias big20 ’ls -l | sort -k 5 | tail -n 20’
       source /star/etc/cshrc

5.5 Removing aliases

There are two methods. One is permanent; the other overrides an alias for a single command. To remove an alias permanently use the unalias command. This accepts *?[ ]wildcards to match the defined aliases.

                               # Removes aliases called:
       % unalias myscript      # myscript
       % unalias kap_*         # kap_ followed by zero or more characters
       % unalias compres?      # compres followed by a single character
       % unalias [f-hz][2-5]   # One of f, g, h, or z then an integer
                               # between 2 and 5 inclusive

To override an alias, precede the alias with backslash. So suppose you have an alias to prevent you accidently removing a file as shown below.

       % alias rm rm -i

In a script where you know that you want to remove a file, you don’t want the script to seek confirmation. So suppose you want to delete file myfile, then you would have a line like

       \rm myfile

in your script.

5.6 Executing a script in the current process

The final option is to source your script so that it runs in the current process. The benefit of this technique is that any aliases defined in the current process will be known to your script. For example,

       % source myscript

runs myscript. Any aliases created in myscript will be available to the current process, unlike invocation by name. To save typing you can define an alias to source a script. See the next section on package aliases for an example.

5.7 Package aliases

While convenient, the creation of aliases in your .cshrc file does have a drawback: if you define many aliases in the .cshrc file, it will decelerate process activation. One way around that is to define a few aliases that run other scripts, each of which in turn define many related aliases. Thus you only create the definitions when they are required. This is how most Starlink packages define their commands. Here is an example. Suppose you had a package or a set of related commands called COSMIC installed in directory /home/user2/dro/cosmic, you would first place the following line in your .cshrc file.

       alias cosmic ‘source /home/user2/dro/cosmic/cosmic.csh‘

The left quotes mean execute the command between them. So when you enter

       % cosmic

the script /home/user2/dro/cosmic/cosmic.csh is run. This file might look like the following.

       alias abund /home/user2/dro/cosmic/abundance
       alias filter ‘source /home/user1/abc/bin/garbage.csh‘
       alias kcorr /home/user2/dro/cosmic/k-correction.csh
       alias radial /home/user2/drmoan/noddy/radial
       alias seeing $KAPPA_DIR/psf isize=21 psf=psf21
       alias zcol kcorr bands=UJR noevol