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
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.
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.
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.
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”.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With