			

				C Shell


csh: 
	The standard command interpreter used on many unix systems
	is referred to as the c shell. Familiar commands such as
	cd to change a directory or ls to list file names are in
	fact commands to the csh interpreter. A complete description
	of the available commands can be seen by typing man csh.

Scripts:

	In addition to the commonly used interactive execution of
	csh commands at the terminal or with an Xwindow command window,
	csh commands can be stored in a file and executed as if the
	file was a program.  On the first line of the file put,

		#!/bin/csh

	This will insure that the commands are interpreted with the
	csh interpretor.

	Add your csh commands. For example try,

		echo 'hello myself'

	After the script has been prepared with the file editor,
	change the mode of the file to permit execution,
	The command line;

		chmod 755 scriptfile

	will allow read and execution for anyone but write only to yourself.

	Simply typing the filename of the script will then execute the 
	commands in the script.  
	Try a two line script with the echo command above.

Executing xspect fortran programs within a script:

	The various fortran programs in the xspect package for
	modeling x-ray systems are intended for use within scripts.
	In general, the arguments to a particular program are expected
	to be located in the lines following the program execution 
	command. For example,

		spect_gen << EOF
		74  12  1 118 1.0 
		EOF

	When placed in a script this takes the arguments on the second
	line as input to the spect_gen program. << EOF on the first
	line establishes EOF as the end of the input arguments and
	the EOF on the third line ends the entry of the arguments.
	Variables (see below) will be interpreted by the shell if
	placed within the input argument region.
	The program is run and when execution is completed the commands
	that follow in the script are executed.

Variables within scripts:

	Variables can be defined within a script and their value
	substituted as an argument in a command. To define the
	value of an arguement;

		set something = 1.234

	The expression $something will then be replaced with 1.234 
	when used in a command line. For example,

		set something = 1.234
		echo $something

	will print 1.234 on the screen.

Numeric expressions:

	To perform arithmetic operations on two variables the utility
	program called expr which is available on most unix systems
	can be used (note that this is not a shell command). The following
	construction will change the value of a variable based on the
	values of two other variables,

		set value = `expr $value + $increment`

	The `..` instructs the shell to treat the expression between the
	`..` as a command to be interpreted. See the online manual entry
	for expr (ie man expr) for details on the syntax for this command.

Loops within scripts:

	A useful construction to repeat execution of a sequence of
	commands can be seen in the following example,

		foreach something  (10 20 40 80)
	  	...
		echo $something
		...
		end    

	The commands between the foreach line and the end line will be 
	executed with $something taking on values equal to those in the list.

	Example:

		#!/bin/csh
		echo "# test to make a gnuplot file" > temp.dat
		echo "# x    y" >> temp.dat
		foreach value (10 20 40 80)
		echo $value $value >> temp.dat
		end

	This will produce a data file which can be plotted with
	gnuplot. Note the use of the redirection command on the
	echo statements. > redirects to a new file called temp.dat
	and >> adds additional lines to that file.

	A second construction which is useful is the while .. end loop,

		while (relation)
			...
		end

	The shell will evalute simple relationships (ie. ==, !=, etc)
	for the relation. See the online manual for csh for the list
	of operators (man csh).

	This can be illustrated as follows,

		#!/bin/csh
		set value = 0
		set incr = 1
		echo "# test to make a gnuplot file" > temp.dat
		echo "# x    y" >> temp.dat
		while ($value != 8)
			set value2 = `expr $value \* $value`
			echo $value $value2 >> temp.dat
			set value = `expr $value + $incr`
		end

	Note the use of \* to multiply within the expr command.
	This is used since the shell interprets * as a wildcard command.

Example script.

	As an example excercise, write a script for execution that 
	similar to one of the examples in the loop section above.
	For the data written to a file, assume that the
	first column is an independant variable and make the value
	of the second column a dependant expression.
	Thus the data written to this file can be
	plotted using a program such as gnuplot.

Programming with other shells:

	The c shell is a command intrepreter which offers access to many
	commands which are useful for controlling the operation of the system
	and the manipulation of the files and programs which any user 
	might use.  However, it provides only limited
	programming capabilities. Other shells are available with 
	programming capabilities which can be invoked as needed. 
	For more advanced programming you may want to investigate
	the tcl/tk script language which is available on UofM CAEN
	systems (see Tclsh.txt).

Further information:

	Most Unix books will have material on the c shell. The
	original documentation is "An introduction to the C shell" by
	William Joy which is distributed with many Unix manuals. The
	man page which is online contains a rather complete list of
	commands which the c shell interprets. The original documentation
	for the bourne shell is "An Introduction to the Unix Shell" by
	S. R. Bourne is also distributed with many Unix manuals.

MJF 1/16/96
    3/02/03
    1/02/04