13

I am tryin to write a tcsh script. I need the script exit if any of its commands fails.

In shell I use set -e but I don't know its equivalent in tcsh

#!/usr/bin/env tcsh

set NAME=aaaa
set VERSION=6.1

#set -e equivalent
#do somthing

Thanks

ARM
  • 363
  • 1
  • 7
  • 18
  • Did you try it? Else did you check `man tcsh`? Good luck. – shellter Aug 18 '15 at 10:37
  • When I use set -e as in shell, I got this error: set: Variable name must begin with a letter. – ARM Aug 18 '15 at 10:43
  • Looking at the man page, I think you have to use it like `#!/bin/tcsh -e ...` (at the top of the file, of course). Good luck. – shellter Aug 18 '15 at 10:46
  • When I put `#!/bin/tcsh -e` instead of `#!/usr/bin/env tcsh`, the script does no thing. tcsh commands are not executed – ARM Aug 18 '15 at 12:56
  • ok, apparently I didn't guess the correct path where `tcsh` lives.If `#!/usr/bin/env tcsh -e` doesn't work, then find the correct path to `tcsh` and edit the she-bang as needed. Good luck. – shellter Aug 18 '15 at 13:24

1 Answers1

11

In (t)csh, set is used to define a variable; set foo = bar will assign the value bar to the variable foo (like foo=bar does in Bourne shell scripting).

In any case, from tcsh(1):

Argument list processing
   If the first argument (argument 0) to the shell is `-'  then  it  is  a
   login shell.  A login shell can be also specified by invoking the shell
   with the -l flag as the only argument.

   The rest of the flag arguments are interpreted as follows:

[...]

   -e  The  shell  exits  if  any invoked command terminates abnormally or
       yields a non-zero exit status.

So you need to invoke tcsh with the -e flag. Let's test it:

% cat test.csh
true
false

echo ":-)"

% tcsh test.csh
:-)

% tcsh -e test.csh
Exit 1

There is no way to set this at runtime, like with sh's set -e, but you can add it to the hashbang:

#!/bin/tcsh -fe
false

so it gets added automatically when you run ./test.csh, but this will not add it when you type csh test.csh, so my recommendation is to use something like a start.sh which will invoke the csh script:

#!/bin/sh
tcsh -ef realscript.csh
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • You've also set the `-f`-flag here – what does that flag do? – qff Jan 02 '19 at 12:59
  • From tcsh man page ... -f The shell does not load any resource or startup files, or perform any command hashing, and thus starts faster. – Krishna Jul 17 '19 at 05:04