Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit tcsh script if error

Tags:

shell

tcsh

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

like image 866
ARM Avatar asked Aug 18 '15 10:08

ARM


People also ask

How do you exit a script if command fails?

Exit When Any Command Fails This can actually be done with a single line using the set builtin command with the -e option. Putting this at the top of a bash script will cause the script to exit if any commands return a non-zero exit code.

How do I get out of TCSH?

One can log out by typing "^D" on an empty line, "logout" or "login" or via the shell's autologout mechanism (see the autologout shell variable). When a login shell terminates it sets the logout shell variable to "normal" or "automatic" as appropriate, then executes commands from the files /etc/csh.

How do you force quit a shell script?

To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.

How do you exit a script?

One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.


1 Answers

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
like image 105
Martin Tournoij Avatar answered Oct 09 '22 21:10

Martin Tournoij