Tcsh programming help

Shell Variables

Shell variables are set using the form set var = value. Space around the = is okay. Environment variables are set with setenv var and examined with printenv.

tcsh% echo $v
v: Undefined variable.
tcsh% set v = 12
tcsh% echo $v
12
tcsh% printenv v
tcsh% tcsh
  tcsh% echo $v
  v: Undefined variable.
  tcsh% printenv v
  tcsh% exit
  exit
tcsh% setenv v 14
tcsh% echo $v
12
tcsh% printenv v
14
tcsh% tcsh
  tcsh% echo $v
  14
  tcsh% printenv v
  14
  tcsh% exit
  exit

Control flow

We can do everything with if statements. The general form is shown below in the shell program. Additional tests on files are given following.

#! /bin/csh -f

# Set some local (shell) variables
set var = 12

if ($var == 12) then
    echo The variable is twelve.
else
    echo The variable is $var.
endif

if (-op filename) then
  expr
endif
opTest
rReadable?
wWritable?
xExecutable?
eExists?
oIs owner of?
zIs empty?
fIs plain file?
dIs directory?
lIs symbolic link?

Repetition

Looping is important. The form for looping in tcsh is:
foreach var ( list )
  statements
end

For example, the script:

foreach n ( 1 2 3 4 )
  echo $n
end

produces

1
2
3
4

corin@cs.washington.edu