C_SHELL
C Shell Script Examples


C_SHELL contains some examples of useful or interesting C Shell scripts.

Usage:

A C Shell script is usually stored in a file whose extension is .csh.

The first line of a C Shell script should usually be

#!/bin/csh
which guarantees, no matter what shell you are running, that the commands in the script will be processed by the C Shell.

A C Shell script can be executed by giving it as input to the C Shell interpreter:

csh myfile.csh

If the shell script has been made executable:

chmod +x myfile.csh
it can be run directly, as though it were a command:
myfile.csh
or, if your current directory is not in your path, perhaps
./myfile.csh

Related Data and Programs:

BASH_SHELL, examples which illustrate the use of BASH shell scripts.

SGE, examples which illustrate the use of the Sun Grid Engine (SGE), a job queueing system whose input is a modified form of shell scripts in the BASH shell, C shell, and so on.

Reference:

  1. Paul DuBois,
    Using CSH and TCSH,
    O'Reilly and Associates, 1995,
    ISBN: 1-56592-132-1
  2. Some notes on using the C shell

Examples and Tests:

ARGV_EXAMPLE shows the argv variable, which stores the command line arguments for a shell script, can be used to retrieve the (unknown number of) arguments one at a time.

CASH_COW shows how, if you set a shell variable and an environment variable, and then run a shell script, that shell script "sees" your environment variable but not your shell variable. Moreover, changes it makes to either variable will NOT affect your versions of them. Shell variable changes are not shared. Environment variable changes only propagate "downwards" from the master process.

FOREACH_EXAMPLE1 shows how the foreach command can set a variable to each value, in succession, from an explicit list.

FOREACH_EXAMPLE2 shows how the foreach command can set a variable to each value, in succession, from a list that is generated as the_output.txtput of a system command. In this case, a directory listing is generated, and then, for each file in the current directory, the wc command is invoked.

HELLO_WORLD uses the echo command to say "Hello, world!".

INDEX_FILE uses the tr, sort and uniq commands to create a sorted index of the unique words in a text file. This example also illustrates the use of the Unix "pipe" symbol |, which allows the_output.txtput of one command to be used as the input to the next.

WHILE_EXAMPLE1 shows how the while command can be used to set up a DO loop. The C Shell doesn't have a DO statement. You can make your own, but you must keep in mind that the C Shell doesn't have a"less than" relation, nor does it have an easy way to generate all integers from 10 to 25, say. Still, by defining an index counter, testing it for equality with the maximum index value plus 1, and incrementing it, you can set up a DO loop.

You can go up one level to the EXAMPLES page.


Last revised on 17 October 2007.