Your script appears to be a mixture of tcsh
and bash
syntax.
As Mark's answer says, the then
keyword has to be on the same line as the if
(unless you use a backslash to splice two lines, but there's not much point in doing that).
For a variable assignment, the set
keyword is not optional; this:
str="$str 1"
is a syntax error in csh/tcsh (it will probably look for a command whose name starts with "str="
.) Write that as:
set str = "$str 1"
Note that you can optionally have spaces around the =
in a set
. tcsh's syntax is a bit messed up:
set foo=bar # ok
set foo = bar # ok
set foo= bar # ok
set foo =bar # error: "Variable name must begin with a letter."
The x=$((x+1))
syntax is specific to bash and related shells. tcsh uses @
for arithmetic assignments:
set x = 42
@ x ++ # sets $x to 43
@ x = $x * 2 # sets $x to 86
If you have a choice, I suggest writing your script to use bash rather than tcsh (you're about halfway there already). Its syntax is much more regular.
The classic rant about csh/tcsh programming can be found here.