Exit tcsh script if error

I am trying to write a tcsh script. I need a script output if any of its commands fail.

In the shell, I use set -e , but I do not know its equivalent in tcsh

 #!/usr/bin/env tcsh set NAME=aaaa set VERSION=6.1 #set -e equivalent #do somthing 

thanks

+6
source share
1 answer

In (t) csh, set used to define a variable; set foo = bar assign the value of bar variable foo (for example, foo=bar in Bourne shell scripts).

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 call tcsh with the -e flag. Let him check it:

 % cat test.csh true false echo ":-)" % tcsh test.csh :-) % tcsh -e test.csh Exit 1 

It is not possible to set this at runtime, for example using sh set -e , but you can add it to hashbang:

 #!/bin/tcsh -fe false 

therefore, it is automatically added when ./test.csh run, but when you enter csh test.csh it will not , so my recommendation is to use something like start.sh that will call the csh script:

 #!/bin/sh tcsh -ef realscript.csh 
+7
source

All Articles